Skip to content

feat: pr commands #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added anim.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type Keybindings struct {
Prs []Keybinding `yaml:"prs"`
}

type Pager struct {
Diff string `yaml:"diff"`
}

type HexColor string

type ColorThemeText struct {
Expand Down Expand Up @@ -94,6 +98,7 @@ type Config struct {
Keybindings Keybindings `yaml:"keybindings"`
RepoPaths map[string]string `yaml:"repoPaths"`
Theme *ThemeConfig `yaml:"theme,omitempty" validate:"omitempty,dive"`
Pager Pager `yaml:"pager"`
}

type configError struct {
Expand Down
28 changes: 28 additions & 0 deletions config/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

import (
"fmt"
"os"
)

func (cfg Config) GetFullScreenDiffPagerEnv() []string {
diff := cfg.Pager.Diff
if diff == "" {
diff = "less"
}
if diff == "delta" {
diff = "delta --paging always"
}

var env = os.Environ()
env = append(
env,
"LESS=CRX",
fmt.Sprintf(
"GH_PAGER=%s",
diff,
),
)

return env
}
31 changes: 17 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/dlvhdr/gh-dash
go 1.17

require (
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
github.com/charmbracelet/bubbles v0.13.0
github.com/charmbracelet/bubbletea v0.22.1
github.com/charmbracelet/glamour v0.5.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/cli/go-gh v0.0.3
github.com/cli/go-gh v0.1.0
github.com/cli/shurcooL-graphql v0.0.1
github.com/go-playground/validator/v10 v10.11.0
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739
github.com/muesli/termenv v0.12.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -20,26 +20,29 @@ require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/henvic/httpretty v0.1.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/microcosm-cc/bluemonday v1.0.18 // indirect
github.com/microcosm-cc/bluemonday v1.0.19 // indirect
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/yuin/goldmark v1.4.12 // indirect
github.com/rivo/uniseg v0.3.4 // indirect
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
github.com/yuin/goldmark v1.4.13 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
81 changes: 50 additions & 31 deletions go.sum

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions ui/components/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/dlvhdr/gh-dash/ui/context"
"github.com/dlvhdr/gh-dash/ui/keys"
"github.com/dlvhdr/gh-dash/ui/styles"
"github.com/dlvhdr/gh-dash/utils"
)

type Model struct {
help bbHelp.Model
help bbHelp.Model
ShowAll bool
}

func NewModel() Model {
help := bbHelp.NewModel()
help.Styles = bbHelp.Styles{
ShortDesc: helpTextStyle.Copy(),
ShortDesc: helpTextStyle.Copy().Foreground(styles.DefaultTheme.FaintText),
FullDesc: helpTextStyle.Copy(),
ShortSeparator: helpTextStyle.Copy(),
ShortSeparator: helpTextStyle.Copy().Foreground(styles.DefaultTheme.SecondaryBorder),
FullSeparator: helpTextStyle.Copy(),
FullKey: helpTextStyle.Copy(),
FullKey: helpTextStyle.Copy().Foreground(styles.DefaultTheme.PrimaryText),
ShortKey: helpTextStyle.Copy(),
Ellipsis: helpTextStyle.Copy(),
}
Expand All @@ -34,18 +35,27 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, utils.Keys.Help):
case key.Matches(msg, keys.Keys.Help):
m.help.ShowAll = !m.help.ShowAll
m.ShowAll = m.help.ShowAll
}
}

return m, nil
}

func (m Model) View(ctx context.ProgramContext) string {
keymap := keys.GetKeyMap(ctx.View)
if m.help.ShowAll {
return styles.FooterStyle.Copy().
Height(styles.ExpandedHelpHeight - 1).
Width(ctx.ScreenWidth).
Render(m.help.View(keymap))
}

return styles.FooterStyle.Copy().
Width(ctx.ScreenWidth).
Render(m.help.View(utils.Keys))
Render(m.help.View(keymap))
}

func (m *Model) SetWidth(width int) {
Expand Down
69 changes: 69 additions & 0 deletions ui/components/prssection/cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package prssection

import (
"errors"
"fmt"
"os"
"os/exec"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/dlvhdr/gh-dash/ui/constants"
"github.com/dlvhdr/gh-dash/ui/context"
)

func (m Model) diff() tea.Cmd {
c := exec.Command(
"gh",
"pr",
"diff",
fmt.Sprint(m.GetCurrRow().GetNumber()),
"-R",
m.GetCurrRow().GetRepoNameWithOwner(),
)
c.Env = m.Ctx.Config.GetFullScreenDiffPagerEnv()

return tea.ExecProcess(c, func(err error) tea.Msg {
if err != nil {
return constants.ErrMsg{Err: err}
}
return nil
})
}

func (m *Model) checkout() (tea.Cmd, error) {
pr := m.GetCurrRow()
repoName := pr.GetRepoNameWithOwner()
repoPath, ok := m.Ctx.Config.RepoPaths[repoName]

if !ok {
return nil, errors.New("Local path to repo not specified, set one in your config.yml under repoPaths")
}

prNumber := pr.GetNumber()
taskId := fmt.Sprintf("checkout_%d", prNumber)
task := context.Task{
Id: taskId,
StartText: fmt.Sprintf("Checking out PR #%d", prNumber),
FinishedText: fmt.Sprintf("PR #%d has been checked out at %s", prNumber, repoPath),
State: context.TaskStart,
Error: nil,
}
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
c := exec.Command(
"gh",
"pr",
"checkout",
fmt.Sprint(m.GetCurrRow().GetNumber()),
)
userHomeDir, _ := os.UserHomeDir()
if strings.HasPrefix(repoPath, "~") {
repoPath = strings.Replace(repoPath, "~", userHomeDir, 1)
}

c.Dir = repoPath
err := c.Run()
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}), nil
}
18 changes: 15 additions & 3 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prssection
import (
"sort"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/dlvhdr/gh-dash/config"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/dlvhdr/gh-dash/ui/components/section"
"github.com/dlvhdr/gh-dash/ui/components/table"
"github.com/dlvhdr/gh-dash/ui/context"
"github.com/dlvhdr/gh-dash/ui/keys"
"github.com/dlvhdr/gh-dash/utils"
)

