Skip to content

Commit 71b72bd

Browse files
committed
fractional update intervals
1 parent b6e3ab5 commit 71b72bd

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

cmd/ssh_dashboard/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ import (
1111
tea "github.com/charmbracelet/bubbletea"
1212
)
1313

14-
func validateInterval(seconds int) time.Duration {
15-
if seconds < 1 || seconds > 3600 {
14+
func validateInterval(seconds float64) time.Duration {
15+
if seconds < 0.01 || seconds > 3600 {
1616
return 0
1717
}
18-
return time.Duration(seconds) * time.Second
18+
return time.Duration(seconds * float64(time.Second))
1919
}
2020

2121
func main() {
2222
flag.Usage = func() {
2323
// HACK: make it look like python's argparse
2424
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS]\n\n", os.Args[0])
2525
fmt.Fprintf(os.Stderr, "Options:\n")
26-
fmt.Fprintf(os.Stderr, " -n, --interval int Update interval in seconds (default: 5, or SSH_DASHBOARD_INTERVAL env var)\n")
26+
fmt.Fprintf(os.Stderr, " -n, --interval float Update interval in seconds (default: 5, or SSH_DASHBOARD_INTERVAL env var)\n")
2727
fmt.Fprintf(os.Stderr, " -h, --help Show this help message\n")
2828
}
2929

30-
var updateIntervalVal int
31-
flag.IntVar(&updateIntervalVal, "n", 0, "")
32-
flag.IntVar(&updateIntervalVal, "interval", 0, "")
30+
var updateIntervalVal float64
31+
flag.Float64Var(&updateIntervalVal, "n", 0, "")
32+
flag.Float64Var(&updateIntervalVal, "interval", 0, "")
3333
flag.Parse()
3434
updateInterval := &updateIntervalVal
3535

@@ -40,7 +40,7 @@ func main() {
4040
interval = validated
4141
}
4242
} else if envInterval := os.Getenv("SSH_DASHBOARD_INTERVAL"); envInterval != "" {
43-
if seconds, err := strconv.Atoi(envInterval); err == nil {
43+
if seconds, err := strconv.ParseFloat(envInterval, 64); err == nil {
4444
if validated := validateInterval(seconds); validated > 0 {
4545
interval = validated
4646
}

internal/ui.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ func censorHostname(hostname string) string {
103103
return hostname[:3] + strings.Repeat("*", 5) + hostname[len(hostname)-3:]
104104
}
105105

106+
func formatInterval(interval time.Duration) string {
107+
seconds := interval.Seconds()
108+
if seconds < 1 {
109+
return fmt.Sprintf("%.2fs", seconds)
110+
} else if seconds < 10 {
111+
return fmt.Sprintf("%.1fs", seconds)
112+
}
113+
return fmt.Sprintf("%.0fs", seconds)
114+
}
115+
106116
func InitialModel(hosts []SSHHost, updateInterval time.Duration) Model {
107117
items := make([]list.Item, len(hosts))
108118
for i, h := range hosts {
@@ -502,8 +512,8 @@ func (m Model) renderOverview() string {
502512
var b strings.Builder
503513

504514
title := fmt.Sprintf(" Overview - All Hosts (%d) ", len(m.selectedHosts))
505-
subtitle := fmt.Sprintf("Last Updated: %s | Interval: %.0fs | 't' per-host | 'c' add hosts | 'q' quit",
506-
time.Now().Format("15:04:05"), m.updateInterval.Seconds())
515+
subtitle := fmt.Sprintf("Last Updated: %s | Interval: %s | 't' per-host | 'c' add hosts | 'q' quit",
516+
time.Now().Format("15:04:05"), formatInterval(m.updateInterval))
507517

508518
b.WriteString(titleStyle.Render(title))
509519
b.WriteString("\n")
@@ -615,8 +625,8 @@ func renderDashboard(hostName string, info *SystemInfo, updateInterval time.Dura
615625
if multiHost {
616626
navHint = " | 'n' next | 't' overview"
617627
}
618-
subtitle := fmt.Sprintf("Last Updated: %s | Interval: %.0fs%s | 'c' add hosts | 'q' quit",
619-
lastUpdate.Format("15:04:05"), updateInterval.Seconds(), navHint)
628+
subtitle := fmt.Sprintf("Last Updated: %s | Interval: %s%s | 'c' add hosts | 'q' quit",
629+
lastUpdate.Format("15:04:05"), formatInterval(updateInterval), navHint)
620630

621631
b.WriteString(titleStyle.Render(title))
622632
b.WriteString("\n")

0 commit comments

Comments
 (0)