Refactored screen

master
Michael Dvorkin 11 years ago
parent a63708a82b
commit 0a9ae0fea8
  1. 4
      lib/format.go
  2. 28
      lib/line_editor.go
  3. 109
      lib/screen.go
  4. 39
      mop.go

@ -86,7 +86,7 @@ func FormatQuotes(quotes Quotes) string {
} }
func header() string { func header() string {
str := fmt.Sprintf(`%-7s `, `Ticker`) str := fmt.Sprintf(`<u>%-7s `, `Ticker`)
str += fmt.Sprintf(`%9s `, `Last`) str += fmt.Sprintf(`%9s `, `Last`)
str += fmt.Sprintf(`%9s `, `Change`) str += fmt.Sprintf(`%9s `, `Change`)
str += fmt.Sprintf(`%9s `, `%Change`) str += fmt.Sprintf(`%9s `, `%Change`)
@ -100,7 +100,7 @@ func header() string {
str += fmt.Sprintf(`%9s `, `P/E`) str += fmt.Sprintf(`%9s `, `P/E`)
str += fmt.Sprintf(`%9s `, `Dividend`) str += fmt.Sprintf(`%9s `, `Dividend`)
str += fmt.Sprintf(`%9s `, `Yield`) str += fmt.Sprintf(`%9s `, `Yield`)
str += fmt.Sprintf(`%10s`, `MktCap`) str += fmt.Sprintf(`%10s</u>`, `MktCap`)
return str return str
} }

@ -13,18 +13,26 @@ type LineEditor struct {
prompt string prompt string
cursor int cursor int
input string input string
screen *Screen
profile *Profile profile *Profile
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) Prompt(command rune, profile *Profile) { func (self *LineEditor) Initialize(screen *Screen, profile *Profile) *LineEditor {
self.screen = screen
self.profile = profile
return self
}
//-----------------------------------------------------------------------------
func (self *LineEditor) Prompt(command rune) {
prompts := map[rune]string{'+': `Add tickers: `, '-': `Remove tickers: `} prompts := map[rune]string{'+': `Add tickers: `, '-': `Remove tickers: `}
if prompt, ok := prompts[command]; ok { if prompt, ok := prompts[command]; ok {
self.prompt = prompt self.prompt = prompt
self.command = command self.command = command
self.profile = profile
DrawLine(0, 3, `<white>` + self.prompt + `</white>`) self.screen.DrawLine(0, 3, `<white>` + self.prompt + `</white>`)
termbox.SetCursor(len(self.prompt), 3) termbox.SetCursor(len(self.prompt), 3)
termbox.Flush() termbox.Flush()
} }
@ -67,7 +75,7 @@ func (self *LineEditor) Handle(ev termbox.Event) bool {
self.insert_character(ev.Ch) self.insert_character(ev.Ch)
} }
} }
//DrawLine(20,20, fmt.Sprintf(`cursor: %02d [%s] %08d`, self.cursor, self.input, ev.Ch)) //self.screen.DrawLine(20,20, fmt.Sprintf(`cursor: %02d [%s] %08d`, self.cursor, self.input, ev.Ch))
return false return false
} }
@ -81,7 +89,7 @@ func (self *LineEditor) delete_previous_character() {
// Remove last input character. // Remove last input character.
self.input = self.input[ : len(self.input)-1] self.input = self.input[ : len(self.input)-1]
} }
DrawLine(len(self.prompt), 3, self.input + ` `) // Erase last character. self.screen.DrawLine(len(self.prompt), 3, self.input + ` `) // Erase last character.
self.move_left() self.move_left()
} }
} }
@ -95,7 +103,7 @@ func (self *LineEditor) insert_character(ch rune) {
// Append the character to the end of the input string. // Append the character to the end of the input string.
self.input += string(ch) self.input += string(ch)
} }
DrawLine(len(self.prompt), 3, self.input) self.screen.DrawLine(len(self.prompt), 3, self.input)
self.move_right() self.move_right()
} }
@ -129,7 +137,7 @@ func (self *LineEditor) jump_to_end() {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *LineEditor) done() { func (self *LineEditor) done() {
ClearLine(0, 3) self.screen.ClearLine(0, 3)
termbox.HideCursor() termbox.HideCursor()
} }
@ -140,7 +148,7 @@ func (self *LineEditor) execute() {
tickers := self.tokenize() tickers := self.tokenize()
if len(tickers) > 0 { if len(tickers) > 0 {
self.profile.AddTickers(tickers) self.profile.AddTickers(tickers)
DrawQuotes(self.profile.Quotes()) self.screen.DrawQuotes(self.profile.Quotes())
} }
case '-': case '-':
tickers := self.tokenize() tickers := self.tokenize()
@ -149,9 +157,9 @@ func (self *LineEditor) execute() {
self.profile.RemoveTickers(tickers) self.profile.RemoveTickers(tickers)
after := len(self.profile.Tickers) after := len(self.profile.Tickers)
if after < before { if after < before {
DrawQuotes(self.profile.Quotes()) self.screen.DrawQuotes(self.profile.Quotes())
for i := before; i > after; i-- { for i := before; i > after; i-- {
ClearLine(0, i + 4) self.screen.ClearLine(0, i + 4)
} }
} }
} }

