Skip to content

Commit 77be4dd

Browse files
authored
Improve digitalclock module (#1581)
* 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
1 parent 9734a72 commit 77be4dd

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

modules/digitalclock/display.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func mergeLines(outString []string) string {
77
}
88

99
func renderWidget(widgetSettings Settings) string {
10-
outputStrings := []string{}
10+
var outputStrings []string
1111

1212
clockString, needBorder := renderClock(widgetSettings)
1313
if needBorder {
@@ -17,14 +17,26 @@ func renderWidget(widgetSettings Settings) string {
1717
}
1818

1919
if widgetSettings.withDate {
20-
outputStrings = append(outputStrings, getDate(widgetSettings.dateFormat, widgetSettings.withDatePrefix), getUTC(), getEpoch())
20+
outputStrings = append(outputStrings, getDate(widgetSettings.dateFormat, widgetSettings.withDatePrefix))
21+
}
22+
23+
if widgetSettings.withUTC {
24+
outputStrings = append(outputStrings, getUTC())
25+
}
26+
27+
if widgetSettings.withEpoch {
28+
outputStrings = append(outputStrings, getEpoch())
2129
}
2230

2331
return mergeLines(outputStrings)
2432
}
2533

2634
func (widget *Widget) display() {
2735
widget.Redraw(func() (string, string, bool) {
28-
return widget.CommonSettings().Title, renderWidget(*widget.settings), false
36+
title := widget.CommonSettings().Title
37+
if widget.settings.dateTitle {
38+
title = getDate(widget.settings.dateFormat, false)
39+
}
40+
return title, renderWidget(*widget.settings), false
2941
})
3042
}

modules/digitalclock/fonts.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,36 @@ func getBigFont() ClockFont {
7979
return bigFont
8080
}
8181

82+
func getBoldFont() ClockFont {
83+
fontsMap := map[string][]string{
84+
"1": {"██", "██", "██", "██", "██"},
85+
"2": {"██████", " ██", "██████", "██ ", "██████"},
86+
"3": {"██████", " ██", "██████", " ██", "██████"},
87+
"4": {"██ ██", "██ ██", "██████", " ██", " ██"},
88+
"5": {"██████", "██ ", "██████", " ██", "██████"},
89+
"6": {"██████", "██ ", "██████", "██ ██", "██████"},
90+
"7": {"██████", " ██", " ██", " ██", " ██"},
91+
"8": {"██████", "██ ██", "██████", "██ ██", "██████"},
92+
"9": {"██████", "██ ██", "██████", " ██", "██████"},
93+
"0": {"██████", "██ ██", "██ ██", "██ ██", "██████"},
94+
":": {" ", "██", " ", "██", " "},
95+
" ": {" ", " ", " ", " ", " "},
96+
"A": {"", "", "", "", "AM"},
97+
"P": {"", "", "", "", "PM"},
98+
}
99+
100+
boldFont := ClockFont{fontRows: 5, fonts: fontsMap}
101+
return boldFont
102+
}
103+
82104
// getFont returns appropriate font map based on the font settings
83105
func getFont(widgetSettings Settings) ClockFont {
84-
if strings.ToLower(widgetSettings.font) == "digitalfont" {
106+
switch strings.ToLower(widgetSettings.font) {
107+
case "digitalfont":
85108
return getDigitalFont()
109+
case "boldfont":
110+
return getBoldFont()
111+
default:
112+
return getBigFont()
86113
}
87-
return getBigFont()
88114
}

modules/digitalclock/settings.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ type Settings struct {
1818
font string `help:"The font of the clock." values:"bigfont or digitalfont"`
1919
hourFormat string `help:"The format of the clock." values:"12 or 24"`
2020
dateFormat string `help:"The format of the date."`
21+
dateTitle bool `help:"Whether or not to display date as widget title"`
2122
withDate bool `help:"Whether or not to display date information"`
23+
withUTC bool `help:"Whether or not to display UTC information"`
24+
withEpoch bool `help:"Whether or not to display Epoch information"`
2225
withDatePrefix bool `help:"Whether or not to display Date: prefix"`
26+
centerAlign bool `help:"Whether or not to use center align in widget"`
2327
}
2428

2529
// NewSettingsFromYAML creates a new settings instance from a YAML config block
@@ -31,8 +35,12 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
3135
font: ymlConfig.UString("font"),
3236
hourFormat: ymlConfig.UString("hourFormat", "24"),
3337
dateFormat: ymlConfig.UString("dateFormat", "Monday January 02 2006"),
38+
dateTitle: ymlConfig.UBool("dateTitle", false),
3439
withDate: ymlConfig.UBool("withDate", true),
40+
withUTC: ymlConfig.UBool("withUTC", true),
41+
withEpoch: ymlConfig.UBool("withEpoch", true),
3542
withDatePrefix: ymlConfig.UBool("withDatePrefix", true),
43+
centerAlign: ymlConfig.UBool("centerAlign", false),
3644
}
3745

3846
return &settings

modules/digitalclock/widget.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Sett
1919

2020
settings: settings,
2121
}
22+
if settings.centerAlign {
23+
widget.View.SetTextAlign(tview.AlignCenter)
24+
}
2225

2326
return &widget
2427
}

0 commit comments

Comments
 (0)