Skip to content

Commit

Permalink
✨ properly updating for CI
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed May 6, 2024
1 parent 0535439 commit 0713d12
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/multi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
Title: "Paint the house",
Task: func(t *taskin.Task) error {
for i := 0; i < 3; i++ {
t.Progress(i+1, 5)
t.Progress(i+1, 3)
t.Title = fmt.Sprintf("Paint the house: [%d/3] walls painted", i+1)
time.Sleep(500 * time.Millisecond)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
Task: func(t *taskin.Task) error {
for i := 0; i < 3; i++ {
t.Title = fmt.Sprintf("Task 1: [%d/3] seconds have passed", i+1)
time.Sleep(1 * time.Second)
time.Sleep(500 * time.Millisecond)
}
return nil
},
Expand All @@ -24,7 +24,7 @@ func main() {
Task: func(t *taskin.Task) error {
for i := 0; i < 3; i++ {
t.Title = fmt.Sprintf("Task 2: [%d/3] seconds have passed", i+1)
time.Sleep(1 * time.Second)
time.Sleep(500 * time.Millisecond)
}
return fmt.Errorf("task 2 failed")
},
Expand Down
2 changes: 1 addition & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Tasks []Task
type Runner struct {
Task Task
State TaskState
Spinner spinner.Model
Spinner *spinner.Model
Config Config
Children Runners
}
Expand Down
Binary file modified multi.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 37 additions & 12 deletions mvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"os"
)

func (r *Runners) Init() tea.Cmd {
var cmds []tea.Cmd
for i := range *r {
cmds = append(cmds, (*r)[i].Spinner.Tick)
if (*r)[i].Spinner != nil {
cmds = append(cmds, (*r)[i].Spinner.Tick)
}
for j := range (*r)[i].Children {
cmds = append(cmds, (*r)[i].Children[j].Spinner.Tick)
if (*r)[i].Children[j].Spinner != nil {
cmds = append(cmds, (*r)[i].Children[j].Spinner.Tick)
}
}
}
return tea.Batch(cmds...)
Expand All @@ -27,16 +32,20 @@ func (r *Runners) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for i := range *r {

if (*r)[i].State == Running || (*r)[i].State == NotStarted {
newSpinner, cmd := (*r)[i].Spinner.Update(msg)
(*r)[i].Spinner = newSpinner
cmds = append(cmds, cmd)
if os.Getenv("CI") == "" {
newSpinner, cmd := (*r)[i].Spinner.Update(msg)
(*r)[i].Spinner = &newSpinner
cmds = append(cmds, cmd)
}
}

for j := range (*r)[i].Children {
if (*r)[i].Children[j].State == Running || (*r)[i].Children[j].State == NotStarted {
newSpinner, cmd := (*r)[i].Children[j].Spinner.Update(msg)
(*r)[i].Children[j].Spinner = newSpinner
cmds = append(cmds, cmd)
if os.Getenv("CI") == "" {
newSpinner, cmd := (*r)[i].Children[j].Spinner.Update(msg)
(*r)[i].Children[j].Spinner = &newSpinner
cmds = append(cmds, cmd)
}
}
}

Expand Down Expand Up @@ -72,9 +81,17 @@ func (r *Runners) View() string {
} else {
if runner.Task.ShowProgress.Total != 0 {
percent := float64(runner.Task.ShowProgress.Current) / float64(runner.Task.ShowProgress.Total)
status = runner.Spinner.View() + runner.Task.Title + " " + runner.Task.Bar.ViewAs(percent)
if runner.Spinner != nil {
status = runner.Spinner.View() + runner.Task.Title + " " + runner.Task.Bar.ViewAs(percent)
} else {
status = "⣟ " + runner.Task.Title + " " + runner.Task.Bar.ViewAs(percent)
}
} else {
status = runner.Spinner.View() + runner.Task.Title
if runner.Spinner != nil {
status = runner.Spinner.View() + runner.Task.Title
} else {
status = "⣟ " + runner.Task.Title
}
}
}
case Completed:
Expand All @@ -92,9 +109,17 @@ func (r *Runners) View() string {
case Running:
if child.Task.ShowProgress.Total != 0 {
percent := float64(child.Task.ShowProgress.Current) / float64(child.Task.ShowProgress.Total)
status = child.Spinner.View() + child.Task.Title + " " + child.Task.Bar.ViewAs(percent)
if child.Spinner == nil {
status = "⣟ " + child.Task.Title + " " + child.Task.Bar.ViewAs(percent)
} else {
status = child.Spinner.View() + child.Task.Title + " " + child.Task.Bar.ViewAs(percent)
}
} else {
status = child.Spinner.View() + child.Task.Title
if child.Spinner == nil {
status = "⣟ " + child.Task.Title
} else {
status = child.Spinner.View() + child.Task.Title
}
}
case Completed:
status = lipgloss.NewStyle().Foreground(child.Config.Colors.Success).Render("✔") + " " + child.Task.Title // Green checkmark
Expand Down
29 changes: 22 additions & 7 deletions taskin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"os"
)

var program *tea.Program

func NewRunner(task Task, cfg Config) Runner {
s := spinner.New(spinner.WithSpinner(cfg.Spinner)) // Initialize with a spinner model
s.Style = lipgloss.NewStyle().Foreground(cfg.Colors.Spinner) // Styling spinner

var spinr *spinner.Model

if os.Getenv("CI") == "" {
spinnerModel := spinner.New(spinner.WithSpinner(cfg.Spinner)) // Initialize with a spinner model
spinnerModel.Style = lipgloss.NewStyle().Foreground(cfg.Colors.Spinner) // Styling spinner
spinr = &spinnerModel
}

children := make(Runners, len(task.Tasks))
for i, childTask := range task.Tasks {
childTask.Config = cfg
Expand All @@ -22,7 +32,7 @@ func NewRunner(task Task, cfg Config) Runner {
return nil
}
}
return Runner{Task: task, State: NotStarted, Spinner: s, Config: cfg, Children: children}
return Runner{Task: task, State: NotStarted, Spinner: spinr, Config: cfg, Children: children}
}

func (task *Task) Progress(current, total int) {
Expand All @@ -37,20 +47,21 @@ func (task *Task) Progress(current, total int) {
}

func (r *Runners) Run() error {
p := tea.NewProgram(r, tea.WithInput(nil))
_, err := p.Run()
program = tea.NewProgram(r, tea.WithInput(nil))
_, err := program.Run()

program.Send(spinner.TickMsg{})
return err
}

func New(tasks Tasks, cfg Config) Runners {
// merge cfg with Defaults
_ = mergo.Merge(&cfg, Defaults)
var runners Runners
for _, task := range tasks {
// Use NewRunner to ensure runners are initialized with spinners correctly
task.Config = cfg
runners = append(runners, NewRunner(task, cfg))
}

go func() {
for i := range runners {

Expand All @@ -65,6 +76,7 @@ func New(tasks Tasks, cfg Config) Runners {
if err != nil {
runners[i].Task.Title = fmt.Sprintf("%s - Error: %s", runners[i].Task.Title, err.Error())
runners[i].State = Failed
program.Send(spinner.TickMsg{})
continue
}

Expand All @@ -76,6 +88,7 @@ func New(tasks Tasks, cfg Config) Runners {
runners[i].Children[j].Task.Title = fmt.Sprintf("%s - Error: %s", runners[i].Children[j].Task.Title, err.Error())
runners[i].Children[j].State = Failed
runners[i].State = Failed // Mark parent task as Failed
program.Send(spinner.TickMsg{})
break
}
runners[i].Children[j].State = Completed
Expand All @@ -86,6 +99,7 @@ func New(tasks Tasks, cfg Config) Runners {
for _, child := range runners[i].Children {
if child.State != Completed {
allChildrenCompleted = false
program.Send(spinner.TickMsg{})
break
}
}
Expand All @@ -94,6 +108,7 @@ func New(tasks Tasks, cfg Config) Runners {
if allChildrenCompleted && runners[i].State != Failed {
runners[i].State = Completed
}
program.Send(spinner.TickMsg{})
}
}()
return runners
Expand Down

0 comments on commit 0713d12

Please sign in to comment.