1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Width-driven column layout for the `tm ls` session TUI (#7224).
//!
//! Why: the pre-#7224 picker computed its column widths once, from
//! `terminal_width()` at startup, so a resize left the menu formatted for a
//! terminal that no longer existed. A TUI redraws every frame, which means the
//! column set has to be a pure function of the CURRENT width — and a pure
//! function is the only part of a TUI that can be unit-tested without a
//! terminal.
//!
//! What: [`visible_columns`] picks which columns fit at a given width by
//! PRIORITY (name, then state, project, id, tmux) and returns them in DISPLAY
//! order; [`header`] and [`cell`] build one column's text, truncating with an
//! ellipsis through the same [`super::super::managed_render::truncate`] the
//! static table uses. [`visible_window`] is the scroll seam: which slice of the
//! rows a viewport of `height` shows with `selected` inside it.
//! [`wrap_message`] is the same idea for the status line: how many rows a
//! message needs at a given width, and what goes on each of them.
//!
//! Column vocabulary matches the console's (#7224): friendly name, short id,
//! project, state, tmux pane.
//!
//! Test: `layout_*`, `window_*` and `wrap_*` in `super::tests`.
use ManagedSessionSummary;
use truncate;
/// One column of the session table.
///
/// Why: a typed set — rather than a width table indexed by string — is what
/// lets [`visible_columns`] express priority and display order as two orderings
/// over the same values without either drifting from the render.
/// What: the five columns the console's session vocabulary names.
/// Test: `layout_columns_*` in `super::tests`.
pub
/// How many characters of the session UUID the `ID` column shows.
///
/// Eight is the git-short-hash convention and stays unambiguous across a fleet
/// of a few hundred sessions; the full UUID is a copy-paste target, and the TUI
/// is not where anyone copies one from.
pub const SHORT_ID_LEN: usize = 8;
/// Rendered width of each column, excluding the one-space gutter between them.
const NAME_WIDTH: u16 = 28;
const ID_WIDTH: u16 = SHORT_ID_LEN as u16;
const PROJECT_WIDTH: u16 = 24;
const STATE_WIDTH: u16 = 18;
const TMUX_WIDTH: u16 = 8;
/// Width the selection marker column takes on the left of every row.
pub const MARKER_WIDTH: u16 = 2;
/// The gutter drawn between two adjacent columns.
const GUTTER: u16 = 1;
/// Columns in the order they are DROPPED from the right of the priority list.
///
/// Name is not in this list: it is unconditional, because a row with no name is
/// not a row anyone can act on.
const BY_PRIORITY: = ;
/// Columns in the order they are DRAWN, left to right.
const BY_DISPLAY: = ;
/// This column's fixed rendered width.
pub
/// The header label for a column.
pub
/// Which columns fit in `total_width`, in display order (#7224 requirement 2).
///
/// Why: below roughly 100 columns the full set cannot be drawn without
/// wrapping, and a wrapped table is unreadable. Dropping whole columns from the
/// least-load-bearing end beats squeezing every column into an ellipsis, which
/// would leave five columns of nothing but `…`.
/// What: keeps [`Column::Name`] unconditionally, then admits each column of
/// [`BY_PRIORITY`] while the running total — marker, columns, and one gutter
/// per column after the first — still fits. The admitted set is returned in
/// [`BY_DISPLAY`] order, so priority never leaks into what the operator sees.
/// A width too small even for `NAME` still returns `[Name]`: the table clips
/// what it cannot draw, and returning nothing would render an empty screen.
/// Test: `layout_columns_full_set_at_wide_width`,
/// `layout_columns_drops_tmux_then_id_at_eighty`,
/// `layout_columns_keeps_name_at_absurdly_narrow_width`.
pub
/// Build one column's cell text for `session`, truncated to the column width.
///
/// Why: truncation goes through the static table's own [`truncate`] so a name
/// clipped in the TUI is clipped identically in `tm ls --plain` — the ellipsis
/// is the same character in both, not two conventions for one fleet.
/// What: `Name` is the friendly name; `Id` the first [`SHORT_ID_LEN`]
/// characters of the UUID (no ellipsis — a short id is a prefix, and an `…`
/// would make it un-pasteable); `Project` the `source_id` or `-`; `State` the
/// static table's annotated state cell, with `attached` winning over the raw
/// word; `Tmux` the pane id or `-`.
/// Test: `layout_cell_*` in `super::tests`.
pub
/// The `STATE` cell text, annotations included.
///
/// Why: the annotations (`[dead]`, `[stale-assets]`, `[resume-parked]`) are the
/// difference between a stopped session someone can resume and one nothing will
/// ever revive. Reusing
/// [`format_state_column`](super::super::managed_render::format_state_column)
/// keeps the TUI and the static table from disagreeing about which is which.
/// What: `attached` replaces the raw state word — a client is connected right
/// now, the strongest signal — then the shared formatter appends the markers.
/// Test: `layout_cell_state_uses_the_shared_annotations`.
pub
/// Which slice of `total` rows a `height`-row viewport shows around `selected`.
///
/// Why: ratatui's `TableState` recomputes its scroll offset from the widget's
/// own memory, which this TUI deliberately does not keep — the session list is
/// rebuilt from the daemon on every refresh, and a stale widget offset would
/// then point into rows that no longer exist. Computing the window here makes
/// scrolling a pure function of four numbers, so "the selected row is always on
/// screen" is a unit test rather than something to eyeball. Threading
/// `previous` through is what keeps the list STILL when the selection moves
/// within the rows already drawn; anchoring the window to the selection instead
/// would scroll the whole table on every arrow key.
/// What: returns `(start, len)`. `len` is `min(height, total)`; `start` begins
/// at `previous`, is clamped to `total - len`, and then moves the minimum
/// distance that brings `selected` back inside. A `height` of `0` returns
/// `(0, 0)` — a viewport with no rows shows no rows, rather than panicking on
/// the subtraction.
/// Test: `window_keeps_selection_visible_scrolling_down`,
/// `window_holds_still_while_the_selection_stays_visible`,
/// `window_clamps_to_the_end_of_the_list`, `window_zero_height_is_empty`.
pub
/// How many rows the status line may grow to before its tail is elided (#7224).
///
/// Four is what the daemon's longest refusal — the delete guard's, which ends
/// with a full UUID on its own line — needs at 80 columns.
pub const STATUS_MAX_LINES: usize = 4;
/// Wrap a status message to `width`, on whitespace, never mid-token (#7224).
///
/// Why: the status line was a one-row unwrapped `Paragraph`, so ratatui clipped
/// whatever did not fit — and what did not fit was the tail of the delete
/// guard's refusal, cutting a session UUID in half. A half-UUID is worse than
/// no UUID: it looks copy-pasteable and is not. Wrapping here rather than
/// through ratatui's own `Wrap` is what lets the caller size the status region
/// from the SAME lines it will draw, so the height and the content cannot
/// disagree. Packing greedily from the front had the same end result at a
/// narrow width by a different route: below about 70 columns the refusal's
/// first paragraph filled all four rows on its own and the id paragraph behind
/// it was truncated away whole.
/// What: splits on `'\n'` first (the refusal puts the id on its own line) and
/// packs each paragraph's whitespace-separated tokens greedily into lines of at
/// most `width` characters. A token longer than `width` is emitted alone and
/// intact — at that width no wrapping could show it whole, and splitting it is
/// the exact failure this exists to prevent. The LAST paragraph's rows are then
/// reserved before anything ahead of them is kept, so the identifier a message
/// ends with survives every width; the paragraphs in front of it fill what
/// budget remains and the last of them is elided on a token boundary. A single
/// paragraph has nothing to reserve behind it and is simply capped at
/// `max_lines` with its last line elided. A `width` or `max_lines` of zero
/// yields no lines.
/// Test: `wrap_message_keeps_a_uuid_whole_at_eighty_columns`,
/// `wrap_message_keeps_the_session_id_at_narrow_widths`,
/// `wrap_message_keeps_an_overlong_token_whole`,
/// `wrap_message_honours_explicit_newlines`,
/// `wrap_message_elides_on_a_token_boundary`,
/// `wrap_message_zero_width_is_empty`.
pub
/// Pack one newline-free paragraph into `width`-wide lines, never mid-token.
///
/// Always returns at least one line, empty when the paragraph holds no tokens —
/// which is what lets [`wrap_message`] recognise and trim a trailing blank.
/// Trim `line` back to a whole-token boundary and mark it elided.
///
/// Drops trailing tokens until the ellipsis fits within `width`. A line with no
/// whitespace to cut at is left exactly as it is rather than gaining a mark that
/// would push it further over.