From 1e6ff70d699d8031abb873c210bc79a22ec5a462 Mon Sep 17 00:00:00 2001 From: Berger Eugene Date: Tue, 5 Nov 2024 01:17:59 +0200 Subject: [PATCH] fix #252: Allow clicking on shortcut buttons --- src/config/styles.go | 11 +++++++++- src/config/themes.go | 4 ++++ src/tui/actions.go | 33 +++++++++++++++++++---------- src/tui/main-grid.go | 2 +- src/tui/proc-table.go | 2 +- src/tui/styles.go | 5 ++--- src/tui/view.go | 48 +++++++++++++++++++++---------------------- 7 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/config/styles.go b/src/config/styles.go index f7b9106..11e7cf7 100644 --- a/src/config/styles.go +++ b/src/config/styles.go @@ -65,6 +65,7 @@ type ( KeyColor Color `yaml:"keyColor"` FgColor Color `yaml:"fgColor"` HlColor Color `yaml:"hlColor"` + ButtonBgColor Color `yaml:"buttonBgColor"` FgCategoryColor Color `yaml:"categoryFgColor"` } @@ -85,7 +86,7 @@ type ( ) func newStyle() Style { - return Style{ + s := Style{ Name: "Default", Body: newBody(), StatTable: newStatTable(), @@ -93,6 +94,7 @@ func newStyle() Style { Help: newHelp(), Dialog: newDialog(), } + return s } func newBody() Body { @@ -131,6 +133,7 @@ func newHelp() Help { FgColor: "black", KeyColor: "white", HlColor: "green", + ButtonBgColor: "black", FgCategoryColor: "lightskyblue", } } @@ -170,6 +173,7 @@ func (s *Styles) Load(path string) error { if err := yaml.Unmarshal(b, s); err != nil { return err } + s.setDefaults() return nil } @@ -244,6 +248,11 @@ func (s *Styles) GetStyleName() string { return s.Style.Name } +// setDefaults sets the background color to help button. +func (s *Styles) setDefaults() { + s.Style.Help.ButtonBgColor = s.Body().BgColor +} + // Dump for debug. func (s *Styles) Dump(w io.Writer) { b, _ := yaml.Marshal(s) diff --git a/src/config/themes.go b/src/config/themes.go index 4913ceb..41bfbbe 100644 --- a/src/config/themes.go +++ b/src/config/themes.go @@ -46,13 +46,16 @@ func NewThemes() *Themes { log.Err(err).Msgf("Error parsing theme %s", fileName) continue } + s.setDefaults() t.styles = append(t.styles, s) } } else { log.Err(err).Msg("Error reading themes folder") } custom, err := t.loadFromFile() + if err == nil { + custom.setDefaults() t.styles = append(t.styles, custom) } @@ -103,6 +106,7 @@ func (t *Themes) SelectStyles(name string) { func (t *Themes) SelectStylesFromFile() { custom, err := t.loadFromFile() if err == nil { + custom.setDefaults() t.activeStyles = custom t.activeStyles.Update() t.fireStylesChanged() diff --git a/src/tui/actions.go b/src/tui/actions.go index 3d8ac03..fb63ba6 100644 --- a/src/tui/actions.go +++ b/src/tui/actions.go @@ -4,9 +4,9 @@ import ( "fmt" "github.com/f1bonacc1/process-compose/src/config" "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" "github.com/rs/zerolog/log" "gopkg.in/yaml.v2" - "io" "os" "strings" ) @@ -171,18 +171,21 @@ func (s *ShortCuts) applyValid(newSc *ShortCuts) { } } -func (s *ShortCuts) writeButton(action ActionName, w io.Writer) { - s.ShortCutKeys[action].writeButton(w, s.style) +func (s *ShortCuts) addButton(action ActionName, flex *tview.Flex) { + s.ShortCutKeys[action].addButton(flex, s.style) } -func (s *ShortCuts) writeToggleButton(action ActionName, w io.Writer, state bool) { - s.ShortCutKeys[action].writeToggleButton(w, state, s.style) +func (s *ShortCuts) addToggleButton(action ActionName, flex *tview.Flex, state bool) { + s.ShortCutKeys[action].addToggleButton(flex, state, s.style) } -func (s *ShortCuts) writeCategory(category string, w io.Writer) { - _, _ = fmt.Fprintf(w, "[%s::b]%s[-:-:-] ", +func (s *ShortCuts) addCategory(category string, flex *tview.Flex) { + textView := tview.NewTextView().SetDynamicColors(true) + _, _ = fmt.Fprintf(textView, "[%s::b]%s[-:-:-] ", string(s.style.FgCategoryColor), category) + + flex.AddItem(textView, len(category)+1, 1, false) } func (s *ShortCuts) StylesChanged(style *config.Styles) { @@ -253,20 +256,28 @@ type Action struct { actionFn func() } -func (a Action) writeButton(w io.Writer, style config.Help) { - _, _ = fmt.Fprintf(w, "%s[%s:%s:]%s[-:-:-] ", +func (a Action) addButton(flex *tview.Flex, style config.Help) { + btnText := fmt.Sprintf("%s[%s:%s:]%s[-:-:-] ", a.ShortCut, string(style.FgColor), string(style.HlColor), a.Description) + button := tview.NewButton(btnText).SetSelectedFunc(a.actionFn) + button.SetStyle(tcell.StyleDefault.Background(style.ButtonBgColor.Color()).Foreground(style.KeyColor.Color())) + button.SetActivatedStyle(tcell.StyleDefault.Background(style.ButtonBgColor.Color()).Foreground(style.KeyColor.Color())) + flex.AddItem(button, len(a.ShortCut+a.Description)+1, 1, false) } -func (a Action) writeToggleButton(w io.Writer, state bool, style config.Help) { - _, _ = fmt.Fprintf(w, "%s[%s:%s:]%s[-:-:-] ", +func (a Action) addToggleButton(flex *tview.Flex, state bool, style config.Help) { + btnText := fmt.Sprintf("%s[%s:%s:]%s[-:-:-] ", a.ShortCut, string(style.FgColor), string(style.HlColor), a.ToggleDescription[state]) + button := tview.NewButton(btnText).SetSelectedFunc(a.actionFn) + button.SetStyle(tcell.StyleDefault.Background(style.ButtonBgColor.Color()).Foreground(style.KeyColor.Color())) + button.SetActivatedStyle(tcell.StyleDefault.Background(style.ButtonBgColor.Color()).Foreground(style.KeyColor.Color())) + flex.AddItem(button, len(a.ShortCut+a.ToggleDescription[state])+1, 1, false) } func newShortCuts() *ShortCuts { diff --git a/src/tui/main-grid.go b/src/tui/main-grid.go index b63fa10..c96c6ce 100644 --- a/src/tui/main-grid.go +++ b/src/tui/main-grid.go @@ -40,7 +40,7 @@ func (pv *pcView) createGrid() { row++ } if !pv.isFullScreen { - pv.mainGrid.AddItem(pv.helpText, row, 0, 1, 1, 0, 0, false) + pv.mainGrid.AddItem(pv.helpFooter, row, 0, 1, 1, 0, 0, false) } pv.appView.SetFocus(pv.mainGrid) pv.autoAdjustProcTableHeight() diff --git a/src/tui/proc-table.go b/src/tui/proc-table.go index d7c1951..da610ca 100644 --- a/src/tui/proc-table.go +++ b/src/tui/proc-table.go @@ -111,7 +111,7 @@ func (pv *pcView) fillTableData() { func (pv *pcView) getMaxProcHeight() int { _, _, _, gridHeight := pv.mainGrid.GetRect() const padding = 7 - _, _, _, helpHeight := pv.helpText.GetRect() + _, _, _, helpHeight := pv.helpFooter.GetRect() gridHeight = gridHeight - pv.statTable.GetRowCount() - helpHeight - padding return gridHeight / 2 } diff --git a/src/tui/styles.go b/src/tui/styles.go index f2a7b7b..4594772 100644 --- a/src/tui/styles.go +++ b/src/tui/styles.go @@ -54,9 +54,8 @@ func (pv *pcView) setLogViewStyle(s *config.Styles) { } func (pv *pcView) setHelpTextStyles(s *config.Styles) { - pv.helpText.SetBackgroundColor(s.BgColor()) - pv.helpText.SetBorderColor(s.BorderColor()) - pv.helpText.SetTextColor(s.Help().KeyColor.Color()) + pv.helpFooter.SetBackgroundColor(s.BgColor()) + pv.helpFooter.SetBorderColor(s.BorderColor()) pv.shortcuts.StylesChanged(s) pv.updateHelpTextView() } diff --git a/src/tui/view.go b/src/tui/view.go index 28cbdf7..47064ca 100644 --- a/src/tui/view.go +++ b/src/tui/view.go @@ -52,7 +52,7 @@ type pcView struct { appView *tview.Application logsText *LogView statusText *tview.TextView - helpText *tview.TextView + helpFooter *tview.Flex pages *tview.Pages procNames []string logFollow bool @@ -98,7 +98,7 @@ func newPcView(project app.IProject) *pcView { statusText: tview.NewTextView().SetDynamicColors(true), logFollow: true, scrSplitState: LogProcHalf, - helpText: tview.NewTextView().SetDynamicColors(true), + helpFooter: tview.NewFlex(), loggedProc: "", procCountCell: tview.NewTableCell(""), procMemCpuCell: tview.NewTableCell(""), @@ -426,35 +426,35 @@ func (pv *pcView) onProcRowSpanChange() { func (pv *pcView) updateHelpTextView() { logScrBool := pv.scrSplitState != LogFull procScrBool := pv.scrSplitState != ProcFull - pv.helpText.Clear() + pv.helpFooter.Clear() + defer pv.helpFooter.AddItem(tview.NewBox(), 0, 1, false) if pv.logsText.isSearchActive() { - pv.shortcuts.writeButton(ActionLogFind, pv.helpText) - pv.shortcuts.writeButton(ActionLogFindNext, pv.helpText) - pv.shortcuts.writeButton(ActionLogFindPrev, pv.helpText) + pv.shortcuts.addButton(ActionLogFind, pv.helpFooter) + pv.shortcuts.addButton(ActionLogFindNext, pv.helpFooter) + pv.shortcuts.addButton(ActionLogFindPrev, pv.helpFooter) if config.IsLogSelectionOn() { - pv.shortcuts.writeToggleButton(ActionLogSelection, pv.helpText, !pv.logSelect) + pv.shortcuts.addToggleButton(ActionLogSelection, pv.helpFooter, !pv.logSelect) } - pv.shortcuts.writeButton(ActionLogFindExit, pv.helpText) + pv.shortcuts.addButton(ActionLogFindExit, pv.helpFooter) return } - pv.shortcuts.writeButton(ActionHelp, pv.helpText) - pv.shortcuts.writeCategory("LOGS:", pv.helpText) - pv.shortcuts.writeToggleButton(ActionLogScreen, pv.helpText, logScrBool) - pv.shortcuts.writeToggleButton(ActionFollowLog, pv.helpText, !pv.logFollow) - pv.shortcuts.writeToggleButton(ActionWrapLog, pv.helpText, !pv.logsText.IsWrapOn()) + pv.shortcuts.addButton(ActionHelp, pv.helpFooter) + pv.shortcuts.addCategory("LOGS:", pv.helpFooter) + pv.shortcuts.addToggleButton(ActionLogScreen, pv.helpFooter, logScrBool) + pv.shortcuts.addToggleButton(ActionFollowLog, pv.helpFooter, !pv.logFollow) + pv.shortcuts.addToggleButton(ActionWrapLog, pv.helpFooter, !pv.logsText.IsWrapOn()) if config.IsLogSelectionOn() { - pv.shortcuts.writeToggleButton(ActionLogSelection, pv.helpText, !pv.logSelect) + pv.shortcuts.addToggleButton(ActionLogSelection, pv.helpFooter, !pv.logSelect) } - pv.shortcuts.writeButton(ActionLogFind, pv.helpText) - //fmt.Fprintf(pv.helpText, "%s ", "[lightskyblue::b]PROCESS:[-:-:-]") - pv.shortcuts.writeCategory("PROCESS:", pv.helpText) - pv.shortcuts.writeButton(ActionProcessScale, pv.helpText) - pv.shortcuts.writeButton(ActionProcessInfo, pv.helpText) - pv.shortcuts.writeButton(ActionProcessStart, pv.helpText) - pv.shortcuts.writeToggleButton(ActionProcessScreen, pv.helpText, procScrBool) - pv.shortcuts.writeButton(ActionProcessStop, pv.helpText) - pv.shortcuts.writeButton(ActionProcessRestart, pv.helpText) - pv.shortcuts.writeButton(ActionQuit, pv.helpText) + pv.shortcuts.addButton(ActionLogFind, pv.helpFooter) + pv.shortcuts.addCategory("PROCESS:", pv.helpFooter) + pv.shortcuts.addButton(ActionProcessScale, pv.helpFooter) + pv.shortcuts.addButton(ActionProcessInfo, pv.helpFooter) + pv.shortcuts.addButton(ActionProcessStart, pv.helpFooter) + pv.shortcuts.addToggleButton(ActionProcessScreen, pv.helpFooter, procScrBool) + pv.shortcuts.addButton(ActionProcessStop, pv.helpFooter) + pv.shortcuts.addButton(ActionProcessRestart, pv.helpFooter) + pv.shortcuts.addButton(ActionQuit, pv.helpFooter) } func (pv *pcView) saveTuiState() {