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

147 lines
4.1 KiB

5 years ago
// Copyright (c) 2013-2019 by Michael Dvorkin and contributors. 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/nsf/termbox-go`
12 years ago
`strings`
`time`
12 years ago
)
// Screen is thin wrapper aroung Termbox library to provide basic display
// capabilities as requied by Mop.
12 years ago
type Screen struct {
10 years ago
width int // Current number of columns.
height int // Current number of rows.
cleared bool // True after the screens gets cleared.
layout *Layout // Pointer to layout (gets created by screen).
markup *Markup // Pointer to markup processor (gets created by screen).
pausedAt *time.Time // Timestamp of the pause request or nil if none.
12 years ago
}
// Initializes Termbox, creates screen along with layout and markup, and
// calculates current screen dimensions. Once initialized the screen is
// ready for display.
func NewScreen() *Screen {
if err := termbox.Init(); err != nil {
12 years ago
panic(err)
}
screen := &Screen{}
screen.layout = NewLayout()
screen.markup = NewMarkup()
12 years ago
return screen.Resize()
12 years ago
}
// Close gets called upon program termination to close the Termbox.
func (screen *Screen) Close() *Screen {
termbox.Close()
11 years ago
return screen
12 years ago
}
// Resize gets called when the screen is being resized. It recalculates screen
// dimensions and requests to clear the screen on next update.
func (screen *Screen) Resize() *Screen {
screen.width, screen.height = termbox.Size()
screen.cleared = false
return screen
}
// Pause is a toggle function that either creates a timestamp of the pause
// request or resets it to nil.
func (screen *Screen) Pause(pause bool) *Screen {
10 years ago
if pause {
screen.pausedAt = new(time.Time)
*screen.pausedAt = time.Now()
} else {
screen.pausedAt = nil
}
return screen
}
// Clear makes the entire screen blank using default background color.
func (screen *Screen) Clear() *Screen {
12 years ago
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
screen.cleared = true
11 years ago
return screen
12 years ago
}
// ClearLine erases the contents of the line starting from (x,y) coordinate
// till the end of the line.
func (screen *Screen) ClearLine(x int, y int) *Screen {
for i := x; i < screen.width; i++ {
termbox.SetCell(i, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
}
termbox.Flush()
return screen
12 years ago
}
// Draw accepts variable number of arguments and knows how to display the
// market data, stock quotes, current time, and an arbitrary string.
func (screen *Screen) Draw(objects ...interface{}) *Screen {
zonename, _ := time.Now().In(time.Local).Zone()
10 years ago
if screen.pausedAt != nil {
defer screen.DrawLine(0, 0, `<right><r>`+screen.pausedAt.Format(`3:04:05pm ` + zonename)+`</r></right>`)
10 years ago
}
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()))
case time.Time:
timestamp := ptr.(time.Time).Format(`3:04:05pm ` + zonename)
10 years ago
screen.DrawLine(0, 0, `<right>`+timestamp+`</right>`)
11 years ago
default:
screen.draw(ptr.(string))
}
}
return screen
12 years ago
}
// DrawLine takes the incoming string, tokenizes it to extract markup
// elements, and displays it all starting at (x,y) location.
func (screen *Screen) DrawLine(x int, y int, str string) {
11 years ago
start, column := 0, 0
for _, token := range screen.markup.Tokenize(str) {
// First check if it's a tag. Tags are eaten up and not displayed.
if screen.markup.IsTag(token) {
continue
}
// Here comes the actual text: display it one character at a time.
for i, char := range token {
if !screen.markup.RightAligned {
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
}
// Underlying workhorse function that takes multiline string, splits it into
// lines, and displays them row by row.
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)
}
}