From e23dc2c8e6e92fb32c4e0e67b734cd733e1ce92a Mon Sep 17 00:00:00 2001 From: Martin Povolny Date: Sun, 27 Nov 2022 18:13:39 +0100 Subject: [PATCH] feat: allow setting of fixed columns in the list of issues --- internal/cmd/issue/list/list.go | 1 + internal/view/helper.go | 19 ++++++++++++ internal/view/issues.go | 2 ++ pkg/tui/table.go | 52 +++++++++++++++++++++++---------- 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/internal/cmd/issue/list/list.go b/internal/cmd/issue/list/list.go index 10426306..c28b4864 100644 --- a/internal/cmd/issue/list/list.go +++ b/internal/cmd/issue/list/list.go @@ -142,6 +142,7 @@ func loadList(cmd *cobra.Command) { return []string{} }(), TableStyle: cmdutil.GetTUIStyleConfig(), + CustomKeys: viper.GetStringMap("custom_keys.issues"), }, } diff --git a/internal/view/helper.go b/internal/view/helper.go index 57545b76..4e9086bf 100644 --- a/internal/view/helper.go +++ b/internal/view/helper.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "os/exec" "strings" "text/tabwriter" "time" @@ -115,6 +116,24 @@ func jiraURLFromTuiData(server string, r int, d interface{}) string { return fmt.Sprintf("%s/browse/%s", server, issueKeyFromTuiData(r, d)) } +func runInSh(command string) { + cmd := exec.Command("sh", "-c", command) + _ = cmd.Run() +} + +func customKeyFunc(row, _ int, data interface{}, command string) { + i := issueKeyFromTuiData(row, data) + expandedCommand := strings.ReplaceAll(command, "%KEY%", i) + if strings.HasPrefix(expandedCommand, "&") { + go func() { + expandedCommand = strings.TrimPrefix(expandedCommand, "&") + runInSh(expandedCommand) + }() + } else { + runInSh(expandedCommand) + } +} + func navigate(server string) tui.SelectedFunc { return func(r, c int, d interface{}) { _ = browser.Browse(jiraURLFromTuiData(server, r, d)) diff --git a/internal/view/issues.go b/internal/view/issues.go index a87dd14f..8213feb0 100644 --- a/internal/view/issues.go +++ b/internal/view/issues.go @@ -21,6 +21,7 @@ type DisplayFormat struct { Columns []string FixedColumns uint TableStyle tui.TableStyle + CustomKeys map[string]interface{} } // IssueList is a list view for issues. @@ -52,6 +53,7 @@ func (l *IssueList) Render() error { } view := tui.NewTable( + tui.WithCustomKeyFunc(customKeyFunc, l.Display.CustomKeys), tui.WithTableStyle(l.Display.TableStyle), tui.WithTableFooterText(l.FooterText), tui.WithSelectedFunc(navigate(l.Server)), diff --git a/pkg/tui/table.go b/pkg/tui/table.go index 57b837ed..bb919366 100644 --- a/pkg/tui/table.go +++ b/pkg/tui/table.go @@ -30,6 +30,9 @@ type CopyFunc func(row, column int, data interface{}) // CopyKeyFunc is fired when a user press 'CTRL+K' character in the table cell. type CopyKeyFunc func(row, column int, data interface{}) +// CustomKeyFunc is fired when one of custom keys is pressed in the table cell. +type CustomKeyFunc func(row, column int, data interface{}, command string) + // TableData is the data to be displayed in a table. type TableData [][]string @@ -42,21 +45,23 @@ type TableStyle struct { // Table is a table layout. type Table struct { - screen *Screen - painter *tview.Pages - view *tview.Table - footer *tview.TextView - style TableStyle - data TableData - colPad uint - colFixed uint - maxColWidth uint - footerText string - selectedFunc SelectedFunc - viewModeFunc ViewModeFunc - refreshFunc RefreshFunc - copyFunc CopyFunc - copyKeyFunc CopyKeyFunc + screen *Screen + painter *tview.Pages + view *tview.Table + footer *tview.TextView + style TableStyle + data TableData + colPad uint + colFixed uint + maxColWidth uint + footerText string + selectedFunc SelectedFunc + viewModeFunc ViewModeFunc + customKeys map[string]interface{} + customKeyFunc CustomKeyFunc + refreshFunc RefreshFunc + copyFunc CopyFunc + copyKeyFunc CopyKeyFunc } // TableOption is a functional option to wrap table properties. @@ -121,6 +126,14 @@ func WithViewModeFunc(fn ViewModeFunc) TableOption { } } +// WithCustomKeyFunc sets a func that is triggered when one of custom keys is pressed. +func WithCustomKeyFunc(fn CustomKeyFunc, keys map[string]interface{}) TableOption { + return func(t *Table) { + t.customKeyFunc = fn + t.customKeys = keys + } +} + // WithRefreshFunc sets a func that is triggered when a user press 'CTRL+R' or 'F5'. func WithRefreshFunc(fn RefreshFunc) TableOption { return func(t *Table) { @@ -232,6 +245,15 @@ func (t *Table) initTable() { // Refresh the screen. t.screen.Draw() }() + default: + if t.customKeyFunc != nil && t.customKeys != nil { + for k, command := range t.customKeys { + if string(ev.Rune()) == k { + r, c := t.view.GetSelection() + t.customKeyFunc(r, c, t.data, command.(string)) + } + } + } } } return ev