-
Notifications
You must be signed in to change notification settings - Fork 443
feat: Adding --show-elapsed flag in spin command #953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,13 @@ package spin | |
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "runtime" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/charmbracelet/bubbles/spinner" | ||
| tea "github.com/charmbracelet/bubbletea" | ||
|
|
@@ -30,20 +32,22 @@ import ( | |
| ) | ||
|
|
||
| type model struct { | ||
| spinner spinner.Model | ||
| title string | ||
| align string | ||
| command []string | ||
| quitting bool | ||
| isTTY bool | ||
| status int | ||
| stdout string | ||
| stderr string | ||
| output string | ||
| showStdout bool | ||
| showStderr bool | ||
| showError bool | ||
| err error | ||
| spinner spinner.Model | ||
| title string | ||
| align string | ||
| command []string | ||
| quitting bool | ||
| isTTY bool | ||
| status int | ||
| stdout string | ||
| stderr string | ||
| output string | ||
| showStdout bool | ||
| showStderr bool | ||
| showError bool | ||
| showElapsed bool | ||
| err error | ||
| started time.Time | ||
| } | ||
|
|
||
| var ( | ||
|
|
@@ -56,6 +60,10 @@ var ( | |
|
|
||
| type errorMsg error | ||
|
|
||
| type startMsg struct { | ||
| started time.Time | ||
| } | ||
|
|
||
| type finishCommandMsg struct { | ||
| stdout string | ||
| stderr string | ||
|
|
@@ -137,10 +145,31 @@ func commandAbort() tea.Msg { | |
| return tea.InterruptMsg{} | ||
| } | ||
|
|
||
| func formatDuration(d time.Duration) string { | ||
| days := int(d.Hours()) / 24 | ||
| hours := int(d.Hours()) % 24 | ||
| minutes := int(d.Minutes()) % 60 | ||
| seconds := int(d.Seconds()) % 60 | ||
|
|
||
| if days > 0 { | ||
| return fmt.Sprintf("(%dd %dh %dm)", days, hours, minutes) | ||
| } else if hours > 0 { | ||
| return fmt.Sprintf("(%dh %dm)", hours, minutes) | ||
| } else if minutes > 0 { | ||
| return fmt.Sprintf("(%dm %ds)", minutes, seconds) | ||
| } | ||
| return fmt.Sprintf("(%ds)", seconds) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this provide enough additional value over plain fmt.Sprintf("duration: %s", d)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean showing only the seconds? or formatting time.Now()? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I mean formatting the duration. I realize we should round, but this: var start = time.Now()
for true {
time.Sleep(100 * time.Millisecond)
fmt.Printf("%s\n", time.Since(start).Round(100*time.Millisecond))
}creates output like Which is basically what you propose, sans the spaces. Hence my question, if you think the visual difference justifies the additional code. (Not my call, just a thought.)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not aware of this format specifier. |
||
|
|
||
| func (m model) Init() tea.Cmd { | ||
| return tea.Batch( | ||
| m.spinner.Tick, | ||
| commandStart(m.command), | ||
| func() tea.Msg { | ||
| return startMsg{ | ||
| started: time.Now(), | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -161,17 +190,25 @@ func (m model) View() string { | |
| return m.title | ||
| } | ||
|
|
||
| elapsed := "" | ||
| if m.showElapsed { | ||
| elapsed = formatDuration(time.Since(m.started)) | ||
| } | ||
|
|
||
| var header string | ||
| if m.align == "left" { | ||
| header = m.spinner.View() + " " + m.title | ||
| } else { | ||
| header = m.title + " " + m.spinner.View() | ||
| } | ||
| return header + "\n" + out | ||
| return header + elapsed + "\n" + out | ||
NucleoFusion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| switch msg := msg.(type) { | ||
| case startMsg: | ||
| m.started = msg.started | ||
| return m, nil | ||
| case finishCommandMsg: | ||
| m.stdout = msg.stdout | ||
| m.stderr = msg.stderr | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.