mirror of
https://github.com/miguelmota/cointop
synced 2024-11-05 00:00:14 +00:00
load from file cache
This commit is contained in:
parent
247d4f1232
commit
9d4080f44b
@ -7,7 +7,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/miguelmota/cointop/pkg/api"
|
||||
types "github.com/miguelmota/cointop/pkg/api/types"
|
||||
"github.com/miguelmota/cointop/pkg/cache"
|
||||
"github.com/miguelmota/cointop/pkg/fcache"
|
||||
"github.com/miguelmota/cointop/pkg/gocui"
|
||||
"github.com/miguelmota/cointop/pkg/table"
|
||||
"github.com/miguelmota/cointop/pkg/termui"
|
||||
@ -15,38 +17,45 @@ import (
|
||||
|
||||
// Cointop cointop
|
||||
type Cointop struct {
|
||||
g *gocui.Gui
|
||||
marketview *gocui.View
|
||||
chartview *gocui.View
|
||||
chartpoints [][]termui.Cell
|
||||
headersview *gocui.View
|
||||
tableview *gocui.View
|
||||
table *table.Table
|
||||
statusbarview *gocui.View
|
||||
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
|
||||
maxtablewidth int
|
||||
actionsmap map[string]bool
|
||||
shortcutkeys map[string]string
|
||||
config config // toml config
|
||||
searchfield *gocui.View
|
||||
favorites map[string]bool
|
||||
filterByFavorites bool
|
||||
savemux sync.Mutex
|
||||
cache *cache.Cache
|
||||
debug bool
|
||||
helpview *gocui.View
|
||||
helpvisible bool
|
||||
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
|
||||
favorites map[string]bool
|
||||
filterByFavorites bool
|
||||
savemux sync.Mutex
|
||||
cache *cache.Cache
|
||||
debug bool
|
||||
helpview *gocui.View
|
||||
helpviewname string
|
||||
helpvisible bool
|
||||
}
|
||||
|
||||
// Run runs cointop
|
||||
@ -56,24 +65,44 @@ func Run() {
|
||||
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,
|
||||
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",
|
||||
}
|
||||
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)
|
||||
@ -89,24 +118,6 @@ func Run() {
|
||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||
log.Fatalf("main loop: %v", err)
|
||||
}
|
||||
/*
|
||||
ifc, ok, _ := ct.readHardCache(&data, filename)
|
||||
if ok {
|
||||
// hard cache hit
|
||||
if ifc != nil {
|
||||
ct.debuglog("hard cache hit")
|
||||
}
|
||||
}
|
||||
*/
|
||||
/*
|
||||
ifc, ok, _ := ct.readHardCache(&allcoinsmap, "allcoinsmap")
|
||||
if ok {
|
||||
// hard cache hit
|
||||
if ifc != nil {
|
||||
ct.debuglog("hard cache hit")
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func (ct *Cointop) quit() error {
|
||||
|
@ -69,8 +69,8 @@ func (ct *Cointop) showHelp() error {
|
||||
|
||||
func (ct *Cointop) hideHelp() error {
|
||||
ct.helpvisible = false
|
||||
ct.setViewOnBottom("help")
|
||||
ct.setActiveView("table")
|
||||
ct.setViewOnBottom(ct.helpviewname)
|
||||
ct.setActiveView(ct.tableviewname)
|
||||
ct.update(func() {
|
||||
ct.helpview.Clear()
|
||||
ct.helpview.Frame = false
|
||||
|
@ -6,34 +6,49 @@ import (
|
||||
"github.com/miguelmota/cointop/pkg/gocui"
|
||||
)
|
||||
|
||||
// layout sets initial layout
|
||||
func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
maxX, maxY := ct.size()
|
||||
chartHeight := 10
|
||||
topOffset := 0
|
||||
|
||||
if v, err := g.SetView("market", 0, topOffset, maxX, 2); err != nil {
|
||||
if v, err := g.SetView(ct.marketbarviewname, 0, topOffset, maxX, 2); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
ct.marketview = v
|
||||
ct.marketview.Frame = false
|
||||
ct.marketview.BgColor = gocui.ColorBlack
|
||||
ct.marketview.FgColor = gocui.ColorWhite
|
||||
go ct.updateMarketbar()
|
||||
ct.marketbarview = v
|
||||
ct.marketbarview.Frame = false
|
||||
ct.marketbarview.BgColor = gocui.ColorBlack
|
||||
ct.marketbarview.FgColor = gocui.ColorWhite
|
||||
go func() {
|
||||
ct.updateMarketbar()
|
||||
_, found := ct.cache.Get(ct.marketbarviewname)
|
||||
if found {
|
||||
ct.cache.Delete(ct.marketbarviewname)
|
||||
ct.updateMarketbar()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
topOffset = topOffset + 1
|
||||
if v, err := g.SetView("chart", 0, topOffset, maxX, topOffset+chartHeight); err != nil {
|
||||
if v, err := g.SetView(ct.chartviewname, 0, topOffset, maxX, topOffset+chartHeight); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
ct.chartview = v
|
||||
ct.chartview.Frame = false
|
||||
go ct.updateChart()
|
||||
go func() {
|
||||
ct.updateChart()
|
||||
_, found := ct.cache.Get("globaldata")
|
||||
if found {
|
||||
ct.cache.Delete("globaldata")
|
||||
ct.updateChart()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
topOffset = topOffset + chartHeight
|
||||
if v, err := g.SetView("header", 0, topOffset, ct.maxtablewidth, topOffset+2); err != nil {
|
||||
if v, err := g.SetView(ct.headerviewname, 0, topOffset, ct.maxtablewidth, topOffset+2); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
@ -45,7 +60,7 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
}
|
||||
|
||||
topOffset = topOffset + 1
|
||||
if v, err := g.SetView("table", 0, topOffset, ct.maxtablewidth, maxY-1); err != nil {
|
||||
if v, err := g.SetView(ct.tableviewname, 0, topOffset, ct.maxtablewidth, maxY-1); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
@ -57,10 +72,16 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
go func() {
|
||||
ct.updateCoins()
|
||||
ct.updateTable()
|
||||
_, found := ct.cache.Get("allcoinsmap")
|
||||
if found {
|
||||
ct.cache.Delete("allcoinsmap")
|
||||
ct.updateCoins()
|
||||
ct.updateTable()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if v, err := g.SetView("statusbar", 0, maxY-2, ct.maxtablewidth, maxY); err != nil {
|
||||
if v, err := g.SetView(ct.statusbarviewname, 0, maxY-2, ct.maxtablewidth, maxY); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
@ -71,7 +92,7 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
go ct.updateStatusbar("")
|
||||
}
|
||||
|
||||
if v, err := g.SetView("searchfield", 0, maxY-2, ct.maxtablewidth, maxY); err != nil {
|
||||
if v, err := g.SetView(ct.searchfieldviewname, 0, maxY-2, ct.maxtablewidth, maxY); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
@ -82,7 +103,7 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
ct.searchfield.FgColor = gocui.ColorWhite
|
||||
}
|
||||
|
||||
if v, err := g.SetView("help", 1, 1, ct.maxtablewidth-2, maxY-1); err != nil {
|
||||
if v, err := g.SetView(ct.helpviewname, 1, 1, ct.maxtablewidth-2, maxY-1); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
@ -94,9 +115,9 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
// run only once on init.
|
||||
// this bit of code should be at the bottom
|
||||
ct.g = g
|
||||
g.SetViewOnBottom("searchfield") // hide
|
||||
g.SetViewOnBottom("help") // hide
|
||||
ct.setActiveView("table")
|
||||
g.SetViewOnBottom(ct.searchfieldviewname) // hide
|
||||
g.SetViewOnBottom(ct.helpviewname) // hide
|
||||
ct.setActiveView(ct.tableviewname)
|
||||
ct.intervalFetchData()
|
||||
}
|
||||
|
||||
@ -106,12 +127,12 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
|
||||
func (ct *Cointop) setActiveView(v string) error {
|
||||
ct.g.SetViewOnTop(v)
|
||||
ct.g.SetCurrentView(v)
|
||||
if v == "searchfield" {
|
||||
if v == ct.searchfieldviewname {
|
||||
ct.searchfield.Clear()
|
||||
ct.searchfield.SetCursor(1, 0)
|
||||
fmt.Fprintf(ct.searchfield, "%s", "/")
|
||||
} else if v == "table" {
|
||||
ct.g.SetViewOnTop("statusbar")
|
||||
} else if v == ct.tableviewname {
|
||||
ct.g.SetViewOnTop(ct.statusbarviewname)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -43,9 +43,9 @@ func (ct *Cointop) updateMarketbar() error {
|
||||
chartname = "Global"
|
||||
}
|
||||
ct.update(func() {
|
||||
ct.marketview.Clear()
|
||||
ct.marketbarview.Clear()
|
||||
fmt.Fprintln(
|
||||
ct.marketview,
|
||||
ct.marketbarview,
|
||||
pad.Right(
|
||||
fmt.Sprintf(
|
||||
"%s [ Chart: %s %s ] Global ▶ Market Cap: %s • 24H Volume: %s • BTC Dominance: %.2f%% • Active Currencies: %s • Active Markets: %s",
|
||||
|
@ -8,12 +8,12 @@ import (
|
||||
)
|
||||
|
||||
func (ct *Cointop) openSearch() error {
|
||||
ct.setActiveView("searchfield")
|
||||
ct.setActiveView(ct.searchfieldviewname)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ct *Cointop) cancelSearch() error {
|
||||
ct.setActiveView("table")
|
||||
ct.setActiveView(ct.tableviewname)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ func (ct *Cointop) doSearch() error {
|
||||
ct.searchfield.Rewind()
|
||||
b := make([]byte, 100)
|
||||
n, err := ct.searchfield.Read(b)
|
||||
defer ct.setActiveView("table")
|
||||
defer ct.setActiveView(ct.tableviewname)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user