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/screen.go

116 lines
2.7 KiB

12 years ago
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
11 years ago
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
12 years ago
package mop
import (
`github.com/michaeldv/termbox-go`
11 years ago
`strings`
`time`
12 years ago
)
11 years ago
type Screen struct {
11 years ago
width int
height int
cleared bool
markup *Markup
11 years ago
}
//-----------------------------------------------------------------------------
func (self *Screen) Initialize() *Screen {
if err := termbox.Init(); err != nil {
11 years ago
panic(err)
}
self.markup = new(Markup).Initialize()
11 years ago
11 years ago
return self.Resize()
12 years ago
}
//-----------------------------------------------------------------------------
11 years ago
func (self *Screen) Resize() *Screen {
self.width, self.height = termbox.Size()
self.cleared = false
11 years ago
11 years ago
return self
}
//-----------------------------------------------------------------------------
func (self *Screen) Clear() *Screen {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
self.cleared = true
11 years ago
11 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *Screen) Close() *Screen {
11 years ago
termbox.Close()
11 years ago
return self
11 years ago
}
//-----------------------------------------------------------------------------
func (self *Screen) Draw(objects ...interface{}) *Screen {
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())
11 years ago
default:
self.draw(ptr.(string))
}
}
return self
12 years ago
}
//-----------------------------------------------------------------------------
11 years ago
func (self *Screen) DrawTime() {
11 years ago
now := time.Now().Format(`3:04:05pm PST`)
11 years ago
self.DrawLine(0, 0, `<right>` + now + `</right>`)
12 years ago
}
//-----------------------------------------------------------------------------
11 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
}
11 years ago
termbox.Flush()
12 years ago
}
//-----------------------------------------------------------------------------
11 years ago
func (self *Screen) DrawLine(x int, y int, str string) {
11 years ago
start, column := 0, 0
for _, token := range self.markup.Tokenize(str) {
if !self.markup.IsTag(token) {
11 years ago
for i, char := range token {
if !self.markup.RightAligned {
11 years ago
start = x + column
column++
} else {
start = self.width - len(token) + i
12 years ago
}
termbox.SetCell(start, y, char, self.markup.Foreground, self.markup.Background)
12 years ago
}
}
}
termbox.Flush()
12 years ago
}
11 years ago
// private
12 years ago
//-----------------------------------------------------------------------------
11 years ago
func (self *Screen) draw(str string) {
if !self.cleared {
self.Clear()
}
12 years ago
for row, line := range strings.Split(str, "\n") {
11 years ago
self.DrawLine(0, row, line)
}
}