telegram_dialog.go RawHistoryFolder
1package modes
2
3import (
4 "github.com/bnema/zut/packages/tui"
5)
6
7// telegramDialog is the picker shown when the user runs `/telegram`
8// without an argument. Lists the three actions (connect, disconnect,
9// status) and routes the choice back to the Interactive via
10// telegramAction.
11//
12// Shape mirrors logoutDialog: a tiny list of rows with the cursor
13// moved by arrow keys and enter to pick, esc to cancel.
14type telegramDialog struct {
15 active bool
16 items []telegramItem
17 cursor int
18}
19
20type telegramItem struct {
21 label string
22 action string // "connect" | "disconnect" | "status"
23 hint string // muted text shown after the label (e.g. "not configured", "active")
24}
25
26type telegramAction struct {
27 Select bool
28 Action string
29 Close bool
30}
31
32func newTelegramDialog() *telegramDialog { return &telegramDialog{} }
33
34// Open shows the picker with items describing the current state.
35// items is rendered in order; the caller builds it so connect is
36// only offered when disconnected, and vice versa.
37func (d *telegramDialog) Open(items []telegramItem) bool {
38 if len(items) == 0 {
39 return false
40 }
41 d.items = items
42 d.cursor = 0
43 d.active = true
44 return true
45}
46
47// Close hides the dialog.
48func (d *telegramDialog) Close() { d.active = false }
49
50// Active reports whether the dialog is consuming input.
51func (d *telegramDialog) Active() bool { return d != nil && d.active }
52
53// HandleKey advances the selection or resolves the dialog.
54func (d *telegramDialog) HandleKey(k tui.Key) telegramAction {
55 switch k.Kind {
56 case tui.KeyUp:
57 if d.cursor > 0 {
58 d.cursor--
59 }
60 case tui.KeyDown:
61 if d.cursor < len(d.items)-1 {
62 d.cursor++
63 }
64 case tui.KeyEsc:
65 d.Close()
66 return telegramAction{Close: true}
67 case tui.KeyEnter:
68 if len(d.items) == 0 {
69 d.Close()
70 return telegramAction{Close: true}
71 }
72 it := d.items[d.cursor]
73 d.Close()
74 return telegramAction{Select: true, Action: it.action}
75 }
76 return telegramAction{}
77}
78
79// Render returns the dialog lines.
80func (d *telegramDialog) Render(th tui.Theme, width int) []string {
81 if !d.Active() {
82 return nil
83 }
84 var lines []string
85 lines = append(lines, frameHeader(th, "telegram", width))
86 lines = append(lines, th.FGColor(th.Muted, "pick an action (\u2191/\u2193, enter, esc to cancel):"))
87 for i, it := range d.items {
88 plain := " " + it.label
89 if it.hint != "" {
90 plain += " " + th.FGColor(th.Muted, "("+it.hint+")")
91 }
92 if i == d.cursor {
93 lines = append(lines, th.PadHighlight(plain, width))
94 } else {
95 lines = append(lines, th.FGColor(th.Muted, plain))
96 }
97 }
98 lines = append(lines, frameRule(th, width))
99 return lines
100}