From 9c97ce20c896e81828aa3e86281333c7b3aac1a3 Mon Sep 17 00:00:00 2001 From: Jan Heuermann Date: Wed, 14 Aug 2024 09:06:30 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Introduce=20=E2=80=9Cbasic=E2=80=9D=20colou?= =?UTF-8?q?r=20theme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- klog/app/cli/config.go | 6 +- klog/app/cli/print.go | 2 +- klog/app/cli/tags.go | 4 +- klog/app/cli/terminalformat/colour_theme.go | 67 +++++++++++++++------ klog/app/cli/terminalformat/style.go | 6 +- klog/app/cli/today.go | 4 +- klog/app/cli/util/prettifier.go | 2 +- klog/app/config.go | 2 + klog/app/text_serialiser.go | 4 +- 9 files changed, 63 insertions(+), 34 deletions(-) diff --git a/klog/app/cli/config.go b/klog/app/cli/config.go index bf7c4ad..58f7bb4 100644 --- a/klog/app/cli/config.go +++ b/klog/app/cli/config.go @@ -26,11 +26,11 @@ func (opt *Config) Run(ctx app.Context) app.Error { opt.NoStyleArgs.Apply(&ctx) styler, _ := ctx.Serialise() for i, e := range app.CONFIG_FILE_ENTRIES { - ctx.Print(styler.Props(tf.StyleProps{Color: tf.SUBDUED}).Format(util.Reflower.Reflow(e.Help.Summary, []string{"# "}))) + ctx.Print(styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}).Format(util.Reflower.Reflow(e.Help.Summary, []string{"# "}))) ctx.Print("\n") - ctx.Print(styler.Props(tf.StyleProps{Color: tf.SUBDUED}).Format(util.Reflower.Reflow("Value: "+e.Help.Value, []string{"# - ", "# "}))) + ctx.Print(styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}).Format(util.Reflower.Reflow("Value: "+e.Help.Value, []string{"# - ", "# "}))) ctx.Print("\n") - ctx.Print(styler.Props(tf.StyleProps{Color: tf.SUBDUED}).Format(util.Reflower.Reflow("Default: "+e.Help.Default, []string{"# - ", "# "}))) + ctx.Print(styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}).Format(util.Reflower.Reflow("Default: "+e.Help.Default, []string{"# - ", "# "}))) ctx.Print("\n") ctx.Print(styler.Props(tf.StyleProps{Color: tf.RED}).Format(e.Name)) ctx.Print(" = ") diff --git a/klog/app/cli/print.go b/klog/app/cli/print.go index ccc97e0..8ab54e6 100644 --- a/klog/app/cli/print.go +++ b/klog/app/cli/print.go @@ -102,7 +102,7 @@ func printWithDurations(styler tf.Styler, ls parser.Lines) string { length := len(p.d.ToString()) value := "" if p.isSub { - value += styler.Props(tf.StyleProps{Color: tf.SUBDUED}).Format(p.d.ToString()) + value += styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}).Format(p.d.ToString()) } else { value += styler.Props(tf.StyleProps{IsUnderlined: true}).Format(p.d.ToString()) } diff --git a/klog/app/cli/tags.go b/klog/app/cli/tags.go index 3227e5c..38d8975 100644 --- a/klog/app/cli/tags.go +++ b/klog/app/cli/tags.go @@ -60,7 +60,7 @@ func (opt *Tags) Run(ctx app.Context) app.Error { table := tf.NewTable(numberOfColumns, " ") for _, t := range totalByTag { totalString := serialiser.Duration(t.Total) - countString := styler.Props(tf.StyleProps{Color: tf.SUBDUED}).Format(fmt.Sprintf(" (%d)", t.Count)) + countString := styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}).Format(fmt.Sprintf(" (%d)", t.Count)) if t.Tag.Value() == "" { table.CellL("#" + t.Tag.Name()) table.CellL(totalString) @@ -71,7 +71,7 @@ func (opt *Tags) Run(ctx app.Context) app.Error { table.CellL(countString) } } else if opt.Values { - table.CellL(" " + styler.Props(tf.StyleProps{Color: tf.SUBDUED}).Format(t.Tag.Value())) + table.CellL(" " + styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}).Format(t.Tag.Value())) table.Skip(1) table.CellL(totalString) if opt.Count { diff --git a/klog/app/cli/terminalformat/colour_theme.go b/klog/app/cli/terminalformat/colour_theme.go index a096e92..e510361 100644 --- a/klog/app/cli/terminalformat/colour_theme.go +++ b/klog/app/cli/terminalformat/colour_theme.go @@ -1,30 +1,21 @@ package terminalformat type ColourTheme string +type colourCodes map[Colour]string const ( COLOUR_THEME_NO_COLOUR = ColourTheme("no_colour") COLOUR_THEME_DARK = ColourTheme("dark") COLOUR_THEME_LIGHT = ColourTheme("light") + COLOUR_THEME_BASIC = ColourTheme("basic") ) func NewStyler(c ColourTheme) Styler { - baseColouredStyler := Styler{ - props: StyleProps{}, - colourCodes: make(map[Colour]string), - reset: "\033[0m", - foregroundPrefix: "\033[38;5;", - backgroundPrefix: "\033[48;5;", - colourSuffix: "m", - underlined: "\033[4m", - bold: "\033[1m", - } - switch c { case COLOUR_THEME_NO_COLOUR: return Styler{ props: StyleProps{}, - colourCodes: make(map[Colour]string), + colourCodes: make(colourCodes), reset: "", foregroundPrefix: "", backgroundPrefix: "", @@ -33,31 +24,67 @@ func NewStyler(c ColourTheme) Styler { bold: "", } case COLOUR_THEME_DARK: - baseColouredStyler.colourCodes = map[Colour]string{ + return newStyler256bit(colourCodes{ TEXT: "015", + TEXT_SUBDUED: "249", TEXT_INVERSE: "000", GREEN: "120", RED: "167", BLUE_DARK: "117", BLUE_LIGHT: "027", - SUBDUED: "249", PURPLE: "213", YELLOW: "221", - } - return baseColouredStyler + }) case COLOUR_THEME_LIGHT: - baseColouredStyler.colourCodes = map[Colour]string{ + return newStyler256bit(colourCodes{ TEXT: "000", + TEXT_SUBDUED: "237", TEXT_INVERSE: "015", GREEN: "028", RED: "124", BLUE_DARK: "025", BLUE_LIGHT: "033", - SUBDUED: "237", PURPLE: "055", YELLOW: "208", - } - return baseColouredStyler + }) + case COLOUR_THEME_BASIC: + return newStyler8bit(colourCodes{ + TEXT: "", // Disabled + TEXT_SUBDUED: "", // Disabled + TEXT_INVERSE: "0", + GREEN: "2", + RED: "1", + BLUE_DARK: "4", + BLUE_LIGHT: "6", + PURPLE: "5", + YELLOW: "3", + }) } panic("Unknown colour theme") } + +func newStyler256bit(cc colourCodes) Styler { + return Styler{ + props: StyleProps{}, + colourCodes: cc, + reset: "\033[0m", + foregroundPrefix: "\033[38;5;", + backgroundPrefix: "\033[48;5;", + colourSuffix: "m", + underlined: "\033[4m", + bold: "\033[1m", + } +} + +func newStyler8bit(cc colourCodes) Styler { + return Styler{ + props: StyleProps{}, + colourCodes: cc, + reset: "\033[0m", + foregroundPrefix: "\033[3", + backgroundPrefix: "\033[4", + colourSuffix: "m", + underlined: "\033[4m", + bold: "\033[1m", + } +} diff --git a/klog/app/cli/terminalformat/style.go b/klog/app/cli/terminalformat/style.go index ac73be2..5ab7b69 100644 --- a/klog/app/cli/terminalformat/style.go +++ b/klog/app/cli/terminalformat/style.go @@ -23,13 +23,13 @@ type Colour int const ( unspecified = iota TEXT + TEXT_SUBDUED TEXT_INVERSE GREEN RED YELLOW BLUE_DARK BLUE_LIGHT - SUBDUED PURPLE ) @@ -50,11 +50,11 @@ func (s Styler) FormatAndRestore(text string, previousStyle Styler) string { func (s Styler) seqs() string { seqs := s.reset - if s.props.Color != unspecified { + if s.props.Color != unspecified && s.colourCodes[s.props.Color] != "" { seqs = seqs + s.foregroundPrefix + s.colourCodes[s.props.Color] + s.colourSuffix } - if s.props.Background != unspecified { + if s.props.Background != unspecified && s.colourCodes[s.props.Background] != "" { seqs = seqs + s.backgroundPrefix + s.colourCodes[s.props.Background] + s.colourSuffix } diff --git a/klog/app/cli/today.go b/klog/app/cli/today.go index 2fdc1ce..b37d551 100644 --- a/klog/app/cli/today.go +++ b/klog/app/cli/today.go @@ -132,7 +132,7 @@ func handle(opt *Today, ctx app.Context) app.Error { table.CellR(serialiser.Time(currentEndTime)) } else { table.CellR( - styler.Props(tf.StyleProps{Color: tf.SUBDUED}). + styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}). Format("(" + currentEndTime.ToString() + ")")) } } else { @@ -177,7 +177,7 @@ func handle(opt *Today, ctx app.Context) app.Error { table.CellR(serialiser.Time(grandEndTime)) } else { table.CellR( - styler.Props(tf.StyleProps{Color: tf.SUBDUED}). + styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}). Format("(" + grandEndTime.ToString() + ")")) } } else { diff --git a/klog/app/cli/util/prettifier.go b/klog/app/cli/util/prettifier.go index 0e5b1b3..a8d969b 100644 --- a/klog/app/cli/util/prettifier.go +++ b/klog/app/cli/util/prettifier.go @@ -42,7 +42,7 @@ func PrettifyParsingError(err app.ParserErrors, styler tf.Styler) error { } message += "\n" message += fmt.Sprintf( - styler.Props(tf.StyleProps{Color: tf.SUBDUED}).Format(INDENT+"%s"), + styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}).Format(INDENT+"%s"), // Replace all tabs with one space each, otherwise the carets might // not be in line with the text anymore (since we can’t know how wide // a tab is). diff --git a/klog/app/config.go b/klog/app/config.go index 95fa417..e0d7424 100644 --- a/klog/app/config.go +++ b/klog/app/config.go @@ -203,6 +203,8 @@ var CONFIG_FILE_ENTRIES = []ConfigFileEntries[any]{ config.ColourScheme.override(tf.COLOUR_THEME_NO_COLOUR, configOriginFile) case string(tf.COLOUR_THEME_LIGHT): config.ColourScheme.override(tf.COLOUR_THEME_LIGHT, configOriginFile) + case string(tf.COLOUR_THEME_BASIC): + config.ColourScheme.override(tf.COLOUR_THEME_BASIC, configOriginFile) default: return errors.New("The value must be `dark`, `light` or `no_colour`") } diff --git a/klog/app/text_serialiser.go b/klog/app/text_serialiser.go index 6a29f47..635a5fd 100644 --- a/klog/app/text_serialiser.go +++ b/klog/app/text_serialiser.go @@ -41,9 +41,9 @@ func (cs TextSerialiser) ShouldTotal(d klog.Duration) string { func (cs TextSerialiser) Summary(s parser.SummaryText) string { txt := s.ToString() - summaryStyler := cs.Styler.Props(tf.StyleProps{Color: tf.SUBDUED}) + summaryStyler := cs.Styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED}) txt = klog.HashTagPattern.ReplaceAllStringFunc(txt, func(h string) string { - return cs.Styler.Props(tf.StyleProps{Color: tf.SUBDUED, IsBold: true}).FormatAndRestore( + return cs.Styler.Props(tf.StyleProps{Color: tf.TEXT_SUBDUED, IsBold: true}).FormatAndRestore( h, summaryStyler, ) }) From ad3514deabd6c62be561e7db765b20f5a1016d46 Mon Sep 17 00:00:00 2001 From: Jan Heuermann Date: Sat, 17 Aug 2024 12:18:24 +0200 Subject: [PATCH 2/2] Extend config help text --- klog/app/config.go | 2 +- klog/app/config_test.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/klog/app/config.go b/klog/app/config.go index e0d7424..e2e42b6 100644 --- a/klog/app/config.go +++ b/klog/app/config.go @@ -215,7 +215,7 @@ var CONFIG_FILE_ENTRIES = []ConfigFileEntries[any]{ }, Help: Help{ Summary: "The colour scheme of your terminal, so that klog can choose an optimal colour theme for its output.", - Value: "The config property must be one of: `dark`, `light` or `no_colour`", + Value: "The config property must be one of: `dark`, `light`, `basic`, or `no_colour`", Default: "If absent/empty, klog assumes a `dark` theme.", }, }, { diff --git a/klog/app/config_test.go b/klog/app/config_test.go index 0e3b616..8689742 100644 --- a/klog/app/config_test.go +++ b/klog/app/config_test.go @@ -106,6 +106,7 @@ func TestSetsColourSchemeParamFromConfigFile(t *testing.T) { }{ {`colour_scheme = dark`, tf.COLOUR_THEME_DARK}, {`colour_scheme = light`, tf.COLOUR_THEME_LIGHT}, + {`colour_scheme = basic`, tf.COLOUR_THEME_BASIC}, {`colour_scheme = no_colour`, tf.COLOUR_THEME_NO_COLOUR}, } { c, _ := NewConfig(