mirror of
https://github.com/Luzifer/runemetrics.git
synced 2024-12-22 12:11:20 +00:00
Add scrollable event log
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
a4ff4c444a
commit
3b2609aafc
1 changed files with 34 additions and 10 deletions
44
main.go
44
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -29,7 +30,9 @@ var (
|
||||||
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
|
eventsPage = 0
|
||||||
lastUpdate = map[string]time.Time{}
|
lastUpdate = map[string]time.Time{}
|
||||||
|
playerData *playerInfo
|
||||||
|
|
||||||
version = "dev"
|
version = "dev"
|
||||||
)
|
)
|
||||||
|
@ -73,8 +76,6 @@ func main() {
|
||||||
updateTicker = time.NewTimer(0)
|
updateTicker = time.NewTimer(0)
|
||||||
)
|
)
|
||||||
|
|
||||||
updateUI(player)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
||||||
|
@ -89,13 +90,25 @@ func main() {
|
||||||
|
|
||||||
case "<Resize>":
|
case "<Resize>":
|
||||||
ui.Clear()
|
ui.Clear()
|
||||||
updateUI(player)
|
updateUI(playerData, nil)
|
||||||
|
|
||||||
|
case "<PageDown>":
|
||||||
|
eventsPage++
|
||||||
|
updateUI(playerData, nil)
|
||||||
|
|
||||||
|
case "<PageUp>":
|
||||||
|
eventsPage--
|
||||||
|
updateUI(playerData, nil)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-updateTicker.C:
|
case <-updateTicker.C:
|
||||||
if err := updateUI(player); err != nil {
|
if playerData, err = getPlayerInfo(player, 20); err != nil {
|
||||||
log.WithError(err).Error("Unable to update metrics")
|
log.WithError(err).Error("Unable to fetch metrics")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := updateUI(playerData, err); err != nil {
|
||||||
|
log.WithError(err).Error("Unable to update UI")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
updateTicker.Reset(time.Until(cron.Next(time.Now())))
|
updateTicker.Reset(time.Until(cron.Next(time.Now())))
|
||||||
|
@ -108,11 +121,9 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateUI(player string) error {
|
func updateUI(playerData *playerInfo, err error) error {
|
||||||
termWidth, termHeight := ui.TerminalDimensions()
|
termWidth, termHeight := ui.TerminalDimensions()
|
||||||
|
|
||||||
playerData, err := getPlayerInfo(player, 20)
|
|
||||||
|
|
||||||
// Status-bar
|
// Status-bar
|
||||||
status := widgets.NewParagraph()
|
status := widgets.NewParagraph()
|
||||||
status.Title = "Status"
|
status.Title = "Status"
|
||||||
|
@ -202,11 +213,24 @@ func updateUI(player string) error {
|
||||||
|
|
||||||
// Latest events
|
// Latest events
|
||||||
events := widgets.NewTable()
|
events := widgets.NewTable()
|
||||||
events.Title = "Event Log"
|
|
||||||
events.RowSeparator = false
|
events.RowSeparator = false
|
||||||
events.ColumnWidths = []int{12, termWidth - 3 - 12}
|
events.ColumnWidths = []int{12, termWidth - 3 - 12}
|
||||||
events.SetRect(0, 6+2+len(playerData.SkillValues)+1, termWidth, termHeight-3)
|
events.SetRect(0, 6+2+len(playerData.SkillValues)+1, termWidth, termHeight-3)
|
||||||
for i, logEntry := range playerData.Activities {
|
|
||||||
|
eventsPerPage := termHeight - 3 - (6 + 2 + len(playerData.SkillValues) + 1)
|
||||||
|
eventPages := int(math.Ceil(float64(len(playerData.Activities)) / float64(eventsPerPage)))
|
||||||
|
|
||||||
|
if eventsPage < 0 {
|
||||||
|
eventsPage = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if eventsPage >= eventPages {
|
||||||
|
eventsPage = eventPages - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
events.Title = fmt.Sprintf("Event Log (%d / %d)", eventsPage+1, eventPages)
|
||||||
|
|
||||||
|
for i, logEntry := range playerData.Activities[eventsPage*eventsPerPage:] {
|
||||||
date, _ := logEntry.GetParsedDate()
|
date, _ := logEntry.GetParsedDate()
|
||||||
events.Rows = append(
|
events.Rows = append(
|
||||||
events.Rows,
|
events.Rows,
|
||||||
|
|
Loading…
Reference in a new issue