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

119 lines
2.4 KiB

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