Skip to content
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

feat: render list inside a viewport #228

Merged
merged 7 commits into from
Apr 2, 2024
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
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/adrg/xdg v0.4.0
github.com/atotto/clipboard v0.1.4
github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7
github.com/caarlos0/env/v9 v9.0.0
github.com/caarlos0/timea.go v1.2.0
Expand All @@ -23,14 +24,12 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
golang.org/x/term v0.16.0
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.26.0
)

require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/catppuccin/go v0.2.0 // indirect
Expand All @@ -52,13 +51,14 @@ require (
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/yuin/goldmark v1.6.0 // indirect
github.com/yuin/goldmark-emoji v1.0.2 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -139,8 +139,8 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
Expand Down
52 changes: 33 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"slices"
"strings"

"github.com/atotto/clipboard"
timeago "github.com/caarlos0/timea.go"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/editor"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
"golang.org/x/term"
)

// Build vars.
Expand Down Expand Up @@ -173,7 +173,7 @@
}

if config.ShowHelp {
//nolint: wrapcheck

Check failure on line 176 in main.go

View workflow job for this annotation

GitHub Actions / lint-soft

directive `//nolint: wrapcheck` is unused for linter "wrapcheck" (nolintlint)
return cmd.Usage()
}

Expand Down Expand Up @@ -478,28 +478,42 @@
return nil
}

func makeOptions(conversations []Conversation) []huh.Option[string] {
opts := make([]huh.Option[string], 0, len(conversations))
for _, c := range conversations {
timea := stdoutStyles().Timeago.Render(timeago.Of(c.UpdatedAt))
left := stdoutStyles().SHA1.Render(c.ID[:sha1short])
right := stdoutStyles().ConversationList.Render(c.Title, timea)
opts = append(opts, huh.NewOption(left+" "+right, c.ID))
}
return opts
}

func printList(conversations []Conversation) {
width := 80
if isOutputTTY() {
if w, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil {
width = w
if isOutputTTY() && isInputTTY() {
var selected string
if err := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Conversations").
Value(&selected).
Options(makeOptions(conversations)...),
),
).Run(); err != nil {
if !errors.Is(err, huh.ErrUserAborted) {
fmt.Fprintln(os.Stderr, err.Error())
}
return
}

_ = clipboard.WriteAll(selected)
termenv.Copy(selected)
printConfirmation("COPIED", selected)

return
}

for _, conversation := range conversations {
if isOutputTTY() {
timea := stdoutStyles().Timeago.Render(timeago.Of(conversation.UpdatedAt))
left := stdoutStyles().Bullet.String() + stdoutStyles().SHA1.Render(conversation.ID[:sha1short])
right := stdoutStyles().ConversationList.
Width(width-lipgloss.Width(left)).
Render(conversation.Title, timea)
fmt.Fprint(
os.Stdout,
lipgloss.JoinHorizontal(lipgloss.Top, left, right),
"\n",
)
continue
}
fmt.Fprintf(
os.Stdout,
"%s\t%s\t%s\n",
Expand Down
23 changes: 20 additions & 3 deletions styles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "github.com/charmbracelet/lipgloss"
import (
"fmt"
"strings"

"github.com/charmbracelet/lipgloss"
)

type styles struct {
AppName,
Expand All @@ -19,7 +24,6 @@ type styles struct {
Quote,
ConversationList,
SHA1,
Bullet,
Timeago lipgloss.Style
}

Expand All @@ -41,7 +45,20 @@ func makeStyles(r *lipgloss.Renderer) (s styles) {
s.Pipe = r.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#8470FF", Dark: "#745CFF"})
s.ConversationList = r.NewStyle().Padding(0, 1)
s.SHA1 = s.Flag.Copy()
s.Bullet = r.NewStyle().SetString("• ").Foreground(lipgloss.AdaptiveColor{Light: "#757575", Dark: "#777"})
s.Timeago = r.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#999", Dark: "#555"})
return s
}

// action messages

const defaultAction = "WROTE"

var outputHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#6C50FF")).Bold(true).Padding(0, 1).MarginRight(1)

func printConfirmation(action, content string) {
if action == "" {
action = defaultAction
}
outputHeader = outputHeader.SetString(strings.ToUpper(action))
fmt.Println(lipgloss.JoinHorizontal(lipgloss.Center, outputHeader.String(), content))
}
Loading