|
|
@ -10,59 +10,90 @@ import ( |
|
|
|
`time` |
|
|
|
`time` |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Can combine attributes and a single color using bitwise OR.
|
|
|
|
type Screen struct { |
|
|
|
//
|
|
|
|
width int |
|
|
|
// AttrBold Attribute = 1 << (iota + 4)
|
|
|
|
height int |
|
|
|
// AttrUnderline
|
|
|
|
tags map[string]termbox.Attribute |
|
|
|
// AttrReverse
|
|
|
|
} |
|
|
|
//
|
|
|
|
|
|
|
|
var tags = map[string]termbox.Attribute{ |
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
`black`: termbox.ColorBlack, |
|
|
|
func (self *Screen) Initialize() *Screen { |
|
|
|
`red`: termbox.ColorRed, |
|
|
|
err := termbox.Init() |
|
|
|
`green`: termbox.ColorGreen, |
|
|
|
if err != nil { |
|
|
|
`yellow`: termbox.ColorYellow, |
|
|
|
panic(err) |
|
|
|
`blue`: termbox.ColorBlue, |
|
|
|
} |
|
|
|
`magenta`: termbox.ColorMagenta, |
|
|
|
|
|
|
|
`cyan`: termbox.ColorCyan, |
|
|
|
self.Resize() |
|
|
|
`white`: termbox.ColorWhite, |
|
|
|
self.tags = make(map[string]termbox.Attribute) |
|
|
|
`right`: termbox.ColorDefault, |
|
|
|
self.tags[`black`] = termbox.ColorBlack |
|
|
|
|
|
|
|
self.tags[`red`] = termbox.ColorRed |
|
|
|
|
|
|
|
self.tags[`green`] = termbox.ColorGreen |
|
|
|
|
|
|
|
self.tags[`yellow`] = termbox.ColorYellow |
|
|
|
|
|
|
|
self.tags[`blue`] = termbox.ColorBlue |
|
|
|
|
|
|
|
self.tags[`magenta`] = termbox.ColorMagenta |
|
|
|
|
|
|
|
self.tags[`cyan`] = termbox.ColorCyan |
|
|
|
|
|
|
|
self.tags[`white`] = termbox.ColorWhite |
|
|
|
|
|
|
|
self.tags[`right`] = termbox.ColorDefault // Termbox can combine attributes and a single color using bitwise OR.
|
|
|
|
|
|
|
|
self.tags[`b`] = termbox.AttrBold // Attribute = 1 << (iota + 4)
|
|
|
|
|
|
|
|
self.tags[`u`] = termbox.AttrUnderline |
|
|
|
|
|
|
|
self.tags[`r`] = termbox.AttrReverse |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func DrawMarket() { |
|
|
|
func (self *Screen) Resize() *Screen { |
|
|
|
|
|
|
|
self.width, self.height = termbox.Size() |
|
|
|
|
|
|
|
return self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *Screen) Clear() *Screen { |
|
|
|
|
|
|
|
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) |
|
|
|
|
|
|
|
return self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *Screen) Close() { |
|
|
|
|
|
|
|
termbox.Close() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *Screen) DrawMarket() { |
|
|
|
market := GetMarket() |
|
|
|
market := GetMarket() |
|
|
|
drawScreen(FormatMarket(market)) |
|
|
|
self.draw(FormatMarket(market)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func DrawQuotes(stocks string) { |
|
|
|
func (self *Screen) DrawQuotes(stocks string) { |
|
|
|
quotes := GetQuotes(stocks) |
|
|
|
quotes := GetQuotes(stocks) |
|
|
|
drawScreen(FormatQuotes(quotes)) |
|
|
|
self.draw(FormatQuotes(quotes)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func DrawTime() { |
|
|
|
func (self *Screen) DrawTime() { |
|
|
|
now := time.Now().Format(`3:04:05pm PST`) |
|
|
|
now := time.Now().Format(`3:04:05pm PST`) |
|
|
|
DrawLine(0, 0, `<right>` + now + `</right>`) |
|
|
|
self.DrawLine(0, 0, `<right>` + now + `</right>`) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func ClearLine(x int, y int) { |
|
|
|
func (self *Screen) ClearLine(x int, y int) { |
|
|
|
width, _ := termbox.Size() |
|
|
|
for i := x; i < self.width; i++ { |
|
|
|
for i := x; i < width; i++ { |
|
|
|
|
|
|
|
termbox.SetCell(i, y, ' ', termbox.ColorDefault, termbox.ColorDefault) |
|
|
|
termbox.SetCell(i, y, ' ', termbox.ColorDefault, termbox.ColorDefault) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
termbox.Flush() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func DrawLine(x int, y int, str string) { |
|
|
|
func (self *Screen) DrawLine(x int, y int, str string) { |
|
|
|
column, right := 0, false |
|
|
|
column, right := 0, false |
|
|
|
foreground, background := termbox.ColorDefault, termbox.ColorDefault |
|
|
|
foreground, background := termbox.ColorDefault, termbox.ColorDefault |
|
|
|
|
|
|
|
|
|
|
|
for _, token := range just.Split(tagsRegexp(), str) { |
|
|
|
for _, token := range just.Split(self.possible_tags(), str) { |
|
|
|
if tag, open := isTag(token); tag { |
|
|
|
if tag, open := self.is_tag(token); tag { |
|
|
|
key := tagName(token) |
|
|
|
key := self.tag_name(token) |
|
|
|
if value, ok := tags[key]; ok { |
|
|
|
if value, ok := self.tags[key]; ok { |
|
|
|
token = `` |
|
|
|
token = `` |
|
|
|
switch key { |
|
|
|
switch key { |
|
|
|
case `right`: |
|
|
|
case `right`: |
|
|
@ -81,8 +112,7 @@ func DrawLine(x int, y int, str string) { |
|
|
|
if !right { |
|
|
|
if !right { |
|
|
|
termbox.SetCell(x+column, y, char, foreground, background) |
|
|
|
termbox.SetCell(x+column, y, char, foreground, background) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
width, _ := termbox.Size() |
|
|
|
termbox.SetCell(self.width-len(token)+i, y, char, foreground, background) |
|
|
|
termbox.SetCell(width-len(token)+i, y, char, foreground, background) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
column += 1 |
|
|
|
column += 1 |
|
|
|
} |
|
|
|
} |
|
|
@ -90,10 +120,11 @@ func DrawLine(x int, y int, str string) { |
|
|
|
termbox.Flush() |
|
|
|
termbox.Flush() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func drawScreen(str string) { |
|
|
|
func (self *Screen) draw(str string) { |
|
|
|
for row, line := range strings.Split(str, "\n") { |
|
|
|
for row, line := range strings.Split(str, "\n") { |
|
|
|
DrawLine(0, row, line) |
|
|
|
self.DrawLine(0, row, line) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -101,10 +132,10 @@ func drawScreen(str string) { |
|
|
|
// Return regular expression that matches all possible tags, i.e.
|
|
|
|
// Return regular expression that matches all possible tags, i.e.
|
|
|
|
// </?black>|</?red>| ... |</?white>
|
|
|
|
// </?black>|</?red>| ... |</?white>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func tagsRegexp() *regexp.Regexp { |
|
|
|
func (self *Screen) possible_tags() *regexp.Regexp { |
|
|
|
arr := []string{} |
|
|
|
arr := []string{} |
|
|
|
|
|
|
|
|
|
|
|
for tag, _ := range tags { |
|
|
|
for tag, _ := range self.tags { |
|
|
|
arr = append(arr, `</?` + tag + `>`) |
|
|
|
arr = append(arr, `</?` + tag + `>`) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -114,8 +145,8 @@ func tagsRegexp() *regexp.Regexp { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// Return true if a string looks like a tag.
|
|
|
|
// Return true if a string looks like a tag.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func isTag(str string) (is bool, open bool) { |
|
|
|
func (self *Screen) is_tag(str string) (is bool, open bool) { |
|
|
|
is = (len(str) > 3 && str[0:1] == `<` && str[len(str)-1:] == `>`) |
|
|
|
is = (len(str) > 2 && str[0:1] == `<` && str[len(str)-1:] == `>`) |
|
|
|
open = (is && str[1:2] != `/`) |
|
|
|
open = (is && str[1:2] != `/`) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -123,7 +154,7 @@ func isTag(str string) (is bool, open bool) { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// Extract tag name from the given tag, i.e. `<hello>` => `hello`
|
|
|
|
// Extract tag name from the given tag, i.e. `<hello>` => `hello`
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func tagName(str string) string { |
|
|
|
func (self *Screen) tag_name(str string) string { |
|
|
|
if len(str) < 3 { |
|
|
|
if len(str) < 3 { |
|
|
|
return `` |
|
|
|
return `` |
|
|
|
} else if str[1:2] != `/` { |
|
|
|
} else if str[1:2] != `/` { |
|
|
|