concord 2.5.2

A terminal user interface client for Discord
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
use super::activity::{ActivityLeading, build_activity_render};
use super::message::list::render_image_preview;
use super::*;
use crate::discord::ActivityKind;
use crate::tui::selection;
use crate::tui::state::SCROLL_OFF;
use crate::tui::text::format_byte_size;
use ratatui::layout::Position;

mod action_menu;
mod attachment_viewer;
mod channel_switcher;
mod confirmation;
mod debug_log;
mod downloads;
mod folder_settings;
mod forum_post;
mod keymap;
mod notification_inbox;
mod options;
mod polls;
mod profile;
mod reactions;
mod search;
mod stream_info;
mod thread_edit;
mod toast;
mod url_picker;
mod voice_participant_audio;

const POPUP_GAUGE_WIDTH: usize = 28;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct PopupScrollMetrics {
    position: usize,
    viewport_len: usize,
    content_len: usize,
}

/// The rendered item rows for the active selectable popup. State owns the
/// selected item, while this UI plan owns terminal geometry and variable row
/// heights. Paging, rendering, scrollbars, and hit testing all derive from the
/// same plan.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct SelectablePopupLayout {
    pub(super) target: SelectablePopupTarget,
    pub(super) popup: Rect,
    pub(super) list: Rect,
    pub(super) scroll: usize,
    row_items: Vec<Option<usize>>,
}

impl SelectablePopupLayout {
    fn new(
        target: SelectablePopupTarget,
        popup: Rect,
        list: Rect,
        snapshot: SelectablePopupSnapshot,
        rows_from_start: impl Fn(usize, usize) -> Vec<Option<usize>>,
    ) -> Self {
        let max_rows = usize::from(list.height).max(1);
        let (scroll, row_items) = popup_rows_with_visible_selection(
            snapshot.item_count,
            snapshot.selected,
            snapshot.scroll,
            max_rows,
            rows_from_start,
        );
        Self {
            target,
            popup,
            list,
            scroll,
            row_items,
        }
    }

    fn fixed(
        target: SelectablePopupTarget,
        popup: Rect,
        snapshot: SelectablePopupSnapshot,
    ) -> Self {
        let inner = panel_block("", false).inner(popup);
        let list = Rect {
            width: inner.width.saturating_sub(1).max(1),
            ..inner
        };
        Self::new(target, popup, list, snapshot, |start, max_rows| {
            (start..snapshot.item_count.min(start.saturating_add(max_rows)))
                .map(Some)
                .collect()
        })
    }

    pub(super) fn visible_items(&self) -> usize {
        self.row_items
            .iter()
            .flatten()
            .copied()
            .fold((None, 0usize), |(previous, count), item| {
                if previous == Some(item) {
                    (previous, count)
                } else {
                    (Some(item), count + 1)
                }
            })
            .1
            .max(1)
    }

    pub(super) fn item_at(&self, column: u16, row: u16) -> Option<usize> {
        let inside = column >= self.list.x
            && column < self.list.x.saturating_add(self.list.width)
            && row >= self.list.y
            && row < self.list.y.saturating_add(self.list.height);
        if !inside {
            return None;
        }
        self.row_items
            .get(usize::from(row.saturating_sub(self.list.y)))
            .copied()
            .flatten()
    }
}

