Skip to content

Commit c8ba5a9

Browse files
committed
Oh fancy TUI
1 parent 280501d commit c8ba5a9

5 files changed

Lines changed: 449 additions & 88 deletions

File tree

cmd/meshexec/tui.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,53 @@ var tuiCmd = &cobra.Command{
4242
if logger != nil {
4343
logger.Info("tui: mDNS discovery", nil)
4444
}
45+
// enable discovery logging
46+
discovery.SetLogger(logger)
4547

46-
// Subscribe to peer updates and push snapshots into the UI
47-
subCtx, subCancel := context.WithCancel(ctx)
48-
defer subCancel()
48+
// Subscribe to peer updates and push snapshots into the UI, sequentially
4949
go func() {
50-
ticker := time.NewTicker(2 * time.Second)
51-
defer ticker.Stop()
50+
interval := 2 * time.Second
5251
for {
5352
select {
54-
case <-subCtx.Done():
53+
case <-ctx.Done():
5554
return
56-
case <-ticker.C:
57-
c, cc := context.WithTimeout(context.Background(), 1500*time.Millisecond)
58-
peers, _ := discovery.Discover(c, 1500*time.Millisecond)
59-
cc()
60-
ui.UpdatePeers(peers)
55+
default:
56+
}
57+
start := time.Now()
58+
c, cc := context.WithTimeout(ctx, 4*time.Second)
59+
peers, err := discovery.Discover(c, 3500*time.Millisecond)
60+
cc()
61+
if err != nil && logger != nil {
62+
logger.Debug("tui: discovery error", map[string]interface{}{"error": err.Error()})
63+
}
64+
ui.UpdatePeers(peers)
65+
// maintain roughly the desired interval
66+
elapsed := time.Since(start)
67+
if remaining := interval - elapsed; remaining > 0 {
68+
select {
69+
case <-ctx.Done():
70+
return
71+
case <-time.After(remaining):
72+
}
6173
}
6274
}
6375
}()
6476

6577
// Start TUI
66-
return ui.StartTUI(ctx, tui.WithInitialView(tuiView))
78+
return ui.StartTUI(ctx, tui.WithInitialView(tuiView), tui.WithTheme(tuiTheme), tui.WithEmoji(!tuiNoEmoji))
6779
},
6880
}
6981

7082
func init() {
7183
rootCmd.AddCommand(tuiCmd)
7284
tuiCmd.Flags().StringVar(&tuiView, "view", "overview", "initial view: peers|results|overview")
85+
tuiCmd.Flags().StringVar(&tuiTheme, "theme", "dark", "theme: dark|light|hc")
86+
tuiCmd.Flags().BoolVar(&tuiNoEmoji, "no-emoji", false, "disable emoji/icons in the TUI")
7387
// removed allow-sim; BLE disabled
7488
}
7589

7690
var tuiView string
91+
var tuiTheme string
92+
var tuiNoEmoji bool
7793

7894
// BLE simulation flag removed

internal/tui/manager.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ func NewManager(logger *logging.Logger) *Manager {
2525
// Options for starting the TUI
2626
type options struct {
2727
initialView string
28+
themeName string
29+
useEmoji bool
2830
}
2931

3032
type Option func(*options)
3133

32-
func defaultOptions() options { return options{initialView: ""} }
34+
func defaultOptions() options { return options{initialView: "", themeName: "dark", useEmoji: true} }
3335

3436
// WithInitialView sets the initial view key (e.g., "overview", "peers", "results")
3537
func WithInitialView(view string) Option { return func(o *options) { o.initialView = view } }
3638

39+
// WithTheme selects a theme variant: "dark" (default), "light", or "hc" (high-contrast)
40+
func WithTheme(name string) Option { return func(o *options) { o.themeName = name } }
41+
42+
// WithEmoji toggles emoji/micro-icons usage
43+
func WithEmoji(enabled bool) Option { return func(o *options) { o.useEmoji = enabled } }
44+
3745
// StartTUI launches the Bubble Tea program and blocks until it exits
3846
func (m *Manager) StartTUI(ctx context.Context, opts ...Option) error {
3947
cfg := defaultOptions()
@@ -54,11 +62,12 @@ func (m *Manager) StartTUI(ctx context.Context, opts ...Option) error {
5462
}
5563
}
5664

65+
th := themeFor(cfg.themeName)
5766
var model model
5867
if cfg.initialView != "" {
59-
model = newModelWithInitialView(m.logger, cfg.initialView)
68+
model = newModelWithInitialView(m.logger, th, cfg.useEmoji, cfg.initialView)
6069
} else {
61-
model = newModel(m.logger)
70+
model = newModel(m.logger, th, cfg.useEmoji)
6271
}
6372
m.mu.Lock()
6473
m.program = tea.NewProgram(model, tea.WithContext(ctx))

0 commit comments

Comments
 (0)