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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Display list construction, sorting and grouping. Implements `impl App`
//! continuation with the builders that turn host + pattern entries into
//! rendered list items, plus `apply_sort` and the group partitioning helpers.
use std::collections::HashMap;
use super::{GroupBy, HostListItem, SortMode};
use crate::app::App;
use crate::ssh_config::model::{ConfigElement, HostEntry, PatternEntry, SshConfigFile};
impl App {
/// Build the display list with group headers from comments above host blocks.
/// Comments are associated with the host block directly below them (no blank line between).
/// Because the parser puts inter-block comments inside the preceding block's directives,
/// we also extract trailing comments from each HostBlock.
pub(crate) fn build_display_list_from(
config: &SshConfigFile,
hosts: &[HostEntry],
patterns: &[PatternEntry],
) -> Vec<HostListItem> {
let mut display_list = Vec::new();
let mut host_index = 0;
let mut pending_comment: Option<String> = None;
for element in &config.elements {
match element {
ConfigElement::GlobalLine(line) => {
let trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix('#') {
let text = rest.trim();
let text = text.strip_prefix("purple:group ").unwrap_or(text);
if !text.is_empty() {
pending_comment = Some(text.to_string());
}
} else if trimmed.is_empty() {
// Blank line breaks the comment-to-host association
pending_comment = None;
} else {
pending_comment = None;
}
}
ConfigElement::HostBlock(block) => {
if crate::ssh_config::model::is_host_pattern(&block.host_pattern) {
pending_comment = None;
continue;
}
if host_index < hosts.len() {
if let Some(header) = pending_comment.take() {
display_list.push(HostListItem::GroupHeader(header));
}
display_list.push(HostListItem::Host { index: host_index });
host_index += 1;
}
// Extract trailing comments from this block for the next host
pending_comment = Self::extract_trailing_comment(&block.directives);
}
ConfigElement::Include(include) => {
pending_comment = None;
for file in &include.resolved_files {
Self::build_display_list_from_included(
&file.elements,
&file.path,
hosts,
&mut host_index,
&mut display_list,
);
}
}
}
}
// Append pattern group at the bottom
if !patterns.is_empty() {
let mut pattern_index = 0usize;
display_list.push(HostListItem::GroupHeader("Patterns".to_string()));
Self::append_pattern_items(&config.elements, &mut pattern_index, &mut display_list);
debug_assert_eq!(
pattern_index,
patterns.len(),
"append_pattern_items and collect_pattern_entries traversal mismatch"
);
}
display_list
}
fn append_pattern_items(
elements: &[ConfigElement],
pattern_index: &mut usize,
display_list: &mut Vec<HostListItem>,
) {
for e in elements {
match e {
ConfigElement::HostBlock(block) => {
if crate::ssh_config::model::is_host_pattern(&block.host_pattern) {
display_list.push(HostListItem::Pattern {
index: *pattern_index,
});
*pattern_index += 1;
}
}
ConfigElement::Include(include) => {
for file in &include.resolved_files {
Self::append_pattern_items(&file.elements, pattern_index, display_list);
}
}
ConfigElement::GlobalLine(_) => {}
}
}
}
/// Extract a trailing comment from a block's directives.
/// If the last non-blank line in the directives is a comment, return it as
/// a potential group header for the next host block.
/// Strips `purple:group ` prefix so headers display as the provider name.
fn extract_trailing_comment(
directives: &[crate::ssh_config::model::Directive],
) -> Option<String> {
let d = directives.last()?;
if !d.is_non_directive {
return None;
}
let trimmed = d.raw_line.trim();
if trimmed.is_empty() {
return None;
}
if let Some(rest) = trimmed.strip_prefix('#') {
let text = rest.trim();
// Skip purple metadata comments (purple:provider, purple:tags)
// Only purple:group should produce a group header
if text.starts_with("purple:") && !text.starts_with("purple:group ") {
return None;
}
let text = text.strip_prefix("purple:group ").unwrap_or(text);
if !text.is_empty() {
return Some(text.to_string());
}
}
None
}
fn build_display_list_from_included(
elements: &[ConfigElement],
file_path: &std::path::Path,
hosts: &[HostEntry],
host_index: &mut usize,
display_list: &mut Vec<HostListItem>,
) {
let mut pending_comment: Option<String> = None;
let file_name = file_path
.file_name()
.map(|f| f.to_string_lossy().to_string())
.unwrap_or_default();
// Add file header for included files
if !file_name.is_empty() {
let has_hosts = elements.iter().any(|e| {
matches!(e, ConfigElement::HostBlock(b)
if !crate::ssh_config::model::is_host_pattern(&b.host_pattern)
)
});
if has_hosts {
display_list.push(HostListItem::GroupHeader(file_name));
}
}
for element in elements {
match element {
ConfigElement::GlobalLine(line) => {
let trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix('#') {
let text = rest.trim();
let text = text.strip_prefix("purple:group ").unwrap_or(text);
if !text.is_empty() {
pending_comment = Some(text.to_string());
}
} else {
pending_comment = None;
}
}
ConfigElement::HostBlock(block) => {
if crate::ssh_config::model::is_host_pattern(&block.host_pattern) {
pending_comment = None;
continue;
}
if *host_index < hosts.len() {
if let Some(header) = pending_comment.take() {
display_list.push(HostListItem::GroupHeader(header));
}
display_list.push(HostListItem::Host { index: *host_index });
*host_index += 1;
}
// Extract trailing comments from this block for the next host
pending_comment = Self::extract_trailing_comment(&block.directives);
}
ConfigElement::Include(include) => {
pending_comment = None;
for file in &include.resolved_files {
Self::build_display_list_from_included(
&file.elements,
&file.path,
hosts,
host_index,
display_list,
);
}
}
}
}
}
/// Rebuild the display list based on the current sort mode and group_by toggle.
pub fn apply_sort(&mut self) {
// Preserve currently selected host or pattern across sort changes
let selected_alias = self
.selected_host()
.map(|h| h.alias.clone())
.or_else(|| self.selected_pattern().map(|p| p.pattern.clone()));
// Multi-select indices become visually misleading after reorder
self.multi_select.clear();
if self.sort_mode == SortMode::Original && matches!(self.group_by, GroupBy::None) {
self.display_list =
Self::build_display_list_from(&self.config, &self.hosts, &self.patterns);
} else if self.sort_mode == SortMode::Original && !matches!(self.group_by, GroupBy::None) {
// Original order but grouped: extract flat indices from config order
let indices: Vec<usize> = (0..self.hosts.len()).collect();
self.display_list = self.group_indices(&indices);
} else {
let mut indices: Vec<usize> = (0..self.hosts.len()).collect();
match self.sort_mode {
SortMode::AlphaAlias => {
indices.sort_by_cached_key(|&i| {
let stale = self.hosts[i].stale.is_some();
(stale, self.hosts[i].alias.to_ascii_lowercase())
});
}
SortMode::AlphaHostname => {
indices.sort_by_cached_key(|&i| {
let stale = self.hosts[i].stale.is_some();
(stale, self.hosts[i].hostname.to_ascii_lowercase())
});
}
SortMode::Frecency => {
indices.sort_by(|a, b| {
let sa = self.hosts[*a].stale.is_some();
let sb = self.hosts[*b].stale.is_some();
sa.cmp(&sb).then_with(|| {
let score_a = self.history.frecency_score(&self.hosts[*a].alias);
let score_b = self.history.frecency_score(&self.hosts[*b].alias);
score_b.total_cmp(&score_a)
})
});
}
SortMode::MostRecent => {
indices.sort_by(|a, b| {
let sa = self.hosts[*a].stale.is_some();
let sb = self.hosts[*b].stale.is_some();
sa.cmp(&sb).then_with(|| {
let ts_a = self.history.last_connected(&self.hosts[*a].alias);
let ts_b = self.history.last_connected(&self.hosts[*b].alias);
ts_b.cmp(&ts_a)
})
});
}
SortMode::Status => {
indices.sort_by(|a, b| {
let sa = self.hosts[*a].stale.is_some();
let sb = self.hosts[*b].stale.is_some();
sa.cmp(&sb).then_with(|| {
let pa = self.ping.status.get(&self.hosts[*a].alias);
let pb = self.ping.status.get(&self.hosts[*b].alias);
super::ping_sort_key(pa).cmp(&super::ping_sort_key(pb))
})
});
}
_ => {}
}
self.display_list = self.group_indices(&indices);
}
// Append pattern group at the bottom (sorted/grouped paths skip
// build_display_list_from which already handles this)
if (self.sort_mode != SortMode::Original || !matches!(self.group_by, GroupBy::None))
&& !self.patterns.is_empty()
{
self.display_list
.push(HostListItem::GroupHeader("Patterns".to_string()));
let mut pattern_index = 0usize;
Self::append_pattern_items(
&self.config.elements,
&mut pattern_index,
&mut self.display_list,
);
}
// Compute group host counts before group filtering
{
self.group_host_counts.clear();
let mut current_group: Option<&str> = None;
for item in &self.display_list {
match item {
HostListItem::GroupHeader(text) => {
current_group = Some(text.as_str());
}
HostListItem::Host { .. } | HostListItem::Pattern { .. } => {
if let Some(group) = current_group {
*self.group_host_counts.entry(group.to_string()).or_insert(0) += 1;
}
}
}
}
}
// Build group tab order. For tag mode, compute from host tags (matching
// render_group_bar's tab list). For provider mode, extract from GroupHeaders.
self.group_tab_order = match &self.group_by {
GroupBy::Tag(_) => {
let mut tag_counts: HashMap<String, usize> = HashMap::new();
for host in &self.hosts {
for tag in host.tags.iter() {
*tag_counts.entry(tag.clone()).or_insert(0) += 1;
}
}
for pattern in &self.patterns {
for tag in &pattern.tags {
*tag_counts.entry(tag.clone()).or_insert(0) += 1;
}
}
let mut sorted: Vec<(String, usize)> = tag_counts.into_iter().collect();
sorted.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
let top: Vec<(String, usize)> = sorted.into_iter().take(10).collect();
self.group_host_counts = top.iter().map(|(t, c)| (t.clone(), *c)).collect();
top.into_iter().map(|(t, _)| t).collect()
}
_ => {
let mut order = Vec::new();
let mut seen: std::collections::HashSet<&str> = std::collections::HashSet::new();
for item in &self.display_list {
if let HostListItem::GroupHeader(text) = item {
if seen.insert(text.as_str()) {
order.push(text.clone());
}
}
}
order
}
};
// Re-derive group_tab_index from group_filter after rebuild
self.group_tab_index = match &self.group_filter {
Some(name) => self
.group_tab_order
.iter()
.position(|g| g == name)
.map(|i| i + 1)
.unwrap_or(0),
None => 0,
};
// Filter by group if active
if let Some(ref filter) = self.group_filter {
let is_tag_mode = matches!(self.group_by, GroupBy::Tag(_));
let mut filtered = Vec::with_capacity(self.display_list.len());
if is_tag_mode {
// In tag mode, filter by host tags directly (GroupHeaders don't
// cover all tags, only the active GroupBy tag).
for item in std::mem::take(&mut self.display_list) {
match &item {
HostListItem::GroupHeader(_) => {} // skip all headers
HostListItem::Host { index } => {
if let Some(host) = self.hosts.get(*index) {
if host
.tags
.iter()
.chain(host.provider_tags.iter())
.any(|t| t == filter)
{
filtered.push(item);
}
}
}
HostListItem::Pattern { index } => {
if let Some(pattern) = self.patterns.get(*index) {
if pattern.tags.iter().any(|t| t == filter) {
filtered.push(item);
}
}
}
}
}
} else {
// In provider/none mode, filter by GroupHeader matching
let mut in_group = false;
for item in std::mem::take(&mut self.display_list) {
match &item {
HostListItem::GroupHeader(text) => {
in_group = text == filter;
}
_ => {
if in_group {
filtered.push(item);
}
}
}
}
}
self.display_list = filtered;
}
// Restore selection by alias, fall back to first host
if let Some(alias) = selected_alias {
self.select_host_by_alias(&alias);
if self.selected_host().is_some() || self.selected_pattern().is_some() {
return;
}
}
self.select_first_host();
}
/// Select the first selectable item in the display list (always skips headers).
pub fn select_first_host(&mut self) {
if let Some(pos) = self.display_list.iter().position(|item| {
matches!(
item,
HostListItem::Host { .. } | HostListItem::Pattern { .. }
)
}) {
self.ui.list_state.select(Some(pos));
}
}
/// Partition sorted indices by provider, inserting group headers.
/// Hosts without provider appear first (no header), then named provider
/// groups (in first-appearance order) with headers.
fn group_indices(&self, sorted_indices: &[usize]) -> Vec<HostListItem> {
match &self.group_by {
GroupBy::None => sorted_indices
.iter()
.map(|&i| HostListItem::Host { index: i })
.collect(),
GroupBy::Provider => Self::group_indices_by_provider(&self.hosts, sorted_indices),
GroupBy::Tag(tag) => Self::group_indices_by_tag(&self.hosts, sorted_indices, tag),
}
}
fn group_indices_by_provider(
hosts: &[HostEntry],
sorted_indices: &[usize],
) -> Vec<HostListItem> {
let mut none_indices: Vec<usize> = Vec::new();
let mut provider_groups: Vec<(&str, Vec<usize>)> = Vec::new();
let mut provider_order: HashMap<&str, usize> = HashMap::new();
for &idx in sorted_indices {
match &hosts[idx].provider {
None => none_indices.push(idx),
Some(name) => {
if let Some(&group_idx) = provider_order.get(name.as_str()) {
provider_groups[group_idx].1.push(idx);
} else {
let group_idx = provider_groups.len();
provider_order.insert(name, group_idx);
provider_groups.push((name, vec![idx]));
}
}
}
}
let mut display_list = Vec::new();
// Non-provider hosts first (no header)
for idx in &none_indices {
display_list.push(HostListItem::Host { index: *idx });
}
// Then provider groups with headers
for (name, indices) in &provider_groups {
let header = crate::providers::provider_display_name(name);
display_list.push(HostListItem::GroupHeader(header.to_string()));
for &idx in indices {
display_list.push(HostListItem::Host { index: idx });
}
}
display_list
}
/// Partition sorted indices by a user tag, inserting a group header.
/// Hosts without the tag appear first (no header), then hosts with the
/// tag appear under a single group header.
fn group_indices_by_tag(
hosts: &[HostEntry],
sorted_indices: &[usize],
tag: &str,
) -> Vec<HostListItem> {
let mut without_tag: Vec<usize> = Vec::new();
let mut with_tag: Vec<usize> = Vec::new();
for &idx in sorted_indices {
if hosts[idx].tags.iter().any(|t| t == tag) {
with_tag.push(idx);
} else {
without_tag.push(idx);
}
}
let mut display_list = Vec::new();
for idx in &without_tag {
display_list.push(HostListItem::Host { index: *idx });
}
if !with_tag.is_empty() {
display_list.push(HostListItem::GroupHeader(tag.to_string()));
for &idx in &with_tag {
display_list.push(HostListItem::Host { index: idx });
}
}
display_list
}
}