From 4d4ed17228a072f1df328e3d8cd47b9cbffc3273 Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Thu, 25 Jul 2013 17:34:41 -0700 Subject: [PATCH] Show sort order indicator in the header --- lib/column_editor.go | 20 +++++++++++++------- lib/layout.go | 27 ++++++++++++++++++++------- mop.go | 2 +- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/column_editor.go b/lib/column_editor.go index 5b92260..e6738f1 100644 --- a/lib/column_editor.go +++ b/lib/column_editor.go @@ -3,21 +3,21 @@ package mop import ( - // `regexp` - // `strings` `github.com/nsf/termbox-go` ) type ColumnEditor struct { screen *Screen layout *Layout + quotes *Quotes profile *Profile } //----------------------------------------------------------------------------- -func (self *ColumnEditor) Initialize(screen *Screen, profile *Profile) *ColumnEditor { +func (self *ColumnEditor) Initialize(screen *Screen, quotes *Quotes) *ColumnEditor { self.screen = screen - self.profile = profile + self.quotes = quotes + self.profile = quotes.profile self.layout = new(Layout).Initialize() self.select_current_column() @@ -33,7 +33,7 @@ func (self *ColumnEditor) Handle(ev termbox.Event) bool { return self.done() case termbox.KeyEnter: - return self.execute().done() + self.execute() case termbox.KeyArrowLeft: self.select_left_column() @@ -72,7 +72,13 @@ func (self *ColumnEditor) select_right_column() *ColumnEditor { //----------------------------------------------------------------------------- func (self *ColumnEditor) execute() *ColumnEditor { - + if self.profile.selected_column == self.profile.SortColumn { + self.profile.Ascending = !self.profile.Ascending + } else { + self.profile.SortColumn = self.profile.selected_column + } + self.profile.Save() + self.screen.Draw(self.quotes) return self } @@ -84,7 +90,7 @@ func (self *ColumnEditor) done() bool { //----------------------------------------------------------------------------- func (self *ColumnEditor) redraw_header() { - self.screen.DrawLine(0, 4, self.layout.Header(self.profile.selected_column)) + self.screen.DrawLine(0, 4, self.layout.Header(self.profile)) termbox.Flush() } diff --git a/lib/layout.go b/lib/layout.go index a68a5e5..8b996bd 100644 --- a/lib/layout.go +++ b/lib/layout.go @@ -96,7 +96,7 @@ func (self *Layout) Quotes(quotes *Quotes) string { Stocks []Stock }{ time.Now().Format(`3:04:05pm PST`), - self.Header(quotes.profile.selected_column), + self.Header(quotes.profile), self.prettify(quotes), } @@ -123,18 +123,19 @@ func (self *Layout) Quotes(quotes *Quotes) string { } //----------------------------------------------------------------------------- -func (self *Layout) Header(selected_column int) string { - str := `` +func (self *Layout) Header(profile *Profile) string { + str, selected_column := ``, profile.selected_column + for i,col := range self.columns { + arrow := arrow_for(i, profile) if i != selected_column { - str += fmt.Sprintf(`%*s`, col.width, col.title) + str += fmt.Sprintf(`%*s`, col.width, arrow + col.title) } else { - str += fmt.Sprintf(`%*s`, col.width, col.title) + str += fmt.Sprintf(`%*s`, col.width, arrow + col.title) } } - str += `` - return str + return `` + str + `` } //----------------------------------------------------------------------------- @@ -183,6 +184,18 @@ func group(quotes *Quotes) []Stock { } } +//----------------------------------------------------------------------------- +func arrow_for(column int, profile *Profile) string { + if column == profile.SortColumn { + if profile.Ascending { + return string('\U00002193') + } else { + return string('\U00002191') + } + } + return `` +} + //----------------------------------------------------------------------------- func nullify(str string) string { if len(str) == 3 && str[0:3] == `N/A` { diff --git a/mop.go b/mop.go index 6637dc1..1c6ca91 100644 --- a/mop.go +++ b/mop.go @@ -40,7 +40,7 @@ loop: line_editor = new(mop.LineEditor).Initialize(screen, quotes) line_editor.Prompt(event.Ch) } else if event.Ch == 'o' || event.Ch == 'O' { - column_editor = new(mop.ColumnEditor).Initialize(screen, profile) + column_editor = new(mop.ColumnEditor).Initialize(screen, quotes) } else if event.Ch == 'g' || event.Ch == 'G' { profile.Regroup() screen.Draw(quotes)