Skip to content

Commit

Permalink
put log stuff into LogPage struct
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Oct 27, 2023
1 parent 38a67b9 commit 13e0c7a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
10 changes: 1 addition & 9 deletions event_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,7 @@ func (ui *Ui) guiEventLoop() {

case msg := <-ui.logger.Prints:
// handle log page output
ui.app.QueueUpdateDraw(func() {
line := time.Now().Local().Format("(15:04:05) ") + msg
ui.logList.InsertItem(0, line, "", 0, nil)

// Make sure the log list doesn't grow infinitely
for ui.logList.GetItemCount() > 100 {
ui.logList.RemoveItem(-1)
}
})
ui.logPage.Print(msg)

case mpvEvent := <-ui.mpvEvents:
events++
Expand Down
6 changes: 3 additions & 3 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Ui struct {
playlistList *tview.List

// log page
logList *tview.List
logPage *LogPage

// modals
addToPlaylistList *tview.List
Expand Down Expand Up @@ -122,15 +122,15 @@ func InitGui(indexes *[]subsonic.SubsonicIndex,
playlistFlex, deletePlaylistModal := ui.createPlaylistPage()

// log page
logListFlex := ui.createLogPage()
ui.logPage = ui.createLogPage()

ui.pages.AddPage("browser", ui.browserPage.Root, true, true).
AddPage("queue", ui.queuePage.Root, true, false).
AddPage("playlists", playlistFlex, true, false).
AddPage("addToPlaylist", ui.browserPage.AddToPlaylistModal, true, false).
AddPage("deletePlaylist", deletePlaylistModal, true, false).
AddPage("messageBox", ui.messageBox, true, false).
AddPage("log", logListFlex, true, false)
AddPage("log", ui.logPage.Root, true, false)

rootFlex := tview.NewFlex().
SetDirection(tview.FlexRow).
Expand Down
43 changes: 37 additions & 6 deletions page_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,44 @@

package main

import "github.com/rivo/tview"
import (
"time"

func (ui *Ui) createLogPage() *tview.Flex {
"github.com/rivo/tview"
)

ui.logList = tview.NewList().ShowSecondaryText(false)
logListFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(ui.logList, 0, 1, true)
type LogPage struct {
Root *tview.Flex

logList *tview.List

// external refs
ui *Ui
}

func (ui *Ui) createLogPage() *LogPage {
logPage := LogPage{
ui: ui,
}

logPage.logList = tview.NewList().ShowSecondaryText(false)

logPage.Root = tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(logPage.logList, 0, 1, true)

return &logPage
}

func (l *LogPage) Print(line string) {
l.ui.app.QueueUpdateDraw(func() {
line := time.Now().Local().Format("(15:04:05) ") + line
l.logList.InsertItem(0, line, "", 0, nil)

// Make sure the log list doesn't grow infinitely
for l.logList.GetItemCount() > 100 {
l.logList.RemoveItem(-1)
}
})

return logListFlex
}

0 comments on commit 13e0c7a

Please sign in to comment.