fn popup_rows_with_visible_selection(
    item_count: usize,
    selected: usize,
    preferred_start: usize,
    max_rows: usize,
    rows_from_start: impl Fn(usize, usize) -> Vec<Option<usize>>,
) -> (usize, Vec<Option<usize>>) {
    if item_count == 0 {
        return (0, Vec::new());
    }

    let selected = selected.min(item_count - 1);
    let mut start = preferred_start.min(item_count - 1).min(selected);
    let mut rows = rows_from_start(start, max_rows);
    while !rows.iter().flatten().any(|item| *item == selected) && start < selected {
        start += 1;
        rows = rows_from_start(start, max_rows);
    }

    // At the end of a list, fill otherwise empty rows without hiding the
    // selection. This keeps the window stable and avoids a blank tail.
    while start > 0
        && rows.len() < max_rows
        && rows.iter().flatten().next_back().copied() == Some(item_count - 1)
    {
        let candidate = rows_from_start(start - 1, max_rows);
        if !candidate.iter().flatten().any(|item| *item == selected) {
            break;
        }
        start -= 1;
        rows = candidate;
    }

    // Match pane navigation's scrolloff, but count logical items rather than
    // terminal rows. This keeps the same breathing room for grouped and
    // variable-height lists without making their geometry a state concern.
    while let Some((before, after, visible)) = popup_visible_item_counts(&rows, selected) {
        let scrolloff = SCROLL_OFF.min(visible.saturating_sub(1) / 2);
        if before < scrolloff && start > 0 {
            let candidate = rows_from_start(start - 1, max_rows);
            if popup_visible_item_counts(&candidate, selected)
                .is_some_and(|(candidate_before, _, _)| candidate_before > before)
            {
                start -= 1;
                rows = candidate;
                continue;
            }
        }
        if after < scrolloff && start < selected {
            let candidate = rows_from_start(start + 1, max_rows);
            if popup_visible_item_counts(&candidate, selected)
                .is_some_and(|(_, candidate_after, _)| candidate_after > after)
            {
                start += 1;
                rows = candidate;
                continue;
            }
        }
        break;
    }

    (start, rows)
}

fn popup_visible_item_counts(
    rows: &[Option<usize>],
    selected: usize,
) -> Option<(usize, usize, usize)> {
    let mut items = Vec::new();
    for item in rows.iter().flatten().copied() {
        if items.last().copied() != Some(item) {
            items.push(item);
        }
    }
    let position = items.iter().position(|item| *item == selected)?;
    Some((
        position,
        items.len().saturating_sub(position + 1),
        items.len(),
    ))
}

pub(super) use action_menu::{
    action_menu_area, key_sequence_hint_area_for_state, render_channel_action_menu,
    render_guild_action_menu, render_key_sequence_hint, render_member_action_menu,
    render_message_action_menu, render_thread_action_menu,
};
#[cfg(test)]
pub(super) use action_menu::{
    channel_action_menu_lines_for_test, message_action_menu_lines,
    message_action_menu_lines_with_keymap_options,
};
#[cfg(test)]
pub(super) use attachment_viewer::centered_viewer_preview_area;
pub(super) use attachment_viewer::render_attachment_viewer;
#[cfg(test)]
pub(super) use channel_switcher::{channel_switcher_cursor_position, channel_switcher_lines};
pub(super) use channel_switcher::{
    channel_switcher_list_layout, channel_switcher_popup_area, render_channel_switcher_popup,
};
pub(super) use confirmation::{
    guild_leave_confirmation_popup_area_for_state, message_confirmation_popup_area_for_state,
    quit_confirmation_popup_area, render_guild_leave_confirmation, render_message_confirmation,
    render_notification_inbox_mark_all_confirmation, render_quit_confirmation,
    render_thread_delete_confirmation, thread_delete_confirmation_popup_area_for_state,
};
#[cfg(test)]
pub(super) use confirmation::{
    message_delete_confirmation_lines, message_pin_confirmation_lines,
    message_remove_embeds_confirmation_lines, quit_confirmation_lines,
};
#[cfg(test)]
pub(super) use debug_log::debug_log_popup_lines;
pub(super) use debug_log::{debug_log_popup_area_for_state, render_debug_log_popup};
#[cfg(test)]
pub(super) use downloads::downloads_popup_lines;
pub(super) use downloads::{
    downloads_popup_area, downloads_popup_line_count, render_downloads_popup,
};
#[cfg(test)]
pub(super) use folder_settings::folder_settings_input_line_for_test;
pub(super) use folder_settings::{folder_settings_popup_area, render_folder_settings_popup};
pub(super) use forum_post::{
    forum_post_composer_metrics, forum_post_composer_popup_area, forum_post_tag_picker_list_layout,
    render_forum_post_composer, render_forum_post_tag_picker,
};
#[cfg(test)]
pub(super) use keymap::keymap_help_popup_lines;
pub(super) use keymap::{
    keymap_popup_area, keymap_popup_text_area, keymap_popup_total_lines, render_keymap_help_popup,
};
pub(super) use notification_inbox::{
    notification_inbox_list_layout, notification_inbox_popup_area, render_notification_inbox_popup,
};
#[cfg(test)]
pub(super) use options::options_popup_lines;
pub(super) use options::{options_popup_area, options_popup_list_layout, render_options_popup};
#[cfg(test)]
pub(super) use polls::poll_vote_picker_lines;
pub(super) use polls::{poll_vote_picker_popup_area, render_poll_vote_picker};
pub(in crate::tui) use profile::user_profile_popup_area;
pub(super) use profile::{
    render_user_profile_popup, user_profile_picker_list_layout, user_profile_popup_has_avatar,
    user_profile_popup_selected_picker_line, user_profile_popup_text_geometry,
    user_profile_popup_total_lines,
};
#[cfg(test)]
pub(super) use profile::{user_profile_popup_lines, user_profile_popup_lines_with_activities};
#[cfg(test)]
pub(super) use reactions::{
    emoji_reaction_picker_lines, emoji_reaction_picker_lines_for_width,
    emoji_reaction_picker_lines_with_own_reactions, filtered_emoji_reaction_picker_lines,
    reaction_list_lines_with_ready_urls, reaction_users_popup_lines,
};
pub(super) use reactions::{
    emoji_reaction_picker_list_layout, emoji_reaction_picker_popup_area_for_state,
    reaction_users_list_layout, reaction_users_popup_area_for_state, render_emoji_reaction_picker,
    render_reaction_users_popup,
};
pub(super) use search::{
    render_search_popup, search_popup_area_for_state, search_popup_list_layout,
};
pub(super) use stream_info::{render_stream_info, stream_info_area, stream_info_lines_for_area};
#[cfg(test)]
pub(super) use stream_info::{stream_info_lines, stream_info_lines_for_width};
pub(super) use thread_edit::{
    render_thread_edit, render_thread_edit_tag_picker, thread_edit_metrics, thread_edit_popup_area,
    thread_edit_tag_picker_list_layout,
};
#[cfg(test)]
pub(super) use toast::toast_line;
pub(super) use toast::{render_toast, toast_area};
#[cfg(test)]
pub(super) use url_picker::message_url_picker_lines_for_width;
pub(super) use url_picker::{message_url_picker_popup_area, render_message_url_picker};
pub(super) use voice_participant_audio::{
    render_voice_participant_audio_popup, voice_participant_audio_list_layout,
    voice_participant_audio_popup_area,
};

