Skip to content

Commit

Permalink
fix editor handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Feb 11, 2025
1 parent d73dd0a commit 0cb37e1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 52 deletions.
11 changes: 9 additions & 2 deletions extensions/tldr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ if [ $# -eq 0 ]; then
# each command can be called through the cli
commands: [
{ name: "list", mode: "filter", description: "Search Pages" },
{ name: "view", mode: "detail", description: "View page", params: [{ name: "page", type: "string", description: "Page to show" }] }
{ name: "view", mode: "detail", description: "View page", params: [{ name: "page", type: "string", description: "Page to show" }] },
{ name: "update", mode: "silent", description: "Update cache" }
]
}'
exit 0
Expand All @@ -37,7 +38,8 @@ if [ "$COMMAND" = "list" ]; then
tldr --list | jq -R '{
title: .,
actions: [
{title: "View Page", type: "run", command: "view", params: {page: .}}
{title: "View Page", type: "run", command: "view", params: {page: .}},
{ title: "Update Cache", type: "run", command: "update" }
]
}' | jq -s '{ items: . }'
elif [ "$COMMAND" = "view" ]; then
Expand All @@ -47,4 +49,9 @@ elif [ "$COMMAND" = "view" ]; then
{title: "Copy Page", type: "copy", text: .}
]
}'
elif [ "$COMMAND" = "update" ]; then
tldr --update
else
echo "Invalid command"
exit 1
fi
5 changes: 4 additions & 1 deletion internal/cli/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewCmdExtension(alias string, extension extensions.Extension) (*cobra.Comma
return err
}

