Skip to content

Commit

Permalink
fix #252: Allow clicking on shortcut buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Nov 9, 2024
1 parent ff2eaa9 commit 1e6ff70
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 41 deletions.
11 changes: 10 additions & 1 deletion src/config/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -85,14 +86,15 @@ type (
)

func newStyle() Style {
return Style{
s := Style{
Name: "Default",
Body: newBody(),
StatTable: newStatTable(),
ProcTable: newProcTable(),
Help: newHelp(),
Dialog: newDialog(),
}
return s
}

func newBody() Body {
Expand Down Expand Up @@ -131,6 +133,7 @@ func newHelp() Help {
FgColor: "black",
KeyColor: "white",
HlColor: "green",
ButtonBgColor: "black",
FgCategoryColor: "lightskyblue",
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/config/themes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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()
Expand Down
33 changes: 22 additions & 11 deletions src/tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/tui/main-grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/tui/proc-table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions src/tui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
48 changes: 24 additions & 24 deletions src/tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(""),
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 1e6ff70

Please sign in to comment.