Skip to content

Commit

Permalink
Add running timer for current command
Browse files Browse the repository at this point in the history
  • Loading branch information
DomBlack committed Aug 24, 2023
1 parent bd177ee commit 9947234
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/tui/history/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,20 @@ func (i Item) View(cfg *config.Config, width int) string {
if finished.IsZero() && i.Status == RunningStatus {
finished = time.Now()
}
if !i.Finished.IsZero() {
timeStr = fmt.Sprintf("(%dms) %s", i.Finished.Sub(i.Started).Milliseconds(), timeStr)
if !finished.IsZero() {
dur := finished.Sub(i.Started)
switch {
case dur < 1*time.Second:
timeStr = fmt.Sprintf("(%dms) %s", dur.Milliseconds(), timeStr)
case dur < 10*time.Second:
timeStr = fmt.Sprintf("(%.1fs) %s", dur.Seconds(), timeStr)
case dur < 1*time.Minute:
timeStr = fmt.Sprintf("(%.0fs) %s", dur.Seconds(), timeStr)
case dur < 2*time.Minute:
timeStr = fmt.Sprintf("(%.1fm) %s", dur.Minutes(), timeStr)
default:
timeStr = fmt.Sprintf("(%.0fm) %s", dur.Minutes(), timeStr)
}
}

// Render the input line
Expand Down
40 changes: 40 additions & 0 deletions pkg/tui/history/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package history

import (
"strings"
"time"

"github.com/DomBlack/bubble-shell/internal/config"
. "github.com/DomBlack/bubble-shell/pkg/modelid"
Expand Down Expand Up @@ -71,6 +72,14 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, tea.Batch(
msg.Item.Init(),
m.SaveHistory(m.Items),
func() tea.Msg {
// start tick messages for the new item, so that we get refreshed
// while the item is running
return tickMsg{
ID: m.id,
ItemID: msg.Item.ID,
}
},
)
}

Expand Down Expand Up @@ -127,6 +136,28 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}
}
}

case tickMsg:
if m.id.Matches(msg) && len(m.Items) > 0 {
// Tick messages are so we can update the timer when the item is running
lastItem := m.Items[len(m.Items)-1]
if lastItem.ID == msg.ItemID && lastItem.Status == RunningStatus {
dur := 10 * time.Millisecond
if time.Since(lastItem.Started).Seconds() > 1 {
dur = 100 * time.Millisecond
} else if time.Since(lastItem.Started).Seconds() > 10 {
// under 10 seconds
dur = time.Second
}

return m, tea.Tick(dur, func(t time.Time) tea.Msg {
return tickMsg{
ID: m.id,
ItemID: msg.ItemID,
}
})
}
}
}

return m, nil
Expand Down Expand Up @@ -296,3 +327,12 @@ type streamOutput struct {
func (msg streamOutput) ForModelID() ID {
return msg.ID
}

type tickMsg struct {
ID ID
ItemID xid.ID
}

func (msg tickMsg) ForModelID() ID {
return msg.ID
}

0 comments on commit 9947234

Please sign in to comment.