From fd1e3b9089e2a352bde6a08e7533b273a6428f8e Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Thu, 1 Aug 2013 22:27:05 -0700 Subject: [PATCH] Refactored updating sort order --- column_editor.go | 9 +++------ layout.go | 37 +++++++++++++++++-------------------- markup.go | 4 ++-- profile.go | 12 ++++++++++++ yahoo_quotes.go | 8 ++++++++ 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/column_editor.go b/column_editor.go index e6738f1..bdc7b92 100644 --- a/column_editor.go +++ b/column_editor.go @@ -72,13 +72,10 @@ 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 + if updated,_ := self.quotes.UpdateSortOrder(); updated { + self.screen.Draw(self.quotes) } - self.profile.Save() - self.screen.Draw(self.quotes) + return self } diff --git a/layout.go b/layout.go index b4f5928..86035bb 100644 --- a/layout.go +++ b/layout.go @@ -36,9 +36,9 @@ func (self *Layout) Initialize() *Layout { { 10, `52w High` }, { 11, `Volume` }, { 11, `AvgVolume` }, - { 10, `P/E` }, - { 10, `Dividend` }, - { 10, `Yield` }, + { 9, `P/E` }, + { 9, `Dividend` }, + { 9, `Yield` }, { 11, `MktCap` }, } @@ -186,9 +186,8 @@ func arrow_for(column int, profile *Profile) string { if column == profile.SortColumn { if profile.Ascending { return string('\U00002191') - } else { - return string('\U00002193') } + return string('\U00002193') } return `` } @@ -197,50 +196,48 @@ func arrow_for(column int, profile *Profile) string { func blank(str string) string { if len(str) == 3 && str[0:3] == `N/A` { return `-` - } else { - return str } + + return str } //----------------------------------------------------------------------------- func blank_currency(str string) string { if str == `0.00` { return `-` - } else { - return currency(str) } + + return currency(str) } //----------------------------------------------------------------------------- func last(str string) string { if len(str) >= 6 && str[0:6] != `N/A - ` { return str - } else { - return str[6:] } + + return str[6:] } //----------------------------------------------------------------------------- func currency(str string) string { if str == `N/A` { return `-` - } else { - switch str[0:1] { - case `+`, `-`: - return str[0:1] + `$` + str[1:] - default: - return `$` + str - } } + if sign := str[0:1]; sign == `+` || sign == `-` { + return sign + `$` + str[1:] + } + + return `$` + str } //----------------------------------------------------------------------------- func percent(str string) string { if str == `N/A` { return `-` - } else { - return str + `%` } + + return str + `%` } //----------------------------------------------------------------------------- diff --git a/markup.go b/markup.go index aedb2df..5bb6a6d 100644 --- a/markup.go +++ b/markup.go @@ -132,7 +132,7 @@ func extract_tag_name(str string) string { return str[1 : len(str)-1] } else if len(str) > 3 { return str[2 : len(str)-1] - } else { - return `/` } + + return `/` } diff --git a/profile.go b/profile.go index 86f5290..16245b1 100644 --- a/profile.go +++ b/profile.go @@ -97,6 +97,18 @@ func (self *Profile) RemoveTickers(tickers []string) (removed int, err error) { return } +//----------------------------------------------------------------------------- +func (self *Profile) UpdateSortOrder() (updated bool, err error) { + if self.selected_column == self.SortColumn { + self.Ascending = !self.Ascending + } else { + self.SortColumn = self.selected_column + } + err = self.Save() + updated = (err == nil) + return +} + // private //----------------------------------------------------------------------------- func (self *Profile) default_file_name() string { diff --git a/yahoo_quotes.go b/yahoo_quotes.go index 6910b30..19714ec 100644 --- a/yahoo_quotes.go +++ b/yahoo_quotes.go @@ -123,6 +123,14 @@ func (self *Quotes) RemoveTickers(tickers []string) (removed int, err error) { return } +//----------------------------------------------------------------------------- +func (self *Quotes) UpdateSortOrder() (updated bool, err error) { + if updated, err = self.profile.UpdateSortOrder(); updated { + self.stocks = nil // Force fetch. + } + return +} + //----------------------------------------------------------------------------- func (self *Quotes) parse(body []byte) *Quotes { lines := bytes.Split(body, []byte{'\n'})