Skip to content

Commit

Permalink
Improve digitalclock module (#1581)
Browse files Browse the repository at this point in the history
* digitalclock: Add bold font

* digitalclock: Add additional settings

- Separate enabling for date, utc and epoch lines
- Ability to show date in widget title
- Central align in widget

* Fix GO-W1027 linter issue

* Fix another linter issue
  • Loading branch information
truman369 authored May 21, 2024
1 parent 9734a72 commit 77be4dd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
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

0 comments on commit 77be4dd

Please sign in to comment.