You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cointop/cointop/cointop.go

149 lines
3.7 KiB
Go

package cointop
import (
"log"
"os"
"sync"
"time"
"github.com/miguelmota/cointop/pkg/api"
types "github.com/miguelmota/cointop/pkg/api/types"
6 years ago
"github.com/miguelmota/cointop/pkg/cache"
"github.com/miguelmota/cointop/pkg/fcache"
6 years ago
"github.com/miguelmota/cointop/pkg/gocui"
"github.com/miguelmota/cointop/pkg/table"
"github.com/miguelmota/cointop/pkg/termui"
)
// Cointop cointop
type Cointop struct {
g *gocui.Gui
marketbarviewname string
marketbarview *gocui.View
chartview *gocui.View
chartviewname string
chartpoints [][]termui.Cell
headersview *gocui.View
headerviewname string
tableview *gocui.View
tableviewname string
table *table.Table
maxtablewidth int
statusbarview *gocui.View
statusbarviewname string
sortdesc bool
sortby string
api api.Interface
allcoins []*coin
coins []*coin
allcoinsmap map[string]*coin
page int
perpage int
refreshmux sync.Mutex
refreshticker *time.Ticker
forcerefresh chan bool
selectedcoin *coin
actionsmap map[string]bool
shortcutkeys map[string]string
config config // toml config
searchfield *gocui.View
searchfieldviewname string
searchfieldvisible bool
favorites map[string]bool
filterByFavorites bool
savemux sync.Mutex
cache *cache.Cache
debug bool
helpview *gocui.View
helpviewname string
helpvisible bool
}
6 years ago
// Instance running cointop instance
var Instance *Cointop
// Run runs cointop
func Run() {
var debug bool
if os.Getenv("DEBUG") != "" {
debug = true
}
ct := Cointop{
api: api.NewCMC(),
refreshticker: time.NewTicker(1 * time.Minute),
sortby: "rank",
sortdesc: false,
page: 0,
perpage: 100,
forcerefresh: make(chan bool),
maxtablewidth: 175,
actionsmap: actionsMap(),
shortcutkeys: defaultShortcuts(),
favorites: map[string]bool{},
cache: cache.New(1*time.Minute, 2*time.Minute),
debug: debug,
marketbarviewname: "market",
chartviewname: "chart",
headerviewname: "header",
tableviewname: "table",
statusbarviewname: "statusbar",
searchfieldviewname: "searchfield",
helpviewname: "help",
}
6 years ago
Instance = &ct
err := ct.setupConfig()
if err != nil {
log.Fatal(err)
}
allcoinsmap := map[string]types.Coin{}
fcache.Get("allcoinsmap", &allcoinsmap)
ct.cache.Set("allcoinsmap", allcoinsmap, 10*time.Second)
var globaldata []float64
fcache.Get("globaldata", &globaldata)
ct.cache.Set("globaldata", globaldata, 10*time.Second)
var market types.GlobalMarketData
fcache.Get("market", &market)
ct.cache.Set("market", market, 10*time.Second)
g, err := gocui.NewGui(gocui.Output256)
if err != nil {
log.Fatalf("new gocui: %v", err)
}
ct.g = g
defer g.Close()
6 years ago
g.InputEsc = true
g.BgColor = gocui.ColorBlack
g.FgColor = gocui.ColorWhite
g.Mouse = true
g.Highlight = true
g.SetManagerFunc(ct.layout)
if err := ct.keybindings(g); err != nil {
log.Fatalf("keybindings: %v", err)
}
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Fatalf("main loop: %v", err)
}
}
func (ct *Cointop) quit() error {
if ct.helpvisible || ct.searchfieldvisible {
return nil
}
return ct.forceQuit()
}
func (ct *Cointop) forceQuit() error {
return gocui.ErrQuit
}
6 years ago
// Exit safely exit application
func Exit() {
if Instance != nil {
Instance.g.Close()
}
}