pub(super) fn active_selectable_popup_layout(
    area: Rect,
    state: &DashboardState,
) -> Option<SelectablePopupLayout> {
    let snapshot = state.active_selectable_popup_snapshot()?;
    Some(match snapshot.target {
        SelectablePopupTarget::MessageActions
        | SelectablePopupTarget::GuildActions
        | SelectablePopupTarget::ChannelActions
        | SelectablePopupTarget::MemberActions
        | SelectablePopupTarget::ThreadActions => SelectablePopupLayout::fixed(
            snapshot.target,
            action_menu_area(area, snapshot.item_count),
            snapshot,
        ),
        SelectablePopupTarget::MessageUrls => SelectablePopupLayout::fixed(
            snapshot.target,
            message_url_picker_popup_area(area, snapshot.item_count),
            snapshot,
        ),
        SelectablePopupTarget::Options => options_popup_list_layout(area, state, snapshot),
        SelectablePopupTarget::UserProfileStatus | SelectablePopupTarget::UserProfileActivity => {
            user_profile_picker_list_layout(area, state, snapshot)
        }
        SelectablePopupTarget::EmojiReactions => {
            emoji_reaction_picker_list_layout(area, state, snapshot)
        }
        SelectablePopupTarget::PollVotes => SelectablePopupLayout::fixed(
            snapshot.target,
            poll_vote_picker_popup_area(area, snapshot.item_count),
            snapshot,
        ),
        SelectablePopupTarget::ReactionList => reaction_users_list_layout(area, snapshot),
        SelectablePopupTarget::ChannelSwitcher => {
            channel_switcher_list_layout(area, state, snapshot)
        }
        SelectablePopupTarget::NotificationInbox => {
            notification_inbox_list_layout(area, state, snapshot)
        }
        SelectablePopupTarget::ForumPostTags => forum_post_tag_picker_list_layout(area, snapshot),
        SelectablePopupTarget::ThreadEditTags => thread_edit_tag_picker_list_layout(area, snapshot),
        SelectablePopupTarget::VoiceParticipantAudio => {
            voice_participant_audio_list_layout(area, snapshot)
        }
        SelectablePopupTarget::SearchResults | SelectablePopupTarget::SearchSuggestions => {
            search_popup_list_layout(area, state, snapshot)
        }
    })
}

