Skip to content

Commit

Permalink
feat: working on spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
Selyss committed Oct 27, 2023
1 parent 93515ad commit e0dcfaa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 72 deletions.
80 changes: 74 additions & 6 deletions pkg/chtsht/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,68 @@ package chtsht

import (
"bufio"
"fmt"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"log"
"os"
"os/exec"
"strings"
)

type errMsg error

type model struct {
spinner spinner.Model
quitting bool
err error
}

func initialModel() model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return model{spinner: s}
}

func (m model) Init() tea.Cmd {
return m.spinner.Tick
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
default:
return m, nil
}

case errMsg:
m.err = msg
return m, nil

default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}

func (m model) View() string {
if m.err != nil {
return m.err.Error()
}
str := fmt.Sprintf("\n %s Loading information...press q to quit\n", m.spinner.View())
if m.quitting {
return str + "\n"
}
return str
}

func SelectFromList(items []string) (string, error) {

cmd := exec.Command("fzf")
Expand All @@ -22,23 +78,35 @@ func SelectFromList(items []string) (string, error) {
}

func DisplayOutput(url string) {
p := tea.NewProgram(initialModel())

cmd := exec.Command("curl", "-s", url)
cmd.Stderr = os.Stderr

lessCmd := exec.Command("less")
lessCmd.Stdin, _ = cmd.StdoutPipe()
lessCmd.Stdout = os.Stdout

if err := cmd.Start(); err != nil {
go func() {
if _, err := p.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
if err := cmd.Run(); err != nil {
log.Fatalf("Error while querying: %s", err)
}

if err := lessCmd.Run(); err != nil {
log.Fatalf("Error while piping into $PAGER: %s", err)
}
go func() {
if err := lessCmd.Run(); err != nil {
log.Fatalf("Error while piping into $PAGER: %s", err)
}
p.Quit()
}()

cmd.Wait()
return
if err := cmd.Run(); err != nil {
log.Fatalf("Error while querying: %s", err)
}
}

func ChtReadOptions() ([]string, error) {
Expand Down
66 changes: 0 additions & 66 deletions pkg/chtsht/spinner.go

This file was deleted.

0 comments on commit e0dcfaa

Please sign in to comment.