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/cmd/mop.go

119 lines
3.1 KiB

// 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
package main
import (
`github.com/michaeldv/mop`
`github.com/michaeldv/termbox-go`
`time`
)
11 years ago
const help = `Mop v0.1.0 -- Copyright (c) 2013 Michael Dvorkin. All Rights Reserved.
11 years ago
NO WARRANTIES OF ANY KIND WHATSOEVER. SEE THE LICENSE FILE FOR DETAILS.
11 years ago
<u>Command</u> <u>Description </u>
+ Add stocks to the list.
- Remove stocks from the list.
11 years ago
o Change column sort order.
11 years ago
g Group stocks by advancing/declining issues.
? Display this help screen.
esc Quit mop.
11 years ago
Enter comma-delimited list of stock tickers when prompted.
11 years ago
<r> Press any key to continue </r>
`
12 years ago
//-----------------------------------------------------------------------------
func mainLoop(screen *mop.Screen, profile *mop.Profile) {
var lineEditor *mop.LineEditor
var columnEditor *mop.ColumnEditor
11 years ago
keyboardQueue := make(chan termbox.Event)
timestampQueue := time.NewTicker(1 * time.Second)
quotesQueue := time.NewTicker(5 * time.Second)
marketQueue := time.NewTicker(12 * time.Second)
showingHelp := false
go func() {
for {
keyboardQueue <- termbox.PollEvent()
}
}()
11 years ago
market := new(mop.Market).Initialize()
12 years ago
quotes := new(mop.Quotes).Initialize(market, profile)
screen.Draw(market, quotes)
12 years ago
loop:
for {
select {
case event := <-keyboardQueue:
switch event.Type {
case termbox.EventKey:
if lineEditor == nil && columnEditor == nil && !showingHelp {
if event.Key == termbox.KeyEsc {
break loop
} else if event.Ch == '+' || event.Ch == '-' {
lineEditor = new(mop.LineEditor).Initialize(screen, quotes)
lineEditor.Prompt(event.Ch)
} else if event.Ch == 'o' || event.Ch == 'O' {
columnEditor = new(mop.ColumnEditor).Initialize(screen, quotes)
} else if event.Ch == 'g' || event.Ch == 'G' {
if profile.Regroup() == nil {
screen.Draw(quotes)
}
11 years ago
} else if event.Ch == '?' || event.Ch == 'h' || event.Ch == 'H' {
showingHelp = true
11 years ago
screen.Clear().Draw(help)
}
} else if lineEditor != nil {
if done := lineEditor.Handle(event); done {
lineEditor = nil
}
} else if columnEditor != nil {
if done := columnEditor.Handle(event); done {
columnEditor = nil
}
} else if showingHelp {
showingHelp = false
11 years ago
screen.Clear().Draw(market, quotes)
}
case termbox.EventResize:
screen.Resize()
if !showingHelp {
11 years ago
screen.Draw(market, quotes)
} else {
screen.Draw(help)
}
}
case <-timestampQueue.C:
if !showingHelp {
screen.Draw(time.Now())
11 years ago
}
case <-quotesQueue.C:
if !showingHelp {
11 years ago
screen.Draw(quotes)
}
case <-marketQueue.C:
if !showingHelp {
11 years ago
screen.Draw(market)
}
}
}
}
//-----------------------------------------------------------------------------
func main() {
12 years ago
screen := new(mop.Screen).Initialize()
defer screen.Close()
profile := new(mop.Profile).Initialize()
mainLoop(screen, profile)
}