fn popup_gauge_spacer() -> Span<'static> {
    Span::raw(" ".repeat(POPUP_GAUGE_WIDTH))
}

fn popup_gauge_line(
    x_offset: u16,
    min_label: &str,
    max_label: String,
    style: Style,
) -> Line<'static> {
    let leading_space = usize::from(x_offset).saturating_sub(min_label.len().saturating_add(1));
    Line::from(vec![
        Span::styled(format!("{}{min_label} ", " ".repeat(leading_space)), style),
        popup_gauge_spacer(),
        Span::styled(format!(" {max_label}"), style),
    ])
}

struct PopupGauge {
    x_offset: u16,
    width_margin: u16,
    y: u16,
    value: u16,
    maximum: u16,
    style: Style,
}

fn render_popup_gauge(frame: &mut Frame, inner: Rect, gauge: PopupGauge) {
    let gauge_width = inner
        .width
        .saturating_sub(gauge.width_margin)
        .min(POPUP_GAUGE_WIDTH as u16);
    if gauge_width == 0 {
        return;
    }
    let ratio = if gauge.maximum == 0 {
        0.0
    } else {
        f64::from(gauge.value) / f64::from(gauge.maximum)
    };
    frame.render_widget(
        Gauge::default()
            .ratio(ratio.clamp(0.0, 1.0))
            .label("")
            .gauge_style(gauge.style),
        Rect::new(
            inner.x.saturating_add(gauge.x_offset),
            gauge.y,
            gauge_width,
            1,
        ),
    );
}

pub(super) fn background_media_occlusion_areas(
    frame_area: Rect,
    state: &DashboardState,
) -> Vec<Rect> {
    let mut areas = Vec::new();

    if state.is_folder_settings_open() {
        areas.push(folder_settings_popup_area(frame_area));
    }
    if let Some(area) = active_modal_popup_area(frame_area, state) {
        areas.push(area);
    }
    if state.is_key_sequence_active() {
        areas.push(key_sequence_hint_area_for_state(frame_area, state));
    }

    let downloads = state.attachment_downloads();
    if !downloads.is_empty() {
        areas.push(downloads_popup_area(
            frame_area,
            downloads_popup_line_count(downloads.len()),
        ));
    }
    if let Some(toast) = state.toast_message() {
        areas.push(toast_area(frame_area, toast.text));
    }
    let members = dashboard_areas(frame_area, state).members;
    let stream_lines = stream_info_lines_for_area(state, members);
    if !stream_lines.is_empty() {
        areas.push(stream_info_area(members, &stream_lines));
    }

    areas.into_iter().filter(|area| !area.is_empty()).collect()
}

