Skip to content

Commit

Permalink
feature mostly complete, must update gif
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 committed Nov 28, 2023
1 parent fd789c5 commit 7edf332
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 85 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ Example yaml file showing all options (copy this into `$HOME/.wander.yaml` and u
# If True, start with filtering active on first view. Default False
#wander_start_filtering: False

# If True, filtering highlights and allows cycling through matches, but does not remove surrounding context. Default True
#wander_filter_with_context: True

# 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 @@ -164,6 +164,12 @@ var (
isBool: true,
defaultIfBool: false,
},
"filter-with-context": {
cfgFileEnvVar: "wander_filter_with_context",
description: `Filtering highlights and allows cycling through matches, but does not remove surrounding context`,
isBool: true,
defaultIfBool: true,
},
}

description = `wander is a terminal application for Nomad by HashiCorp. It is used to
Expand Down Expand Up @@ -225,6 +231,7 @@ func init() {
"start-all-tasks",
"compact-tables",
"start-filtering",
"filter-with-context",
} {
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 @@ -249,6 +249,11 @@ func retrieveStartFiltering(cmd *cobra.Command) bool {
return trueIfTrue(v)
}

func retrieveFilterWithContext(cmd *cobra.Command) bool {
v := cmd.Flags().Lookup("filter-with-context").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 @@ -299,6 +304,7 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
startAllTasksView := retrieveStartAllTasksView(cmd)
compactTables := retrieveCompactTables(cmd)
startFiltering := retrieveStartFiltering(cmd)
filterWithContext := retrieveFilterWithContext(cmd)

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

type Model struct {
Expand Down Expand Up @@ -170,17 +171,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case nomad.PageLoadedMsg:
if msg.Page == m.currentPage {
m.getCurrentPageModel().SetHeader(msg.TableHeader)
m.getCurrentPageModel().SetAllPageData(msg.AllPageRows)
m.getCurrentPageModel().SetAllPageRows(msg.AllPageRows)
if m.currentPageLoading() {
m.getCurrentPageModel().SetViewportXOffset(0)
}
if m.getCurrentPageModel().FilterWithContext {
m.getCurrentPageModel().ResetContextFilter()
}
m.getCurrentPageModel().SetLoading(false)

if m.currentPage.CanBeFirstPage() && len(msg.AllPageRows) == 0 {
// oddly, nomad http api errors when one provides the wrong token,
// but returns empty results when one provides an empty token
m.getCurrentPageModel().SetHeader([]string{"Error"})
m.getCurrentPageModel().SetAllPageData([]page.Row{
m.getCurrentPageModel().SetAllPageRows([]page.Row{
{"", "No results. Is the cluster empty or was no nomad token provided?"},
{"", "Press q or ctrl+c to quit."},
})
Expand Down Expand Up @@ -327,7 +331,7 @@ func (m *Model) initialize() error {
m.pageModels = make(map[nomad.Page]*page.Model)
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)
p := page.New(pageConfig, m.config.CopySavePath, startFiltering, m.config.FilterWithContext)
m.pageModels[k] = &p
}

Expand Down
16 changes: 14 additions & 2 deletions internal/tui/components/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Model struct {
keyMap filterKeyMap
textinput textinput.Model
compact bool
suffix string
}

func New(prefix string) Model {
Expand Down Expand Up @@ -50,6 +51,7 @@ func (m Model) View() string {
} else {
// editing but no filter value yet
m.textinput.Prompt = ""
m.SetSuffix("")
m.textinput.Cursor.SetMode(cursor.CursorHide)
m.textinput.SetValue("type to filter")
}
Expand All @@ -66,8 +68,10 @@ func (m Model) View() string {
m.textinput.PromptStyle = style.Regular
m.textinput.TextStyle = style.Regular
m.textinput.SetValue("'/' to filter")
m.SetSuffix("")
}
}
m.textinput.SetValue(m.textinput.Value() + m.suffix)
filterString := m.textinput.View()
filterStringStyle := m.textinput.TextStyle.Copy().MarginLeft(1).PaddingLeft(1).PaddingRight(0)

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

func (m *Model) SetPrefix(prefix string) {
m.prefix = prefix
func (m Model) HasFilterText() bool {
return m.Value() != ""
}

func (m Model) Focused() bool {
return m.textinput.Focused()
}

func (m *Model) SetPrefix(prefix string) {
m.prefix = prefix
}

func (m *Model) SetSuffix(suffix string) {
m.suffix = suffix
}

func (m *Model) Focus() {
m.textinput.Cursor.SetMode(cursor.CursorBlink)
m.textinput.Focus()
Expand Down
Loading

0 comments on commit 7edf332

Please sign in to comment.