You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mop/lib/screen.go

182 lines
4.8 KiB

12 years ago
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
package mop
import (
12 years ago
`github.com/michaeldv/just`
`github.com/nsf/termbox-go`
`regexp`
`strings`
`time`
12 years ago
)
12 years ago
type Screen struct {
width int
height int
cleared bool
12 years ago
tags map[string]termbox.Attribute
}
//-----------------------------------------------------------------------------
func (self *Screen) Initialize() *Screen {
err := termbox.Init()
if err != nil {
panic(err)
}
self.Resize()
self.tags = make(map[string]termbox.Attribute)
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
12 years ago
}
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) Resize() *Screen {
self.width, self.height = termbox.Size()
self.cleared = false
12 years ago
return self
}
//-----------------------------------------------------------------------------
func (self *Screen) Clear() *Screen {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
self.cleared = true
12 years ago
return self
}
//-----------------------------------------------------------------------------
func (self *Screen) Close() {
termbox.Close()
}
//-----------------------------------------------------------------------------
func (self *Screen) Draw(objects ...interface{}) {
for _, ptr := range objects {
switch ptr.(type) {
case *Market:
object := ptr.(*Market)
self.draw(object.Fetch().Format())
case *Quotes:
object := ptr.(*Quotes)
self.draw(object.Fetch().Format())
}
}
12 years ago
}
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) DrawTime() {
12 years ago
now := time.Now().Format(`3:04:05pm PST`)
12 years ago
self.DrawLine(0, 0, `<right>` + now + `</right>`)
12 years ago
}
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) ClearLine(x int, y int) {
for i := x; i < self.width; i++ {
termbox.SetCell(i, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
12 years ago
}
12 years ago
termbox.Flush()
12 years ago
}
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) DrawLine(x int, y int, str string) {
column, right := 0, false
12 years ago
foreground, background := termbox.ColorDefault, termbox.ColorDefault
12 years ago
for _, token := range just.Split(self.possible_tags(), str) {
if tag, open := self.is_tag(token); tag {
key := self.tag_name(token)
if value, ok := self.tags[key]; ok {
12 years ago
token = ``
switch key {
12 years ago
case `right`:
right = open
default:
if open {
if value >= termbox.AttrBold {
foreground |= value
} else {
foreground = value
}
} else {
if value >= termbox.AttrBold {
foreground &= ^value
} else {
foreground = termbox.ColorDefault
}
}
12 years ago
}
}
}
for i, char := range token {
if !right {
termbox.SetCell(x+column, y, char, foreground, background)
} else {
12 years ago
termbox.SetCell(self.width-len(token)+i, y, char, foreground, background)
}
column++
12 years ago
}
}
termbox.Flush()
12 years ago
}
12 years ago
// private
12 years ago
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) draw(str string) {
if !self.cleared {
self.Clear()
}
12 years ago
for row, line := range strings.Split(str, "\n") {
12 years ago
self.DrawLine(0, row, line)
}
}
//
// Return regular expression that matches all possible tags, i.e.
// </?black>|</?red>| ... |</?white>
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) possible_tags() *regexp.Regexp {
arr := []string{}
12 years ago
for tag, _ := range self.tags {
12 years ago
arr = append(arr, `</?` + tag + `>`)
}
12 years ago
return regexp.MustCompile(strings.Join(arr, `|`))
}
//
// Return true if a string looks like a tag.
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) is_tag(str string) (is bool, open bool) {
is = (len(str) > 2 && str[0:1] == `<` && str[len(str)-1:] == `>`)
12 years ago
open = (is && str[1:2] != `/`)
return
}
//
12 years ago
// Extract tag name from the given tag, i.e. `<hello>` => `hello`
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) tag_name(str string) string {
if len(str) < 3 {
12 years ago
return ``
} else if str[1:2] != `/` {
return str[1 : len(str)-1]
} else {
return str[2 : len(str)-1]
12 years ago
}
}