Started making golint happy...

master
Michael Dvorkin 11 years ago
parent 93db48015b
commit f1548e6bac
  1. 66
      cmd/mop.go
  2. 94
      column_editor.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 main package main
@ -27,19 +27,19 @@ Enter comma-delimited list of stock tickers when prompted.
` `
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func main_loop(screen *mop.Screen, profile *mop.Profile) { func mainLoop(screen *mop.Screen, profile *mop.Profile) {
var line_editor *mop.LineEditor var lineEditor *mop.LineEditor
var column_editor *mop.ColumnEditor var columnEditor *mop.ColumnEditor
keyboard_queue := make(chan termbox.Event) keyboardQueue := make(chan termbox.Event)
timestamp_queue := time.NewTicker(1 * time.Second) timestampQueue := time.NewTicker(1 * time.Second)
quotes_queue := time.NewTicker(5 * time.Second) quotesQueue := time.NewTicker(5 * time.Second)
market_queue := time.NewTicker(12 * time.Second) marketQueue := time.NewTicker(12 * time.Second)
showing_help := false showingHelp := false
go func() { go func() {
for { for {
keyboard_queue <- termbox.PollEvent() keyboardQueue <- termbox.PollEvent()
} }
}() }()
@ -50,58 +50,58 @@ func main_loop(screen *mop.Screen, profile *mop.Profile) {
loop: loop:
for { for {
select { select {
case event := <-keyboard_queue: case event := <-keyboardQueue:
switch event.Type { switch event.Type {
case termbox.EventKey: case termbox.EventKey:
if line_editor == nil && column_editor == nil && !showing_help { if lineEditor == nil && columnEditor == nil && !showingHelp {
if event.Key == termbox.KeyEsc { if event.Key == termbox.KeyEsc {
break loop break loop
} else if event.Ch == '+' || event.Ch == '-' { } else if event.Ch == '+' || event.Ch == '-' {
line_editor = new(mop.LineEditor).Initialize(screen, quotes) lineEditor = new(mop.LineEditor).Initialize(screen, quotes)
line_editor.Prompt(event.Ch) lineEditor.Prompt(event.Ch)
} else if event.Ch == 'o' || event.Ch == 'O' { } else if event.Ch == 'o' || event.Ch == 'O' {
column_editor = new(mop.ColumnEditor).Initialize(screen, quotes) columnEditor = new(mop.ColumnEditor).Initialize(screen, quotes)
} else if event.Ch == 'g' || event.Ch == 'G' { } else if event.Ch == 'g' || event.Ch == 'G' {
if profile.Regroup() == nil { if profile.Regroup() == nil {
screen.Draw(quotes) screen.Draw(quotes)
} }
} else if event.Ch == '?' || event.Ch == 'h' || event.Ch == 'H' { } else if event.Ch == '?' || event.Ch == 'h' || event.Ch == 'H' {
showing_help = true showingHelp = true
screen.Clear().Draw(help) screen.Clear().Draw(help)
} }
} else if line_editor != nil { } else if lineEditor != nil {
if done := line_editor.Handle(event); done { if done := lineEditor.Handle(event); done {
line_editor = nil lineEditor = nil
} }
} else if column_editor != nil { } else if columnEditor != nil {
if done := column_editor.Handle(event); done { if done := columnEditor.Handle(event); done {
column_editor = nil columnEditor = nil
} }
} else if showing_help { } else if showingHelp {
showing_help = false showingHelp = false
screen.Clear().Draw(market, quotes) screen.Clear().Draw(market, quotes)
} }
case termbox.EventResize: case termbox.EventResize:
screen.Resize() screen.Resize()
if !showing_help { if !showingHelp {
screen.Draw(market, quotes) screen.Draw(market, quotes)
} else { } else {
screen.Draw(help) screen.Draw(help)
} }
} }
case <-timestamp_queue.C: case <-timestampQueue.C:
if !showing_help { if !showingHelp {
screen.DrawTime() screen.DrawTime()
} }
case <-quotes_queue.C: case <-quotesQueue.C:
if !showing_help { if !showingHelp {
screen.Draw(quotes) screen.Draw(quotes)
} }
case <-market_queue.C: case <-marketQueue.C:
if !showing_help { if !showingHelp {
screen.Draw(market) screen.Draw(market)
} }
} }
@ -114,5 +114,5 @@ func main() {
defer screen.Close() defer screen.Close()
profile := new(mop.Profile).Initialize() profile := new(mop.Profile).Initialize()
main_loop(screen, profile) mainLoop(screen, profile)
} }

@ -1,93 +1,97 @@
// 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
import ( import `github.com/michaeldv/termbox-go`
`github.com/michaeldv/termbox-go`
)
// ColumnEditor handles column sort order. When activated it highlights
// current column name in the header, then waits for arrow keys (choose
// another column), Enter (reverse sort order), or Esc (exit).
type ColumnEditor struct { type ColumnEditor struct {
screen *Screen screen *Screen // Pointer to Screen so we could use screen.Draw().
quotes *Quotes quotes *Quotes // Pointer to Quotes to redraw them when the sort order changes.
profile *Profile profile *Profile // Pointer to Profile where we save newly selected sort order.
} }
//----------------------------------------------------------------------------- // Initialize sets internal variables and highlights current column name
func (self *ColumnEditor) Initialize(screen *Screen, quotes *Quotes) *ColumnEditor { // (as stored in Profile).
self.screen = screen func (editor *ColumnEditor) Initialize(screen *Screen, quotes *Quotes) *ColumnEditor {
self.quotes = quotes editor.screen = screen
self.profile = quotes.profile editor.quotes = quotes
editor.profile = quotes.profile
editor.selectCurrentColumn()
self.select_current_column() return editor
return self
} }
//----------------------------------------------------------------------------- // Handle takes over the keyboard events and dispatches them to appropriate
func (self *ColumnEditor) Handle(ev termbox.Event) bool { // column editor handlers. It returns true when user presses Esc.
defer self.redraw_header() func (editor *ColumnEditor) Handle(event termbox.Event) bool {
defer editor.redrawHeader()
switch ev.Key { switch event.Key {
case termbox.KeyEsc: case termbox.KeyEsc:
return self.done() return editor.done()
case termbox.KeyEnter: case termbox.KeyEnter:
self.execute() editor.execute()
case termbox.KeyArrowLeft: case termbox.KeyArrowLeft:
self.select_left_column() editor.selectLeftColumn()
case termbox.KeyArrowRight: case termbox.KeyArrowRight:
self.select_right_column() editor.selectRightColumn()
} }
return false return false
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) select_current_column() *ColumnEditor { func (editor *ColumnEditor) selectCurrentColumn() *ColumnEditor {
self.profile.selected_column = self.profile.SortColumn editor.profile.selected_column = editor.profile.SortColumn
self.redraw_header() editor.redrawHeader()
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) select_left_column() *ColumnEditor { func (editor *ColumnEditor) selectLeftColumn() *ColumnEditor {
self.profile.selected_column-- editor.profile.selected_column--
if self.profile.selected_column < 0 { if editor.profile.selected_column < 0 {
self.profile.selected_column = TotalColumns - 1 editor.profile.selected_column = TotalColumns - 1
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) select_right_column() *ColumnEditor { func (editor *ColumnEditor) selectRightColumn() *ColumnEditor {
self.profile.selected_column++ editor.profile.selected_column++
if self.profile.selected_column > TotalColumns - 1 { if editor.profile.selected_column > TotalColumns - 1 {
self.profile.selected_column = 0 editor.profile.selected_column = 0
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) execute() *ColumnEditor { func (editor *ColumnEditor) execute() *ColumnEditor {
if self.profile.Reorder() == nil { if editor.profile.Reorder() == nil {
self.screen.Draw(self.quotes) editor.screen.Draw(editor.quotes)
} }
return self return editor
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) done() bool { func (editor *ColumnEditor) done() bool {
self.profile.selected_column = -1 editor.profile.selected_column = -1
return true return true
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *ColumnEditor) redraw_header() { func (editor *ColumnEditor) redrawHeader() {
self.screen.DrawLine(0, 4, self.screen.layout.Header(self.profile)) editor.screen.DrawLine(0, 4, editor.screen.layout.Header(editor.profile))
termbox.Flush() termbox.Flush()
} }

Loading…
Cancel
Save