Skip to content

Commit

Permalink
Add config option to start with first page filter active (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 authored Nov 11, 2023
1 parent ccf1712 commit 15cfb6c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 41 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ Example yaml file showing all options (copy this into `$HOME/.wander.yaml` and u
# Log byte offset from which logs start. Default 1000000
#wander_log_offset: 1000000

# If True, start with filtering active on first view. Default False
#wander_start_filtering: False

# If True, follow new logs as they come in rather than having to reload. Default True
#wander_log_tail: True

Expand Down
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ var (
isBool: true,
defaultIfBool: true,
},
"start-filtering": {
cfgFileEnvVar: "wander_start_filtering",
description: `Start with filtering active on first view`,
isBool: true,
defaultIfBool: false,
},
}

description = `wander is a terminal application for Nomad by HashiCorp. It is used to
Expand Down Expand Up @@ -218,6 +224,7 @@ func init() {
"compact-header",
"start-all-tasks",
"compact-tables",
"start-filtering",
} {
c := rootNameToArg[cliLong]
if c.isBool {
Expand Down
7 changes: 7 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ func retrieveCompactTables(cmd *cobra.Command) bool {
return trueIfTrue(v)
}

func retrieveStartFiltering(cmd *cobra.Command) bool {
v := cmd.Flags().Lookup("start-filtering").Value.String()
return trueIfTrue(v)
}

// customLoggingMiddleware provides basic connection logging. Connects are logged with the
// remote address, invoked command, TERM setting, window dimensions and if the
// auth was public key based. Disconnect will log the remote address and
Expand Down Expand Up @@ -293,6 +298,7 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
startCompact := retrieveStartCompact(cmd)
startAllTasksView := retrieveStartAllTasksView(cmd)
compactTables := retrieveCompactTables(cmd)
startFiltering := retrieveStartFiltering(cmd)

initialModel := app.InitialModel(app.Config{
Version: getVersion(),
Expand Down Expand Up @@ -327,6 +333,7 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
StartCompact: startCompact,
StartAllTasksView: startAllTasksView,
CompactTables: compactTables,
StartFiltering: startFiltering,
})
return initialModel, []tea.ProgramOption{tea.WithAltScreen()}
}
15 changes: 12 additions & 3 deletions internal/tui/components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Config struct {
StartCompact bool
StartAllTasksView bool
CompactTables bool
StartFiltering bool
}

type Model struct {
Expand Down Expand Up @@ -92,11 +93,16 @@ type Model struct {
err error
}

func InitialModel(c Config) Model {
func getFirstPage(c Config) nomad.Page {
firstPage := nomad.JobsPage
if c.StartAllTasksView {
firstPage = nomad.AllTasksPage
}
return firstPage
}

func InitialModel(c Config) Model {
firstPage := getFirstPage(c)
initialHeader := header.New(
constants.LogoString,
c.LogoColor,
Expand Down Expand Up @@ -316,9 +322,12 @@ func (m *Model) initialize() error {
}
m.client = *client

firstPage := getFirstPage(m.config)

m.pageModels = make(map[nomad.Page]*page.Model)
for k, c := range nomad.GetAllPageConfigs(m.width, m.getPageHeight(), m.config.CopySavePath, m.config.CompactTables) {
p := page.New(c)
for k, pageConfig := range nomad.GetAllPageConfigs(m.width, m.getPageHeight(), m.config.CompactTables) {
startFiltering := m.config.StartFiltering && k == firstPage
p := page.New(pageConfig, m.config.CopySavePath, startFiltering)
m.pageModels[k] = &p
}

Expand Down
17 changes: 10 additions & 7 deletions internal/tui/components/page/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
)

type Config struct {
Width, Height int
LoadingString string
CopySavePath, SelectionEnabled, WrapText, RequestInput bool
CompactTableContent bool
ViewportConditionalStyle map[string]lipgloss.Style
Width, Height int
LoadingString string
SelectionEnabled, WrapText, RequestInput bool
CompactTableContent bool
ViewportConditionalStyle map[string]lipgloss.Style
}

type Model struct {
Expand All @@ -45,8 +45,11 @@ type Model struct {
initialized bool
}

func New(c Config) Model {
func New(c Config, copySavePath, startFiltering bool) Model {
pageFilter := filter.New("")
if startFiltering {
pageFilter.Focus()
}
pageViewport := viewport.New(c.Width, c.Height-pageFilter.ViewHeight(), c.CompactTableContent)
pageViewport.SetSelectionEnabled(c.SelectionEnabled)
pageViewport.SetWrapText(c.WrapText)
Expand All @@ -69,7 +72,7 @@ func New(c Config) Model {
filter: pageFilter,
loadingString: c.LoadingString,
loading: true,
copySavePath: c.CopySavePath,
copySavePath: copySavePath,
doesRequestInput: c.RequestInput,
textinput: pageTextInput,
needsNewInput: needsNewInput,
Expand Down
62 changes: 31 additions & 31 deletions internal/tui/nomad/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,89 +36,89 @@ const (
LoglinePage
)

func GetAllPageConfigs(width, height int, copySavePath, compactTables bool) map[Page]page.Config {
func GetAllPageConfigs(width, height int, compactTables bool) map[Page]page.Config {
return map[Page]page.Config{
JobsPage: {
Width: width, Height: height,
LoadingString: JobsPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: true, WrapText: false, RequestInput: false,
LoadingString: JobsPage.LoadingString(),
SelectionEnabled: true, WrapText: false, RequestInput: false,
CompactTableContent: compactTables,
ViewportConditionalStyle: constants.JobsTableStatusStyles,
},
AllTasksPage: {
Width: width, Height: height,
LoadingString: AllTasksPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: true, WrapText: false, RequestInput: false,
LoadingString: AllTasksPage.LoadingString(),
SelectionEnabled: true, WrapText: false, RequestInput: false,
CompactTableContent: compactTables,
ViewportConditionalStyle: constants.TasksTableStatusStyles,
},
JobSpecPage: {
Width: width, Height: height,
LoadingString: JobSpecPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: false,
LoadingString: JobSpecPage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: false,
},
JobEventsPage: {
Width: width, Height: height,
LoadingString: JobEventsPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: true, WrapText: false, RequestInput: false,
LoadingString: JobEventsPage.LoadingString(),
SelectionEnabled: true, WrapText: false, RequestInput: false,
},
JobEventPage: {
Width: width, Height: height,
LoadingString: JobEventPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: false,
LoadingString: JobEventPage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: false,
},
JobMetaPage: {
Width: width, Height: height,
LoadingString: JobMetaPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: false,
LoadingString: JobMetaPage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: false,
CompactTableContent: compactTables,
},
AllocEventsPage: {
Width: width, Height: height,
LoadingString: AllocEventsPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: true, WrapText: false, RequestInput: false,
LoadingString: AllocEventsPage.LoadingString(),
SelectionEnabled: true, WrapText: false, RequestInput: false,
},
AllocEventPage: {
Width: width, Height: height,
LoadingString: AllocEventPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: false,
LoadingString: AllocEventPage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: false,
},
AllEventsPage: {
Width: width, Height: height,
LoadingString: AllEventsPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: true, WrapText: false, RequestInput: false,
LoadingString: AllEventsPage.LoadingString(),
SelectionEnabled: true, WrapText: false, RequestInput: false,
},
AllEventPage: {
Width: width, Height: height,
LoadingString: AllEventPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: false,
LoadingString: AllEventPage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: false,
},
JobTasksPage: {
Width: width, Height: height,
LoadingString: JobTasksPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: true, WrapText: false, RequestInput: false,
LoadingString: JobTasksPage.LoadingString(),
SelectionEnabled: true, WrapText: false, RequestInput: false,
CompactTableContent: compactTables,
ViewportConditionalStyle: constants.TasksTableStatusStyles,
},
ExecPage: {
Width: width, Height: height,
LoadingString: ExecPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: true,
LoadingString: ExecPage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: true,
},
AllocSpecPage: {
Width: width, Height: height,
LoadingString: AllocSpecPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: false,
LoadingString: AllocSpecPage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: false,
},
LogsPage: {
Width: width, Height: height,
LoadingString: LogsPage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: true, WrapText: false, RequestInput: false,
LoadingString: LogsPage.LoadingString(),
SelectionEnabled: true, WrapText: false, RequestInput: false,
},
LoglinePage: {
Width: width, Height: height,
LoadingString: LoglinePage.LoadingString(),
CopySavePath: copySavePath, SelectionEnabled: false, WrapText: true, RequestInput: false,
LoadingString: LoglinePage.LoadingString(),
SelectionEnabled: false, WrapText: true, RequestInput: false,
},
}
}
Expand Down

0 comments on commit 15cfb6c

Please sign in to comment.