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

118 lines
2.8 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`
12 years ago
`strings`
`time`
12 years ago
)
12 years ago
type Screen struct {
11 years ago
width int
height int
cleared bool
layout *Layout
markup *Markup
12 years ago
}
//-----------------------------------------------------------------------------
func (self *Screen) Initialize() *Screen {
if err := termbox.Init(); err != nil {
12 years ago
panic(err)
}
self.layout = new(Layout).Initialize()
self.markup = new(Markup).Initialize()
12 years ago
11 years ago
return self.Resize()
12 years ago
}
//-----------------------------------------------------------------------------
12 years ago
func (self *Screen) Resize() *Screen {
self.width, self.height = termbox.Size()
self.cleared = false
11 years ago
12 years ago
return self
}
//-----------------------------------------------------------------------------
func (self *Screen) Clear() *Screen {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
self.cleared = true
11 years ago
12 years ago
return self
}
//-----------------------------------------------------------------------------
11 years ago
func (self *Screen) Close() *Screen {
12 years ago
termbox.Close()
11 years ago
return self
12 years ago
}
//-----------------------------------------------------------------------------
func (self *Screen) Draw(objects ...interface{}) *Screen {
for _, ptr := range objects {
switch ptr.(type) {
case *Market:
object := ptr.(*Market)
self.draw(self.layout.Market(object.Fetch()))
case *Quotes:
object := ptr.(*Quotes)
self.draw(self.layout.Quotes(object.Fetch()))
11 years ago
default:
self.draw(ptr.(string))
}
}
return self
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) {
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
}
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)
}
}