Initial golint sweep for screen.go

master
Michael Dvorkin 11 years ago
parent 2e05c91d29
commit 89347a040e
  1. 113
      screen.go

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

Loading…
Cancel
Save