Skip to content

Commit 115a586

Browse files
Filter includes surrounding context and allows cycling matches by default (#111)
1 parent fd789c5 commit 115a586

File tree

12 files changed

+273
-86
lines changed

12 files changed

+273
-86
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ Example yaml file showing all options (copy this into `$HOME/.wander.yaml` and u
149149
# If True, start with filtering active on first view. Default False
150150
#wander_start_filtering: False
151151

152+
# If True, filtering highlights and allows cycling through matches, but does not remove surrounding context. Default True
153+
#wander_filter_with_context: True
154+
152155
# If True, follow new logs as they come in rather than having to reload. Default True
153156
#wander_log_tail: True
154157

cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ var (
164164
isBool: true,
165165
defaultIfBool: false,
166166
},
167+
"filter-with-context": {
168+
cfgFileEnvVar: "wander_filter_with_context",
169+
description: `Filtering highlights and allows cycling through matches, but does not remove surrounding context`,
170+
isBool: true,
171+
defaultIfBool: true,
172+
},
167173
}
168174

169175
description = `wander is a terminal application for Nomad by HashiCorp. It is used to
@@ -225,6 +231,7 @@ func init() {
225231
"start-all-tasks",
226232
"compact-tables",
227233
"start-filtering",
234+
"filter-with-context",
228235
} {
229236
c := rootNameToArg[cliLong]
230237
if c.isBool {

cmd/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ func retrieveStartFiltering(cmd *cobra.Command) bool {
249249
return trueIfTrue(v)
250250
}
251251

252+
func retrieveFilterWithContext(cmd *cobra.Command) bool {
253+
v := cmd.Flags().Lookup("filter-with-context").Value.String()
254+
return trueIfTrue(v)
255+
}
256+
252257
// customLoggingMiddleware provides basic connection logging. Connects are logged with the
253258
// remote address, invoked command, TERM setting, window dimensions and if the
254259
// auth was public key based. Disconnect will log the remote address and
@@ -299,6 +304,7 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
299304
startAllTasksView := retrieveStartAllTasksView(cmd)
300305
compactTables := retrieveCompactTables(cmd)
301306
startFiltering := retrieveStartFiltering(cmd)
307+
filterWithContext := retrieveFilterWithContext(cmd)
302308

303309
initialModel := app.InitialModel(app.Config{
304310
Version: getVersion(),
@@ -334,6 +340,7 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
334340
StartAllTasksView: startAllTasksView,
335341
CompactTables: compactTables,
336342
StartFiltering: startFiltering,
343+
FilterWithContext: filterWithContext,
337344
})
338345
return initialModel, []tea.ProgramOption{tea.WithAltScreen()}
339346
}

img/wander.gif

1.59 MB
Loading

internal/tui/components/app/app.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Config struct {
5454
StartAllTasksView bool
5555
CompactTables bool
5656
StartFiltering bool
57+
FilterWithContext bool
5758
}
5859

5960
type Model struct {
@@ -170,17 +171,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
170171
case nomad.PageLoadedMsg:
171172
if msg.Page == m.currentPage {
172173
m.getCurrentPageModel().SetHeader(msg.TableHeader)
173-
m.getCurrentPageModel().SetAllPageData(msg.AllPageRows)
174+
m.getCurrentPageModel().SetAllPageRows(msg.AllPageRows)
174175
if m.currentPageLoading() {
175176
m.getCurrentPageModel().SetViewportXOffset(0)
176177
}
178+
if m.getCurrentPageModel().FilterWithContext {
179+
m.getCurrentPageModel().ResetContextFilter()
180+
}
177181
m.getCurrentPageModel().SetLoading(false)
178182

179183
if m.currentPage.CanBeFirstPage() && len(msg.AllPageRows) == 0 {
180184
// oddly, nomad http api errors when one provides the wrong token,
181185
// but returns empty results when one provides an empty token
182186
m.getCurrentPageModel().SetHeader([]string{"Error"})
183-
m.getCurrentPageModel().SetAllPageData([]page.Row{
187+
m.getCurrentPageModel().SetAllPageRows([]page.Row{
184188
{"", "No results. Is the cluster empty or was no nomad token provided?"},
185189
{"", "Press q or ctrl+c to quit."},
186190
})
@@ -327,7 +331,7 @@ func (m *Model) initialize() error {
327331
m.pageModels = make(map[nomad.Page]*page.Model)
328332
for k, pageConfig := range nomad.GetAllPageConfigs(m.width, m.getPageHeight(), m.config.CompactTables) {
329333
startFiltering := m.config.StartFiltering && k == firstPage
330-
p := page.New(pageConfig, m.config.CopySavePath, startFiltering)
334+
p := page.New(pageConfig, m.config.CopySavePath, startFiltering, m.config.FilterWithContext)
331335
m.pageModels[k] = &p
332336
}
333337

internal/tui/components/filter/filter.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Model struct {
1919
keyMap filterKeyMap
2020
textinput textinput.Model
2121
compact bool
22+
suffix string
2223
}
2324

2425
func New(prefix string) Model {
@@ -50,6 +51,7 @@ func (m Model) View() string {
5051
} else {
5152
// editing but no filter value yet
5253
m.textinput.Prompt = ""
54+
m.SetSuffix("")
5355
m.textinput.Cursor.SetMode(cursor.CursorHide)
5456
m.textinput.SetValue("type to filter")
5557
}
@@ -66,8 +68,10 @@ func (m Model) View() string {
6668
m.textinput.PromptStyle = style.Regular
6769
m.textinput.TextStyle = style.Regular
6870
m.textinput.SetValue("'/' to filter")
71+
m.SetSuffix("")
6972
}
7073
}
74+
m.textinput.SetValue(m.textinput.Value() + m.suffix)
7175
filterString := m.textinput.View()
7276
filterStringStyle := m.textinput.TextStyle.Copy().MarginLeft(1).PaddingLeft(1).PaddingRight(0)
7377

@@ -90,14 +94,22 @@ func (m Model) ViewHeight() int {
9094
return lipgloss.Height(m.View())
9195
}
9296

93-
func (m *Model) SetPrefix(prefix string) {
94-
m.prefix = prefix
97+
func (m Model) HasFilterText() bool {
98+
return m.Value() != ""
9599
}
96100

97101
func (m Model) Focused() bool {
98102
return m.textinput.Focused()
99103
}
100104

105+
func (m *Model) SetPrefix(prefix string) {
106+
m.prefix = prefix
107+
}
108+
109+
func (m *Model) SetSuffix(suffix string) {
110+
m.suffix = suffix
111+
}
112+
101113
func (m *Model) Focus() {
102114
m.textinput.Cursor.SetMode(cursor.CursorBlink)
103115
m.textinput.Focus()

0 commit comments

Comments
 (0)