imap-notes/tui/main.go

55 lines
1.4 KiB
Go
Raw Normal View History

2022-02-11 23:56:04 +00:00
package tui
import (
"git.iamthefij.com/iamthefij/slog"
tea "github.com/charmbracelet/bubbletea"
)
type ActiveSection int
const (
FolderSelect ActiveSection = iota
NoteSelect
NotePager
)
func LaunchTUI() {
// LaunchFolderView()
// p := tea.NewProgram(initialModel(), tea.WithAltScreen())
mvm := NewMultiViewModel([]tea.Model{newFolderList(), newNoteList()}, nil)
mvm.OnColumnSelect = func(baseModel *MultiViewModel, msg tea.Msg) tea.Cmd {
switch baseModel.GetActiveIndex() {
case int(FolderSelect):
folderView := (*baseModel.GetActiveView()).(folderListModel)
noteView := (*baseModel.NextView()).(noteListModel)
noteView.SetFolder(folderView.selectedFolder)
baseModel.SetActiveView(noteView)
case int(NoteSelect):
noteView := (*baseModel.GetActiveView()).(noteListModel)
markdown, err := noteView.selectedNote.ReadMarkdown()
slog.OnErrFatalf(err, "failed getting markdown for selected note")
slog.Debugf("got markdown: %s", markdown)
pager := NewPagerModel(markdown)
baseModel.SetMainView(pager)
baseModel.NextView()
}
return nil
}
mvm.OnColumnBack = func(baseModel *MultiViewModel, msg tea.Msg) tea.Cmd {
if baseModel.GetActiveIndex() == int(NotePager) {
baseModel.ClearMainView()
}
baseModel.PreviousView()
return nil
}
p := tea.NewProgram(mvm, tea.WithAltScreen())
slog.OnErrFatalf(p.Start(), "error running TUI")
}