Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve digitalclock module #1581

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions modules/digitalclock/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func mergeLines(outString []string) string {
}

func renderWidget(widgetSettings Settings) string {
outputStrings := []string{}
var outputStrings []string

clockString, needBorder := renderClock(widgetSettings)
if needBorder {
Expand All @@ -17,14 +17,26 @@ func renderWidget(widgetSettings Settings) string {
}

if widgetSettings.withDate {
outputStrings = append(outputStrings, getDate(widgetSettings.dateFormat, widgetSettings.withDatePrefix), getUTC(), getEpoch())
outputStrings = append(outputStrings, getDate(widgetSettings.dateFormat, widgetSettings.withDatePrefix))
}

if widgetSettings.withUTC {
outputStrings = append(outputStrings, getUTC())
}

if widgetSettings.withEpoch {
outputStrings = append(outputStrings, getEpoch())
}

return mergeLines(outputStrings)
}

func (widget *Widget) display() {
widget.Redraw(func() (string, string, bool) {
return widget.CommonSettings().Title, renderWidget(*widget.settings), false
title := widget.CommonSettings().Title
if widget.settings.dateTitle {
title = getDate(widget.settings.dateFormat, false)
}
return title, renderWidget(*widget.settings), false
})
}
30 changes: 28 additions & 2 deletions modules/digitalclock/fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,36 @@ func getBigFont() ClockFont {
return bigFont
}

func getBoldFont() ClockFont {
fontsMap := map[string][]string{
"1": {"██", "██", "██", "██", "██"},
"2": {"██████", " ██", "██████", "██ ", "██████"},
"3": {"██████", " ██", "██████", " ██", "██████"},
"4": {"██ ██", "██ ██", "██████", " ██", " ██"},
"5": {"██████", "██ ", "██████", " ██", "██████"},
"6": {"██████", "██ ", "██████", "██ ██", "██████"},
"7": {"██████", " ██", " ██", " ██", " ██"},
"8": {"██████", "██ ██", "██████", "██ ██", "██████"},
"9": {"██████", "██ ██", "██████", " ██", "██████"},
"0": {"██████", "██ ██", "██ ██", "██ ██", "██████"},
":": {" ", "██", " ", "██", " "},
" ": {" ", " ", " ", " ", " "},
"A": {"", "", "", "", "AM"},
"P": {"", "", "", "", "PM"},
}

boldFont := ClockFont{fontRows: 5, fonts: fontsMap}
return boldFont
}

// getFont returns appropriate font map based on the font settings
func getFont(widgetSettings Settings) ClockFont {
if strings.ToLower(widgetSettings.font) == "digitalfont" {
switch strings.ToLower(widgetSettings.font) {
case "digitalfont":
return getDigitalFont()
case "boldfont":
return getBoldFont()
default:
return getBigFont()
}
return getBigFont()
}
8 changes: 8 additions & 0 deletions modules/digitalclock/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ type Settings struct {
font string `help:"The font of the clock." values:"bigfont or digitalfont"`
hourFormat string `help:"The format of the clock." values:"12 or 24"`
dateFormat string `help:"The format of the date."`
dateTitle bool `help:"Whether or not to display date as widget title"`
withDate bool `help:"Whether or not to display date information"`
withUTC bool `help:"Whether or not to display UTC information"`
withEpoch bool `help:"Whether or not to display Epoch information"`
withDatePrefix bool `help:"Whether or not to display Date: prefix"`
centerAlign bool `help:"Whether or not to use center align in widget"`
}

// NewSettingsFromYAML creates a new settings instance from a YAML config block
Expand All @@ -31,8 +35,12 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
font: ymlConfig.UString("font"),
hourFormat: ymlConfig.UString("hourFormat", "24"),
dateFormat: ymlConfig.UString("dateFormat", "Monday January 02 2006"),
dateTitle: ymlConfig.UBool("dateTitle", false),
withDate: ymlConfig.UBool("withDate", true),
withUTC: ymlConfig.UBool("withUTC", true),
withEpoch: ymlConfig.UBool("withEpoch", true),
withDatePrefix: ymlConfig.UBool("withDatePrefix", true),
centerAlign: ymlConfig.UBool("centerAlign", false),
}

return &settings
Expand Down
3 changes: 3 additions & 0 deletions modules/digitalclock/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Sett

settings: settings,
}
if settings.centerAlign {
widget.View.SetTextAlign(tview.AlignCenter)
}

return &widget
}
Expand Down