@ -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] != `/` {

@ -9,15 +9,7 @@ import (
) )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func initTermbox() { func mainLoop(screen *mop.Screen, profile *mop.Profile) {
err := termbox.Init()
if err != nil {
panic(err)
}
}
//-----------------------------------------------------------------------------
func mainLoop(profile *mop.Profile) {
var line_editor *mop.LineEditor var line_editor *mop.LineEditor
keyboard_queue := make(chan termbox.Event) keyboard_queue := make(chan termbox.Event)
timestamp_queue := time.NewTicker(1 * time.Second) timestamp_queue := time.NewTicker(1 * time.Second)
@ -30,9 +22,9 @@ func mainLoop(profile *mop.Profile) {
} }
}() }()
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) screen.Clear()
mop.DrawMarket() screen.DrawMarket()
mop.DrawQuotes(profile.Quotes()) screen.DrawQuotes(profile.Quotes())
loop: loop:
for { for {
select { select {
@ -43,8 +35,8 @@ loop:
if event.Key == termbox.KeyEsc { if event.Key == termbox.KeyEsc {
break loop break loop
} else if event.Ch == '+' || event.Ch == '-' { } else if event.Ch == '+' || event.Ch == '-' {
line_editor = new(mop.LineEditor) line_editor = new(mop.LineEditor).Initialize(screen, profile)
line_editor.Prompt(event.Ch, profile) line_editor.Prompt(event.Ch)
} }
} else { } else {
done := line_editor.Handle(event) done := line_editor.Handle(event)
@ -53,29 +45,28 @@ loop:
} }
} }
case termbox.EventResize: case termbox.EventResize:
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) screen.Resize().Clear()
mop.DrawMarket() screen.DrawMarket()
mop.DrawQuotes(profile.Quotes()) screen.DrawQuotes(profile.Quotes())
} }
case <-timestamp_queue.C: case <-timestamp_queue.C:
mop.DrawTime() screen.DrawTime()
case <-quotes_queue.C: case <-quotes_queue.C:
mop.DrawQuotes(profile.Quotes()) screen.DrawQuotes(profile.Quotes())
case <-market_queue.C: case <-market_queue.C:
mop.DrawMarket() screen.DrawMarket()
} }
} }
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func main() { func main() {
screen := new(mop.Screen).Initialize()
initTermbox() defer screen.Close()
defer termbox.Close()
profile := new(mop.Profile).Initialize() profile := new(mop.Profile).Initialize()
mainLoop(profile) mainLoop(screen, profile)
} }

Loading…
Cancel
Save