From 84942b8fc75cec1dd825b92ee94f00f39ef34bb4 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 20 Sep 2023 15:39:26 -0400 Subject: [PATCH] chore: wordwrap conversation listing output --- go.mod | 4 ++-- go.sum | 8 ++++---- main.go | 23 +++++++++++++++++------ styles.go | 4 +++- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index daadc244..85978907 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 + golang.org/x/term v0.12.0 gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.25.0 ) @@ -50,8 +51,7 @@ require ( golang.org/x/mod v0.11.0 // indirect golang.org/x/net v0.12.0 // indirect golang.org/x/sync v0.3.0 // indirect - golang.org/x/sys v0.10.0 // indirect - golang.org/x/term v0.10.0 // indirect + golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.11.0 // indirect golang.org/x/tools v0.6.0 // indirect lukechampine.com/uint128 v1.2.0 // indirect diff --git a/go.sum b/go.sum index 387d7e83..2846271e 100644 --- a/go.sum +++ b/go.sum @@ -952,14 +952,14 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= -golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= -golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= +golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/main.go b/main.go index 9147429b..cd3436bf 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,9 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/glamour" "github.com/charmbracelet/glow/editor" + "github.com/charmbracelet/lipgloss" "github.com/spf13/cobra" + "golang.org/x/term" ) // Build vars. @@ -344,15 +346,24 @@ func listConversations() error { return nil } + var width = 80 + if isOutputTTY() { + if w, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil { + width = w + } + } + for _, conversation := range conversations { if isOutputTTY() { - fmt.Fprintf( + 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, - "%s %s %s %s\n", - stdoutStyles().Bullet, - stdoutStyles().SHA1.Render(conversation.ID[:sha1short]), - conversation.Title, - stdoutStyles().Timeago.Render(timeago.Of(conversation.UpdatedAt)), + lipgloss.JoinHorizontal(lipgloss.Top, left, right), + "\n", ) continue } diff --git a/styles.go b/styles.go index eba6dc37..fbd318b2 100644 --- a/styles.go +++ b/styles.go @@ -17,6 +17,7 @@ type styles struct { Link, Pipe, Quote, + ConversationList, SHA1, Bullet, Timeago lipgloss.Style @@ -38,8 +39,9 @@ func makeStyles(r *lipgloss.Renderer) (s styles) { s.Link = r.NewStyle().Foreground(lipgloss.Color("#00AF87")).Underline(true) s.Quote = r.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#FF71D0", Dark: "#FF78D2"}) s.Pipe = r.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#8470FF", Dark: "#745CFF"}) + s.ConversationList = r.NewStyle().Padding(0, 1) s.SHA1 = r.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#00B594", Dark: "#11d4b1"}).Bold(true) - s.Bullet = r.NewStyle().SetString("•").Foreground(lipgloss.AdaptiveColor{Light: "#757575", Dark: "#777"}) + 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 }