Skip to content

Commit

Permalink
cmd/ask: allow users to select a command and copy it to the clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed May 14, 2024
1 parent fe176de commit 21ed67f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
52 changes: 48 additions & 4 deletions cmd/ask.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cmd

import (
"errors"
"fmt"
"os"
"runtime"
"strings"

"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
"github.com/getsavvyinc/savvy-cli/client"
"github.com/getsavvyinc/savvy-cli/cmd/component"
"github.com/getsavvyinc/savvy-cli/cmd/component/list"
"github.com/getsavvyinc/savvy-cli/display"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -65,25 +68,66 @@ var askCmd = &cobra.Command{
Runbook: *runbook,
})

m, err := newDisplayCommandsModel(rb)
m, err := newAskCommandsModel(rb)
if err != nil {
display.ErrorWithSupportCTA(err)
os.Exit(1)
}

p := tea.NewProgram(m, tea.WithOutput(programOutput), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
result, err := p.Run()
if err != nil {
// TODO: fail gracefully and provide users a link to view the runbook
display.ErrorWithSupportCTA(fmt.Errorf("could not display runbook: %w", err))
os.Exit(1)
}
if rb.URL != "" {
display.Success("View and edit your runbook online at: " + rb.URL)

if m, ok := result.(*askCommands); ok {
selectedCommand := m.l.SelectedCommand()
if selectedCommand == "" {
return
}
if err := clipboard.WriteAll(selectedCommand); err != nil {
display.Info(selectedCommand)
return
}
display.Info(fmt.Sprintf("Copied to clipboard: %s", selectedCommand))
}
return
},
}

type askCommands struct {
l list.Model
}

func newAskCommandsModel(runbook *component.Runbook) (*askCommands, error) {
if runbook == nil {
return nil, errors.New("runbook is empty")
}

listItems := toItems(runbook.Steps)
l := list.NewModelWithDelegate(listItems, runbook.Title, runbook.URL, list.NewAskDelegate())
return &askCommands{l: l}, nil
}
func (dc *askCommands) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
dc.l.Init()
return nil
}

func (dc *askCommands) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m, cmd := dc.l.Update(msg)
if m, ok := m.(list.Model); ok {
dc.l = m
}
return dc, cmd
}

func (dc *askCommands) View() string {
return dc.l.View()
}

func init() {
rootCmd.AddCommand(askCmd)
}
8 changes: 3 additions & 5 deletions cmd/component/list/ask_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ func NewAskDelegate() list.DefaultDelegate {
}

func HandleSelectedCommand(selectedCommand string) tea.Cmd {
return tea.Sequence(
func() tea.Msg {
return SelectedCommandMsg{selectedCommand}
}, tea.Quit,
)
return func() tea.Msg {
return SelectedCommandMsg{selectedCommand}
}
}

type SelectedCommandMsg struct {
Expand Down
6 changes: 5 additions & 1 deletion cmd/component/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
case SelectedCommandMsg:
m.selectedCommand = msg.Command
return m, nil
return m, tea.Quit
}

var cmd tea.Cmd
Expand All @@ -116,3 +116,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m Model) View() string {
return docStyle.Render(m.list.View())
}

func (m Model) SelectedCommand() string {
return m.selectedCommand
}

0 comments on commit 21ed67f

Please sign in to comment.