parent
713b87609d
commit
8f727f1204
@ -0,0 +1,64 @@ |
||||
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
|
||||
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
||||
package mop |
||||
|
||||
import ( |
||||
"github.com/nsf/termbox-go" |
||||
) |
||||
|
||||
// const (
|
||||
// add_prompt = "Add tickers: "
|
||||
// remove_prompt = "Remove tickers: "
|
||||
// )
|
||||
|
||||
// const prompts = map[rune]string{'+': `Add tickers: `, '-': `Remove tickers: `}
|
||||
|
||||
type LineEditor struct { |
||||
command rune |
||||
prompt string |
||||
cursor int |
||||
input string |
||||
} |
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
func (self *LineEditor) Prompt(command rune) { |
||||
prompts := map[rune]string{'+': `Add tickers: `, '-': `Remove tickers: `} |
||||
|
||||
self.command = command |
||||
switch self.command { |
||||
case '+', '-': |
||||
self.prompt = prompts[self.command] |
||||
// if self.command == '+' {
|
||||
// self.prompt = add_prompt
|
||||
// } else {
|
||||
// self.prompt = remove_prompt
|
||||
// }
|
||||
DrawLine(0, 3, "<white>"+self.prompt+"</white>") |
||||
termbox.SetCursor(len(self.prompt), 3) |
||||
termbox.Flush() |
||||
} |
||||
} |
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
func (self *LineEditor) Handle(ev termbox.Event) bool { |
||||
switch ev.Key { |
||||
case termbox.KeyEsc: |
||||
ClearLine(0, 3) |
||||
termbox.HideCursor() |
||||
termbox.Flush() |
||||
return true |
||||
case termbox.KeyEnter: |
||||
ClearLine(0, 3) |
||||
termbox.HideCursor() |
||||
termbox.Flush() |
||||
return true |
||||
default: |
||||
if ev.Ch != 0 { |
||||
self.input += string(ev.Ch) |
||||
DrawLine(len(self.prompt), 3, self.input) |
||||
termbox.SetCursor(len(self.prompt)+len(self.input), 3) |
||||
termbox.Flush() |
||||
} |
||||
} |
||||
return false |
||||
} |
@ -1,56 +1,57 @@ |
||||
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
|
||||
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
||||
package mop |
||||
|
||||
import ( |
||||
"strings" |
||||
"os/user" |
||||
"io/ioutil" |
||||
"encoding/json" |
||||
"encoding/json" |
||||
"io/ioutil" |
||||
"os/user" |
||||
"strings" |
||||
) |
||||
|
||||
const rcfile = "/.moprc" |
||||
|
||||
type Profile struct { |
||||
MarketRefreshRate int |
||||
QuotesRefreshRate int |
||||
Tickers []string |
||||
SortBy string |
||||
SortOrder string |
||||
MarketRefreshRate int |
||||
QuotesRefreshRate int |
||||
Tickers []string |
||||
SortBy string |
||||
SortOrder string |
||||
} |
||||
|
||||
var profile Profile |
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
func LoadProfile() string { |
||||
data, err := ioutil.ReadFile(defaultProfile()) |
||||
if err != nil { |
||||
// Set default values.
|
||||
profile.MarketRefreshRate = 12 |
||||
profile.QuotesRefreshRate = 5 |
||||
profile.Tickers = []string{ "AAPL", "C", "GOOG", "IBM", "KO", "ORCL", "V" } |
||||
profile.SortBy = "Ticker" |
||||
profile.SortOrder = "Desc" |
||||
profile.Save() |
||||
} else { |
||||
json.Unmarshal(data, &profile) |
||||
} |
||||
data, err := ioutil.ReadFile(defaultProfile()) |
||||
if err != nil { |
||||
// Set default values.
|
||||
profile.MarketRefreshRate = 12 |
||||
profile.QuotesRefreshRate = 5 |
||||
profile.Tickers = []string{"AAPL", "C", "GOOG", "IBM", "KO", "ORCL", "V"} |
||||
profile.SortBy = "Ticker" |
||||
profile.SortOrder = "Desc" |
||||
profile.Save() |
||||
} else { |
||||
json.Unmarshal(data, &profile) |
||||
} |
||||
return strings.Join(profile.Tickers, "+") |
||||
} |
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
func (profile *Profile) Save() error { |
||||
if data, err := json.Marshal(profile); err != nil { |
||||
return err |
||||
} else { |
||||
return ioutil.WriteFile(defaultProfile(), data, 0644) |
||||
} |
||||
if data, err := json.Marshal(profile); err != nil { |
||||
return err |
||||
} else { |
||||
return ioutil.WriteFile(defaultProfile(), data, 0644) |
||||
} |
||||
} |
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
func defaultProfile() string { |
||||
usr, err := user.Current() |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return usr.HomeDir + rcfile |
||||
usr, err := user.Current() |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return usr.HomeDir + rcfile |
||||
} |
||||
|
Loading…
Reference in new issue