fn active_modal_popup_area(frame_area: Rect, state: &DashboardState) -> Option<Rect> {
    let kind = state.active_modal_popup_kind()?;
    match kind {
        ActiveModalPopupKind::MessageActionMenu => {
            let actions = state.selected_message_action_items();
            (!actions.is_empty()).then(|| action_menu_area(frame_area, actions.len()))
        }
        ActiveModalPopupKind::GuildActionMenu => {
            let count = state.guild_action_row_count();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::ChannelActionMenu => {
            let count = state.channel_action_row_count();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::MemberActionMenu => {
            let count = state.selected_member_action_items().len();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::MessageUrlPicker => {
            let urls = state.selected_message_url_items();
            (!urls.is_empty()).then(|| message_url_picker_popup_area(frame_area, urls.len()))
        }
        ActiveModalPopupKind::MessageConfirmation => {
            message_confirmation_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::QuitConfirmation => Some(quit_confirmation_popup_area(frame_area)),
        ActiveModalPopupKind::GuildLeaveConfirmation => {
            guild_leave_confirmation_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::ThreadDeleteConfirmation => {
            thread_delete_confirmation_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::Options => Some(options_popup_area(frame_area, state)),
        ActiveModalPopupKind::AttachmentViewer => Some(attachment_viewer_popup(
            frame_area,
            state.attachment_viewer_zoom(),
        )),
        ActiveModalPopupKind::UserProfile => Some(user_profile_popup_area(frame_area)),
        ActiveModalPopupKind::EmojiReactionPicker => {
            emoji_reaction_picker_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::PollVotePicker => state
            .poll_vote_picker_items()
            .filter(|answers| !answers.is_empty())
            .map(|answers| poll_vote_picker_popup_area(frame_area, answers.len())),
        ActiveModalPopupKind::ReactionUsers => {
            reaction_users_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::DebugLog => Some(debug_log_popup_area_for_state(frame_area, state)),
        ActiveModalPopupKind::KeymapHelp => Some(keymap_popup_area(frame_area)),
        ActiveModalPopupKind::ChannelSwitcher => Some(channel_switcher_popup_area(frame_area)),
        ActiveModalPopupKind::NotificationInbox => Some(notification_inbox_popup_area(frame_area)),
        ActiveModalPopupKind::Search => search_popup_area_for_state(frame_area, state),
        ActiveModalPopupKind::ForumPostComposer => Some(forum_post_composer_popup_area(frame_area)),
        ActiveModalPopupKind::ThreadEdit => Some(thread_edit_popup_area(frame_area)),
        ActiveModalPopupKind::ThreadActionMenu => {
            let count = state.thread_action_row_count();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::VoiceParticipantAudio => {
            Some(voice_participant_audio_popup_area(frame_area))
        }
    }
}

/// Clears the popup area, draws the standard focused panel border, and
/// returns the inner content rect. Every modal popup opens with this
/// sequence and then renders its content into the returned rect.
fn render_modal_frame(frame: &mut Frame, popup: Rect, title: impl Into<String>) -> Rect {
    clear_area(frame, popup);
    let block = modal_block_owned(title.into());
    let inner = block.inner(popup);
    frame.render_widget(block, popup);
    inner
}

fn render_modal_paragraph(
    frame: &mut Frame,
    popup: Rect,
    title: impl Into<String>,
    lines: Vec<Line<'static>>,
) {
    let inner = render_modal_frame(frame, popup, title);
    frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
}

/// Renders a one-row-per-item popup list with the shared selection viewport.
/// Keeping clipping and the scrollbar here prevents individual list popups from
/// showing page navigation while leaving the selected row off-screen.
fn render_selectable_popup_list(
    frame: &mut Frame,
    popup: Rect,
    title: impl Into<String>,
    lines: Vec<Line<'static>>,
    scroll: usize,
) {
    let inner = render_modal_frame(frame, popup, title);
    let viewport_len = usize::from(inner.height).max(1);
    let range = selection::visible_window(scroll, viewport_len, lines.len());
    let content = Rect {
        width: inner.width.saturating_sub(1).max(1),
        ..inner
    };
    let width = usize::from(content.width).max(1);
    let visible_lines = lines[range.clone()]
        .iter()
        .cloned()
        .map(|line| truncate_line_to_display_width(line, width))
        .collect::<Vec<_>>();
    frame.render_widget(
        Paragraph::new(visible_lines).wrap(Wrap { trim: false }),
        content,
    );
    render_vertical_scrollbar(frame, inner, range.start, viewport_len, lines.len());
}

fn popup_shortcut_help_text(items: &[(&str, &str)]) -> String {
    items
        .iter()
        .map(|(shortcut, description)| format!("[{shortcut}] {description}"))
        .collect::<Vec<_>>()
        .join(" · ")
}

fn popup_button_line(shortcut: &'static str, label: &'static str, active: bool) -> Line<'static> {
    popup_button_line_with_style(shortcut, label, active, Style::default())
}

fn popup_danger_button_line(
    shortcut: &'static str,
    label: &'static str,
    active: bool,
) -> Line<'static> {
    popup_button_line_with_style(
        shortcut,
        label,
        active,
        theme::current().style(theme::HighlightGroup::Error),
    )
}

fn popup_button_line_with_style(
    shortcut: &'static str,
    label: &'static str,
    active: bool,
    label_style: Style,
) -> Line<'static> {
    let active_style = |style| {
        if active {
            theme::current().apply(theme::HighlightGroup::ActiveField, style)
        } else {
            style
        }
    };
    Line::from(vec![
        Span::styled(
            editable_field_marker(active),
            active_style(Style::default()),
        ),
        Span::styled(
            format!("[{shortcut}] "),
            active_style(theme::current().style(theme::HighlightGroup::Shortcut)),
        ),
        Span::styled(label, active_style(label_style)),
    ])
}

fn truncate_line_to_display_width(line: Line<'static>, max_width: usize) -> Line<'static> {
    if max_width == 0 {
        return Line::default();
    }
    let mut remaining = max_width;
    let mut new_spans: Vec<Span<'static>> = Vec::with_capacity(line.spans.len() + 1);
    for span in line.spans {
        if remaining == 0 {
            break;
        }
        if span.content.width() <= remaining {
            remaining = remaining.saturating_sub(span.content.width());
            new_spans.push(span);
            continue;
        }
        let truncated = truncate_display_width(&span.content, remaining);
        remaining = remaining.saturating_sub(truncated.width());
        new_spans.push(Span::styled(truncated, span.style));
    }
    if remaining > 0 {
        new_spans.push(Span::styled(" ".repeat(remaining), line.style));
    }
    let mut truncated = Line::from(new_spans);
    truncated.style = line.style;
    truncated.alignment = line.alignment;
    truncated
}

fn truncate_popup_lines(lines: Vec<Line<'static>>, width: usize) -> Vec<Line<'static>> {
    lines
        .into_iter()
        .map(|line| truncate_line_to_display_width(line, width))
        .collect()
}

fn wrapped_styled_popup_lines(text: &str, width: usize, style: Style) -> Vec<Line<'static>> {
    if width == 0 {
        return vec![Line::from(Span::styled(String::new(), style))];
    }

    let mut lines = Vec::new();
    let mut current = String::new();
    for word in text.split_whitespace() {
        let candidate = if current.is_empty() {
            word.to_owned()
        } else {
            format!("{current} {word}")
        };

        if candidate.width() <= width {
            current = candidate;
            continue;
        }

        if !current.is_empty() {
            lines.push(Line::from(Span::styled(current, style)));
        }
        current = truncate_display_width(word, width);
    }

    if !current.is_empty() {
        lines.push(Line::from(Span::styled(current, style)));
    }
    if lines.is_empty() {
        lines.push(Line::from(Span::styled(String::new(), style)));
    }
    lines
}

fn push_wrapped_styled_popup_text(
    lines: &mut Vec<Line<'static>>,
    text: &str,
    width: usize,
    style: Style,
) {
    lines.extend(wrapped_styled_popup_lines(text, width, style));
}

fn selectable_popup_marker(selected: bool) -> Span<'static> {
    selection_marker_with("", selected)
}

fn editable_field_marker(active: bool) -> &'static str {
    if active { "" } else { "  " }
}

fn editable_field_label_style(active: bool, editing: bool) -> Style {
    if editing {
        theme::current().apply(
            theme::HighlightGroup::Strong,
            theme::current().style(theme::HighlightGroup::Editing),
        )
    } else if active {
        theme::current().style(theme::HighlightGroup::ActiveField)
    } else {
        theme::current().style(theme::HighlightGroup::Disabled)
    }
}

fn editable_field_value_style(active: bool, editing: bool) -> Style {
    if editing {
        theme::current().style(theme::HighlightGroup::Editing)
    } else if active {
        theme::current().style(theme::HighlightGroup::ActiveField)
    } else {
        theme::current().style(theme::HighlightGroup::Disabled)
    }
}

fn editable_tags_section_line(active: bool, required: bool) -> Line<'static> {
    let mut spans = vec![Span::styled(
        format!("{}tags:", editable_field_marker(active)),
        editable_field_label_style(active, false),
    )];
    if required {
        spans.push(Span::styled(
            " required",
            theme::current().style(theme::HighlightGroup::Error),
        ));
    }
    Line::from(spans)
}

fn selectable_popup_shortcut_span(shortcut: impl Into<String>) -> Span<'static> {
    Span::styled(
        shortcut.into(),
        theme::current().style(theme::HighlightGroup::Shortcut),
    )
}

fn selectable_popup_label_style(selected: bool, enabled: bool) -> Style {
    let mut style = if enabled {
        Style::default()
    } else {
        theme::current().style(theme::HighlightGroup::Disabled)
    };
    if selected {
        style = theme::current().apply(theme::HighlightGroup::SelectedRow, style);
    }
    style
}

fn shortcut_prefix(shortcut: Option<char>) -> String {
    shortcut
        .map(|shortcut| format!("[{shortcut}] "))
        .unwrap_or_else(|| "    ".to_owned())
}