rootList := tui.NewRootList(history, func() ([]sunbeam.ListItem, error) {
rootList := tui.NewHomePage(history, func() ([]sunbeam.ListItem, error) {
return extension.RootItems(), nil
})

Expand Down Expand Up @@ -168,6 +168,9 @@ func runExtension(extension extensions.Extension, command sunbeam.Command, param
return err
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
case sunbeam.CommandModeAction:
output, err := extension.Output(context.Background(), command, params)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ See https://pomdtr.github.io/sunbeam for more information.`,
return err
}

rootList := tui.NewRootList(history, func() ([]sunbeam.ListItem, error) {
rootList := tui.NewHomePage(history, func() ([]sunbeam.ListItem, error) {
exts, err := LoadExtensions(utils.ExtensionsDir(), false)
if err != nil {
return nil, err
Expand Down
32 changes: 12 additions & 20 deletions internal/tui/root.go → internal/tui/home.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package tui

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -18,7 +16,7 @@ import (
"github.com/pomdtr/sunbeam/pkg/sunbeam"
)

type RootList struct {
type HomePage struct {
width, height int
err *Detail
list *List
Expand All @@ -30,18 +28,18 @@ type RootList struct {

type ReloadMsg struct{}

func NewRootList(history history.History, generator func() ([]sunbeam.ListItem, error)) *RootList {
return &RootList{
func NewHomePage(history history.History, generator func() ([]sunbeam.ListItem, error)) *HomePage {
return &HomePage{
history: history,
generator: generator,
}
}

func (c *RootList) Init() tea.Cmd {
func (c *HomePage) Init() tea.Cmd {
return c.Reload()
}

func (c *RootList) Reload() tea.Cmd {
func (c *HomePage) Reload() tea.Cmd {
rootItems, err := c.generator()
if err != nil {
return c.SetError(err)
Expand All @@ -61,15 +59,15 @@ func (c *RootList) Reload() tea.Cmd {
}
}

func (c *RootList) Focus() tea.Cmd {
func (c *HomePage) Focus() tea.Cmd {
return c.list.Focus()
}

func (c *RootList) Blur() tea.Cmd {
func (c *HomePage) Blur() tea.Cmd {
return c.list.SetIsLoading(false)
}

func (c *RootList) SetSize(width, height int) {
func (c *HomePage) SetSize(width, height int) {
c.width, c.height = width, height
if c.err != nil {
c.err.SetSize(width, height)
Expand All @@ -83,15 +81,15 @@ func (c *RootList) SetSize(width, height int) {
}
}

func (c *RootList) SetError(err error) tea.Cmd {
func (c *HomePage) SetError(err error) tea.Cmd {
c.err = NewErrorPage(err)
c.err.SetSize(c.width, c.height)
return func() tea.Msg {
return err
}
}

func (c *RootList) Update(msg tea.Msg) (Page, tea.Cmd) {
func (c *HomePage) Update(msg tea.Msg) (Page, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
Expand Down Expand Up @@ -200,17 +198,11 @@ func (c *RootList) Update(msg tea.Msg) (Page, tea.Cmd) {
return c, PushPageCmd(runner)
case sunbeam.CommandModeSilent:
return c, func() tea.Msg {
output, err := extension.Output(context.Background(), command, params)
_, err := extension.Output(context.Background(), command, params)
if err != nil {
return PushPageMsg{NewErrorPage(err)}
}

if len(output) > 0 {
output = bytes.Trim(output, "\n")
rows := strings.Split(string(output), "\n")
return ShowNotificationMsg{rows[len(rows)-1]}
}

return ExitMsg{}
}

Expand Down Expand Up @@ -280,7 +272,7 @@ func (c *RootList) Update(msg tea.Msg) (Page, tea.Cmd) {
return c, nil
}

func (c *RootList) View() string {
func (c *HomePage) View() string {
if c.err != nil {
return c.err.View()
}
Expand Down
51 changes: 26 additions & 25 deletions internal/tui/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,33 +190,34 @@ func (c *Runner) Update(msg tea.Msg) (Page, tea.Cmd) {

return c, PushPageCmd(runner)
case sunbeam.CommandModeAction:
return c, func() tea.Msg {
output, err := c.extension.Output(context.Background(), command, params)
if err != nil {
return PushPageMsg{NewErrorPage(err)}
}

if len(output) == 0 {
return ReloadMsg{}
}

var action sunbeam.Action
if err := json.Unmarshal(output, &action); err != nil {
return PushPageMsg{NewErrorPage(err)}
}

return action
}
return c, tea.Sequence(c.SetIsLoading(true),
func() tea.Msg {
output, err := c.extension.Output(context.Background(), command, params)
if err != nil {
return PushPageMsg{NewErrorPage(err)}
}

if len(output) == 0 {
return ReloadMsg{}
}

var action sunbeam.Action
if err := json.Unmarshal(output, &action); err != nil {
return PushPageMsg{NewErrorPage(err)}
}

return action
})
case sunbeam.CommandModeSilent:
return c, func() tea.Msg {
_, err := c.extension.Output(context.Background(), command, params)
return c, tea.Sequence(c.SetIsLoading(true),
func() tea.Msg {
_, err := c.extension.Output(context.Background(), command, params)
if err != nil {
return PushPageMsg{NewErrorPage(err)}
}

if err != nil {
return PushPageMsg{NewErrorPage(err)}
}

return ReloadMsg{}
}
return ReloadMsg{}
})
}
case sunbeam.ActionTypeCopy:
return c, func() tea.Msg {
Expand Down
7 changes: 4 additions & 3 deletions internal/tui/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type StatusBar struct {
}

type ShowNotificationMsg struct {
Title string
Reload bool
Message string
}

type HideNotificationMsg struct{}
Expand Down Expand Up @@ -114,11 +115,11 @@ func (p StatusBar) Update(msg tea.Msg) (StatusBar, tea.Cmd) {
}
case ShowNotificationMsg:
p.Reset()
if msg.Title == "" {
if msg.Message == "" {
return p, nil
}

p.notification = msg.Title
p.notification = msg.Message
return p, tea.Tick(1*time.Second, func(t time.Time) tea.Msg {
return HideNotificationMsg{}
})
Expand Down

0 comments on commit 0cb37e1

Please sign in to comment.