From 6d683f196137b71ebd93951b3eae90771f096da9 Mon Sep 17 00:00:00 2001 From: Mohit Agarwal Date: Thu, 21 Mar 2024 13:43:46 +0000 Subject: [PATCH] Adding alternate row shading for readability. --- markup.go | 3 +++ profile.go | 5 +++++ screen.go | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/markup.go b/markup.go index 5f0f32d..9dd4ae1 100644 --- a/markup.go +++ b/markup.go @@ -27,6 +27,7 @@ import ( type Markup struct { Foreground termbox.Attribute // Foreground color. Background termbox.Attribute // Background color (so far always termbox.ColorDefault). + RowShading termbox.Attribute // Background color for Row Shading. RightAligned bool // True when the string is right aligned. tags map[string]termbox.Attribute // Tags to Termbox translation hash. regex *regexp.Regexp // Regex to identify the supported tag names. @@ -74,6 +75,8 @@ func NewMarkup(profile *Profile) *Markup { markup.Background = termbox.ColorDefault markup.RightAligned = false + markup.RowShading = markup.tags[profile.Colors.RowShading] + markup.regex = markup.supportedTags() // Once we have the hash we could build the regex. return markup diff --git a/profile.go b/profile.go index dc23824..dd1698f 100644 --- a/profile.go +++ b/profile.go @@ -32,6 +32,7 @@ type Profile struct { Grouped bool // True when stocks are grouped by advancing/declining. Filter string // Filter in human form UpDownJump int // Number of lines to go up/down when scrolling. + RowShading bool // Should alternate rows be shaded? Colors struct { // User defined colors Gain string Loss string @@ -39,6 +40,7 @@ type Profile struct { Header string Time string Default string + RowShading string } ShowTimestamp bool // Show or hide current time in the top right of the screen filterExpression *govaluate.EvaluableExpression // The filter as a govaluate expression @@ -86,6 +88,7 @@ func NewProfile(filename string) (*Profile, error) { InitColor(&profile.Colors.Header, defaultHeaderColor) InitColor(&profile.Colors.Time, defaultTimeColor) InitColor(&profile.Colors.Default, defaultColor) + InitColor(&profile.Colors.RowShading, defaultColor) profile.SetFilter(profile.Filter) } @@ -118,6 +121,8 @@ func (profile *Profile) InitDefaultProfile() { profile.Colors.Header = defaultHeaderColor profile.Colors.Time = defaultTimeColor profile.Colors.Default = defaultColor + profile.Colors.RowShading = defaultColor + profile.RowShading = false profile.ShowTimestamp = false profile.Save() } diff --git a/screen.go b/screen.go index 9a8e024..dbfef0b 100644 --- a/screen.go +++ b/screen.go @@ -22,6 +22,7 @@ type Screen struct { layout *Layout // Pointer to layout (gets created by screen). markup *Markup // Pointer to markup processor (gets created by screen). pausedAt *time.Time // Timestamp of the pause request or nil if none. + profile *Profile // Pointer to profile passed to NewScreen offset int // Offset for scolling headerLine int // Line number of header for scroll feature max int // highest offset @@ -37,6 +38,7 @@ func NewScreen(profile *Profile) *Screen { screen := &Screen{} screen.layout = NewLayout() screen.markup = NewMarkup(profile) + screen.profile = profile screen.offset = 0 return screen.Resize() @@ -188,8 +190,21 @@ func (screen *Screen) DrawLineFlush(x int, y int, str string, flush bool) { } else { start = screen.width - len(token) + i } - termbox.SetCell(start, y, char, screen.markup.Foreground, screen.markup.Background) + if y % 2 == 0 && y > 4 && screen.profile.RowShading { + termbox.SetCell(start, y, char, screen.markup.Foreground, screen.markup.RowShading) + } else { + termbox.SetCell(start, y, char, screen.markup.Foreground, screen.markup.Background) + } + } + if screen.profile.RowShading { + if start < screen.width && y % 2 == 0 && y > 4 { + for i := start ; i < screen.width; i++ { + start ++; + termbox.SetCell(start, y, ' ', termbox.ColorDefault, screen.markup.RowShading); + } + } + } } if flush { termbox.Flush() @@ -213,7 +228,7 @@ func (screen *Screen) DrawLineFlushInverted(x int, y int, str string, flush bool } else { start = screen.width - len(token) + i } - termbox.SetCell(start, y, char, screen.markup.tags[`black`], screen.markup.Foreground) + termbox.SetCell(start, y, char, screen.markup.tags[`black`], screen.markup.Foreground) } } if flush {