Skip to content

Commit

Permalink
twitter: add tui
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 27, 2023
1 parent 17a6513 commit 77d2300
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
31 changes: 18 additions & 13 deletions cmd/twitterview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ import (
"os"
"time"

"github.com/azimut/cli-view/internal/tui"
"github.com/azimut/cli-view/internal/twitter"
)

type options struct {
timeout time.Duration
userAgent string
useColors bool
usePretty bool
width int
useTUI bool
userAgent string
}

var opt options
var url string
var opts options

func init() {
flag.DurationVar(&opt.timeout, "t", time.Second*5, "timeout in seconds")
flag.StringVar(&opt.userAgent, "A", "Twitter_View/0.1", "default User-Agent sent")
flag.BoolVar(&opt.useColors, "C", true, "use colors")
flag.BoolVar(&opt.usePretty, "P", true, "use pretty formatting")
flag.IntVar(&opt.width, "w", 0, "fixed with, defaults to console width")
flag.BoolVar(&opts.useColors, "C", true, "use colors")
flag.BoolVar(&opts.useTUI, "x", false, "use TUI")
flag.DurationVar(&opts.timeout, "t", time.Second*5, "timeout in seconds")
flag.StringVar(&opts.userAgent, "A", "Twitter_View/0.1", "default User-Agent sent")
}

func usage() {
Expand All @@ -43,12 +41,19 @@ func run(args []string, stdout io.Writer) error {
flag.Usage()
return errors.New("missing URL argument")
}
url = flag.Args()[0]
res, err := twitter.Fetch(url, opt.userAgent, opt.timeout)

url := flag.Args()[0]
tweet, err := twitter.Fetch(url, opts.userAgent, opts.timeout)
if err != nil {
return err
}
fmt.Println(twitter.Format(res))

if opts.useTUI {
tui.RenderLoop(twitter.NewProgram(*tweet))
} else {
fmt.Println(tweet)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/caser/gophernews v0.0.0-20150107061403-f17255d5b792
github.com/charmbracelet/bubbles v0.15.0
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.6.0
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.15.0
github.com/gomarkdown/markdown v0.0.0-20230322041520-c84983bdbf2a
Expand All @@ -24,7 +25,6 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/charmbracelet/lipgloss v0.6.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions internal/twitter/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ func Fetch(tweetUrl, ua string, timeout time.Duration) (*Embedded, error) {
if err != nil {
return nil, err
}

rawJson, err := fetch.Fetch(embedUrl, ua, timeout)
if err != nil {
return nil, err
}

tweet, err := toEmbedded(rawJson)
if err != nil {
return nil, err
}

return tweet, nil
}

Expand Down
10 changes: 5 additions & 5 deletions internal/twitter/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"github.com/jaytaylor/html2text"
)

func Format(t *Embedded) (formatted string) {
func (t Embedded) String() (ret string) {
msg, links := paragraph(t.Html)
formatted += fmt.Sprintf("URL: %s\n", t.Url)
ret += fmt.Sprintf("URL: %s\n", t.Url)
for _, link := range links {
formatted += formatLink(link)
ret += formatLink(link)
}
formatted += "\n" + fitInScreen(plaintext(msg))
formatted += "\n\n" + date(t.Html)
ret += "\n" + fitInScreen(plaintext(msg))
ret += "\n\n" + date(t.Html)
return
}

Expand Down
39 changes: 39 additions & 0 deletions internal/twitter/tui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package twitter

import (
"fmt"

"github.com/azimut/cli-view/internal/tui"
tea "github.com/charmbracelet/bubbletea"
)

const rightPadding = 10

type Model struct {
render tui.Model
Embedded
}

func NewProgram(thread Embedded) *tea.Program {
return tea.NewProgram(Model{Embedded: thread},
tea.WithAltScreen())
}

func (m Model) Init() tea.Cmd {
return nil
}

func (m Model) View() string {
return m.render.View()
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.render, cmd = m.render.Update(msg)
switch msg.(type) {
case tea.WindowSizeMsg:
m.render.RawContent = fmt.Sprint(m)
m.render.Viewport.SetContent(fmt.Sprint(m))
}
return m, cmd
}

0 comments on commit 77d2300

Please sign in to comment.