Expand Down Expand Up @@ -40,19 +42,28 @@ func NewModel(id int, ctx *context.ProgramContext, cfg config.SectionConfig) Mod

func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
var cmd tea.Cmd
var err error

switch msg := msg.(type) {

case tea.KeyMsg:
switch {

switch msg.Type {
case key.Matches(msg, keys.PRKeys.Diff):
cmd = m.diff()

case tea.KeyEnter:
case key.Matches(msg, keys.PRKeys.Checkout):
cmd, err = m.checkout()
if err != nil {
m.Ctx.Error = err
}

case msg.Type == tea.KeyEnter:
m.SearchValue = m.SearchBar.Value()
m.SetIsSearching(false)
return &m, m.FetchSectionRows()

case tea.KeyCtrlC, tea.KeyEsc:
case msg.Type == tea.KeyCtrlC, msg.Type == tea.KeyEsc:
m.SearchBar.SetValue(m.SearchValue)
blinkCmd := m.SetIsSearching(false)
return &m, blinkCmd
Expand Down Expand Up @@ -116,6 +127,7 @@ func GetSectionColumns() []table.Column {
{
Title: "",
Width: &ciCellWidth,
Grow: new(bool),
},
{
Title: "",
Expand Down
8 changes: 6 additions & 2 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,19 @@ func (m *Model) CreateNextTickCmd(nextTickCmd tea.Cmd) tea.Cmd {
func (m *Model) GetDimensions() constants.Dimensions {
return constants.Dimensions{
Width: m.Ctx.MainContentWidth - containerStyle.GetHorizontalPadding(),
Height: m.Ctx.MainContentHeight - 2 - styles.SearchHeight,
Height: m.Ctx.MainContentHeight - styles.SearchHeight,
}
}

func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) {
oldDimensions := m.GetDimensions()
m.Ctx = ctx
newDimensions := m.GetDimensions()
m.Table.SetDimensions(newDimensions)
tableDimensions := constants.Dimensions{
Height: newDimensions.Height - 2,
Width: newDimensions.Width,
}
m.Table.SetDimensions(tableDimensions)

if oldDimensions.Height != newDimensions.Height || oldDimensions.Width != newDimensions.Width {
m.Table.SyncViewPortContent()
Expand Down
6 changes: 3 additions & 3 deletions ui/components/sidebar/sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/dlvhdr/gh-dash/ui/constants"
"github.com/dlvhdr/gh-dash/ui/context"
"github.com/dlvhdr/gh-dash/ui/keys"
)

type Model struct {
Expand Down Expand Up @@ -36,10 +36,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, constants.Keys.PageDown):
case key.Matches(msg, keys.Keys.PageDown):
m.viewport.HalfViewDown()

case key.Matches(msg, constants.Keys.PageUp):
case key.Matches(msg, keys.Keys.PageUp):
m.viewport.HalfViewUp()
}
}
Expand Down
55 changes: 0 additions & 55 deletions ui/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,59 +31,4 @@ var (
WaitingGlyph = lipgloss.NewStyle().Foreground(styles.DefaultTheme.FaintText).Render("")
FailureGlyph = lipgloss.NewStyle().Foreground(styles.DefaultTheme.WarningText).Render("")
SuccessGlyph = lipgloss.NewStyle().Foreground(styles.DefaultTheme.SuccessText).Render("")

Keys = KeyMap{
Up: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "move down"),
),
FirstItem: key.NewBinding(
key.WithKeys("g", "home"),
key.WithHelp("g/home", "first item"),
),
LastItem: key.NewBinding(
key.WithKeys("G", "end"),
key.WithHelp("G/end", "last item"),
),
PrevSection: key.NewBinding(
key.WithKeys("left", "h"),
key.WithHelp("/h", "previous section"),
),
NextSection: key.NewBinding(
key.WithKeys("right", "l"),
key.WithHelp("/l", "next section"),
),
PageUp: key.NewBinding(
key.WithKeys("ctrl+u"),
key.WithHelp("Ctrl+u", "preview page up"),
),
PageDown: key.NewBinding(
key.WithKeys("ctrl+d"),
key.WithHelp("Ctrl+d", "preview page down"),
),
TogglePreview: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "open in Preview"),
),
OpenGithub: key.NewBinding(
key.WithKeys("o"),
key.WithHelp("o", "open in GitHub"),
),
Refresh: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "refresh"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
}
)
Loading