Small optimizations

master
Michael Dvorkin 11 years ago
parent c514053803
commit 79a1d5b508
  1. 67
      layout.go

@ -22,6 +22,7 @@ type Column struct {
type Layout struct { type Layout struct {
columns []Column columns []Column
regex *regexp.Regexp
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -43,6 +44,7 @@ func (self *Layout) Initialize() *Layout {
{ 9, `Yield` }, { 9, `Yield` },
{ 11, `MktCap` }, { 11, `MktCap` },
} }
self.regex = regexp.MustCompile(`(\.\d+)[MB]?$`)
return self return self
} }
@ -117,21 +119,21 @@ func (self *Layout) prettify(quotes *Quotes) []Stock {
pretty := make([]Stock, len(quotes.stocks)) pretty := make([]Stock, len(quotes.stocks))
for i, q := range quotes.stocks { for i, q := range quotes.stocks {
pretty[i].Ticker = pad(q.Ticker, self.columns[0].width) pretty[i].Ticker = self.pad(q.Ticker, 0)
pretty[i].LastTrade = pad(currency(q.LastTrade), self.columns[1].width) pretty[i].LastTrade = self.pad(currency(q.LastTrade), 1)
pretty[i].Change = pad(currency(q.Change), self.columns[2].width) pretty[i].Change = self.pad(currency(q.Change), 2)
pretty[i].ChangePct = pad(last(q.ChangePct), self.columns[3].width) pretty[i].ChangePct = self.pad(last(q.ChangePct), 3)
pretty[i].Open = pad(currency(q.Open), self.columns[4].width) pretty[i].Open = self.pad(currency(q.Open), 4)
pretty[i].Low = pad(currency(q.Low), self.columns[5].width) pretty[i].Low = self.pad(currency(q.Low), 5)
pretty[i].High = pad(currency(q.High), self.columns[6].width) pretty[i].High = self.pad(currency(q.High), 6)
pretty[i].Low52 = pad(currency(q.Low52), self.columns[7].width) pretty[i].Low52 = self.pad(currency(q.Low52), 7)
pretty[i].High52 = pad(currency(q.High52), self.columns[8].width) pretty[i].High52 = self.pad(currency(q.High52), 8)
pretty[i].Volume = pad(q.Volume, self.columns[9].width) pretty[i].Volume = self.pad(q.Volume, 9)
pretty[i].AvgVolume = pad(q.AvgVolume, self.columns[10].width) pretty[i].AvgVolume = self.pad(q.AvgVolume, 10)
pretty[i].PeRatio = pad(blank(q.PeRatioX), self.columns[11].width) pretty[i].PeRatio = self.pad(blank(q.PeRatioX), 11)
pretty[i].Dividend = pad(blank_currency(q.Dividend), self.columns[12].width) pretty[i].Dividend = self.pad(blank_currency(q.Dividend), 12)
pretty[i].Yield = pad(percent(q.Yield), self.columns[13].width) pretty[i].Yield = self.pad(percent(q.Yield), 13)
pretty[i].MarketCap = pad(currency(q.MarketCapX), self.columns[14].width) pretty[i].MarketCap = self.pad(currency(q.MarketCapX), 14)
pretty[i].Advancing = q.Advancing pretty[i].Advancing = q.Advancing
} }
@ -148,6 +150,21 @@ func (self *Layout) prettify(quotes *Quotes) []Stock {
return pretty return pretty
} }
//-----------------------------------------------------------------------------
func (self *Layout) pad(str string, col int) string {
match := self.regex.FindStringSubmatch(str)
if len(match) > 0 {
switch len(match[1]) {
case 2:
str = strings.Replace(str, match[1], match[1] + `0`, 1)
case 4, 5:
str = strings.Replace(str, match[1], match[1][0:3], 1)
}
}
return fmt.Sprintf(`%*s`, self.columns[col].width, str)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func highlight(collections ...map[string]string) { func highlight(collections ...map[string]string) {
for _, collection := range collections { for _, collection := range collections {
@ -163,13 +180,13 @@ func group(stocks []Stock) []Stock {
current := 0 current := 0
for _,stock := range stocks { for _,stock := range stocks {
if strings.Index(stock.Change, "-") == -1 { if stock.Advancing {
grouped[current] = stock grouped[current] = stock
current++ current++
} }
} }
for _,stock := range stocks { for _,stock := range stocks {
if strings.Index(stock.Change, "-") != -1 { if !stock.Advancing {
grouped[current] = stock grouped[current] = stock
current++ current++
} }
@ -236,19 +253,3 @@ func percent(str string) string {
return str + `%` return str + `%`
} }
//-----------------------------------------------------------------------------
func pad(str string, width int) string {
re := regexp.MustCompile(`(\.\d+)[MB]?$`)
match := re.FindStringSubmatch(str)
if len(match) > 0 {
switch len(match[1]) {
case 2:
str = strings.Replace(str, match[1], match[1] + `0`, 1)
case 4, 5:
str = strings.Replace(str, match[1], match[1][0:3], 1)
}
}
return fmt.Sprintf(`%*s`, width, str)
}

Loading…
Cancel
Save