Skip to content

Commit

Permalink
feat(actions): allow passing string parameters to actions (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored Oct 2, 2024
1 parent a269ba6 commit 82482fa
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion internal/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func Map(client *gh.Client) ActionsMap {
}

type Runner interface {
Run(*notifications.Notification, io.Writer) error
Run(*notifications.Notification, []string, io.Writer) error
}
5 changes: 3 additions & 2 deletions internal/actions/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ package debug
import (
"fmt"
"io"
"strings"

"github.com/nobe4/gh-not/internal/colors"
"github.com/nobe4/gh-not/internal/notifications"
)

type Runner struct{}

func (_ *Runner) Run(n *notifications.Notification, w io.Writer) error {
fmt.Fprint(w, colors.Yellow("DEBUG ")+n.String())
func (_ *Runner) Run(n *notifications.Notification, args []string, w io.Writer) error {
fmt.Fprint(w, colors.Yellow("DEBUG ")+n.String()+" "+strings.Join(args, ", "))

return nil
}
2 changes: 1 addition & 1 deletion internal/actions/done/done.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Runner struct {
Client *gh.Client
}

func (a *Runner) Run(n *notifications.Notification, w io.Writer) error {
func (a *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error {
slog.Debug("marking notification as done", "notification", n)

n.Meta.Done = true
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/hide/hide.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type Runner struct{}

func (_ *Runner) Run(n *notifications.Notification, w io.Writer) error {
func (_ *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error {
slog.Debug("marking notification as hidden", "notification", n.Id)

n.Meta.Hidden = true
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Runner struct {
Client *gh.Client
}

func (a *Runner) Run(n *notifications.Notification, w io.Writer) error {
func (a *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error {
slog.Debug("open notification in browser", "notification", n)

browser := browser.New("", w, w)
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/pass/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (

type Runner struct{}

func (_ *Runner) Run(n *notifications.Notification, _ io.Writer) error {
func (_ *Runner) Run(n *notifications.Notification, _ []string, _ io.Writer) error {
return nil
}
2 changes: 1 addition & 1 deletion internal/actions/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type Runner struct{}

func (_ *Runner) Run(n *notifications.Notification, w io.Writer) error {
func (_ *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error {
if !n.Meta.Hidden {
fmt.Fprint(w, n.String())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Runner struct {
Client *gh.Client
}

func (a *Runner) Run(n *notifications.Notification, w io.Writer) error {
func (a *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error {
_, err := a.Client.API.Request(http.MethodPatch, n.URL, nil)

// go-gh currently fails to handle HTTP-205 correctly, however it's possible
Expand Down
2 changes: 1 addition & 1 deletion internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (m *Manager) Apply() error {
continue
}

if err := runner.Run(notification, os.Stdout); err != nil {
if err := runner.Run(notification, nil, os.Stdout); err != nil {
slog.Error("action failed", "action", rule.Action, "err", err)
}
fmt.Fprintln(os.Stdout, "")
Expand Down
45 changes: 38 additions & 7 deletions internal/repl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,45 @@ import (
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/nobe4/gh-not/internal/actions"
)

type Run struct {
Runner actions.Runner
Args []string
}

func (m model) commandAndArgs(value, suggestion string) (string, []string) {
if value == suggestion {
return value, nil
}

values := strings.Split(value, " ")
command, args := values[0], values[1:]

// Manually get the new suggestion
m.command.SetValue(command)
// Workaround until https://github.com/charmbracelet/bubbles/pull/630 is
// merged.
m.command.SetSuggestions(m.command.AvailableSuggestions())
command = m.command.CurrentSuggestion()

return command, args
}

func (m model) acceptCommand() (tea.Model, tea.Cmd) {
command := m.command.CurrentSuggestion()
command, args := m.commandAndArgs(
m.command.Value(),
m.command.CurrentSuggestion(),
)

slog.Debug("acceptCommand", "command", command)
slog.Debug("acceptCommand", "command", command, "args", args)

m.command.SetValue("")
m.command.Blur()
m.showResult = true

return m, m.applyCommand(command)
return m, m.applyCommand(command, args)
}

func (m model) cancelCommand() (tea.Model, tea.Cmd) {
Expand All @@ -32,9 +59,10 @@ func (m model) cancelCommand() (tea.Model, tea.Cmd) {
type ApplyCommandMsg struct {
Items []item
Command string
Args []string
}

func (m model) applyCommand(command string) tea.Cmd {
func (m model) applyCommand(command string, args []string) tea.Cmd {
return func() tea.Msg {
slog.Debug("applyCommand", "command", command)

Expand All @@ -46,7 +74,7 @@ func (m model) applyCommand(command string) tea.Cmd {
}
}

return ApplyCommandMsg{Items: selected, Command: command}
return ApplyCommandMsg{Items: selected, Command: command, Args: args}
}
}

Expand All @@ -59,7 +87,10 @@ func (msg ApplyCommandMsg) apply(m model) (tea.Model, tea.Cmd) {
}

m.resultStrings = []string{}
m.currentRunner = runner
m.currentRun = Run{
Runner: runner,
Args: msg.Args,
}
m.processQueue = msg.Items

return m, tea.Sequence(m.renderResult(nil), m.applyNext())
Expand Down Expand Up @@ -93,7 +124,7 @@ func (m model) applyNext() tea.Cmd {

message := ""
out := &strings.Builder{}
if err := m.currentRunner.Run(current.notification, out); err != nil {
if err := m.currentRun.Runner.Run(current.notification, m.currentRun.Args, out); err != nil {
message = fmt.Sprintf("Error for '%s': %s", current.notification.Subject.Title, err.Error())
} else {
message = out.String()
Expand Down
2 changes: 1 addition & 1 deletion internal/repl/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (m *model) handleBrowsing(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keymap.Open):
current, ok := m.list.SelectedItem().(item)
if ok {
m.actions["open"].Run(current.notification, os.Stderr)
m.actions["open"].Run(current.notification, nil, os.Stderr)
}

case key.Matches(msg, m.keymap.CommandMode):
Expand Down
6 changes: 3 additions & 3 deletions internal/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

type model struct {
keymap Keymap
actions actions.ActionsMap
currentRunner actions.Runner
keymap Keymap
actions actions.ActionsMap
currentRun Run

showHelp bool
list list.Model
Expand Down

0 comments on commit 82482fa

Please sign in to comment.