Skip to content

Commit

Permalink
feat: allow setting of fixed columns in the list of issues
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpovolny committed Dec 10, 2022
1 parent 999ddad commit e23dc2c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
1 change: 1 addition & 0 deletions internal/cmd/issue/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func loadList(cmd *cobra.Command) {
return []string{}
}(),
TableStyle: cmdutil.GetTUIStyleConfig(),
CustomKeys: viper.GetStringMap("custom_keys.issues"),
},
}

Expand Down
19 changes: 19 additions & 0 deletions internal/view/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions internal/view/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)),
Expand Down
52 changes: 37 additions & 15 deletions pkg/tui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e23dc2c

Please sign in to comment.