1use async_trait::async_trait;
2use crossterm::event;
3use futures::{StreamExt, stream};
4use octocrab::models::{
5 CommentId, Event as IssueEvent, IssueState, issues::Comment as ApiComment,
6 reactions::ReactionContent, timelines::TimelineEvent,
7};
8use pulldown_cmark::{
9 BlockQuoteKind, CodeBlockKind, Event as MdEvent, Options, Parser, Tag, TagEnd, TextMergeStream,
10};
11use rat_cursor::HasScreenCursor;
12use rat_widget::{
13 event::{HandleEvent, Outcome, TextOutcome, ct_event},
14 focus::{FocusBuilder, FocusFlag, HasFocus, Navigation},
15 list::{ListState, selection::RowSelection},
16 paragraph::{Paragraph, ParagraphState},
17 textarea::{TextArea, TextAreaState, TextWrap},
18};
19use ratatui::{
20 buffer::Buffer,
21 layout::Rect,
22 style::{Color, Modifier, Style, Stylize},
23 text::{Line, Span, Text},
24 widgets::{self, Block, ListItem, StatefulWidget, Widget},
25};
26use ratatui_macros::{horizontal, line, span, vertical};
27use std::{
28 collections::{HashMap, HashSet},
29 sync::{Arc, OnceLock},
30};
31use syntect::{
32 easy::HighlightLines,
33 highlighting::{FontStyle, Theme, ThemeSet},
34 parsing::{SyntaxReference, SyntaxSet},
35};
36use textwrap::{core::display_width, wrap};
37use throbber_widgets_tui::{BRAILLE_SIX_DOUBLE, Throbber, ThrobberState, WhichUse};
38use tracing::trace;
39
40use crate::{
41 app::GITHUB_CLIENT,
42 errors::AppError,
43 ui::{
44 Action,
45 components::{
46 Component,
47 help::HelpElementKind,
48 issue_list::{IssueClosePopupState, MainScreen, render_issue_close_popup},
49 },
50 layout::Layout,
51 toast_action,
52 utils::get_border_style,
53 },
54};
55use anyhow::anyhow;
56use hyperrat::Link;
57use ratatui_toaster::{ToastPosition, ToastType};
58
59pub const HELP: &[HelpElementKind] = &[
60 crate::help_text!("Issue Conversation Help"),
61 crate::help_keybind!("Up/Down", "select issue body/comment entry"),
62 crate::help_keybind!("PageUp/PageDown/Home/End", "scroll message body pane"),
63 crate::help_keybind!("t", "toggle timeline events"),
64 crate::help_keybind!("f", "toggle fullscreen body view"),
65 crate::help_keybind!("C", "close selected issue"),
66 crate::help_keybind!("l", "copy link to selected message"),
67 crate::help_keybind!("Enter (popup)", "confirm close reason"),
68 crate::help_keybind!("Ctrl+P", "toggle comment input/preview"),
69 crate::help_keybind!("e", "edit selected comment in external editor"),
70 crate::help_keybind!("r", "add reaction to selected comment"),
71 crate::help_keybind!("R", "remove reaction from selected comment"),
72 crate::help_keybind!("Ctrl+Enter / Alt+Enter", "send comment"),
73 crate::help_keybind!("Esc", "exit fullscreen / return to issue list"),
74];
75
76struct SyntectAssets {
77 syntaxes: SyntaxSet,
78 theme: Theme,
79}
80
81static SYNTECT_ASSETS: OnceLock<SyntectAssets> = OnceLock::new();
82
83fn syntect_assets() -> &'static SyntectAssets {
84 SYNTECT_ASSETS.get_or_init(|| {
85 let syntaxes = SyntaxSet::load_defaults_nonewlines();
86 let theme_set = ThemeSet::load_defaults();
87 let theme = theme_set
88 .themes
89 .get("base16-ocean.dark")
90 .or_else(|| theme_set.themes.values().next())
91 .cloned()
92 .expect("syntect default theme set should include at least one theme");
93 SyntectAssets { syntaxes, theme }
94 })
95}
96
97#[derive(Debug, Clone)]
98pub struct IssueConversationSeed {
99 pub number: u64,
100 pub author: Arc<str>,
101 pub created_at: Arc<str>,
102 pub created_ts: i64,
103 pub body: Option<Arc<str>>,
104 pub title: Option<Arc<str>>,
105}
106
107impl IssueConversationSeed {
108 pub fn from_issue(issue: &octocrab::models::issues::Issue) -> Self {
109 Self {
110 number: issue.number,
111 author: Arc::<str>::from(issue.user.login.as_str()),
112 created_at: Arc::<str>::from(issue.created_at.format("%Y-%m-%d %H:%M").to_string()),
113 created_ts: issue.created_at.timestamp(),
114 body: issue.body.as_ref().map(|b| Arc::<str>::from(b.as_str())),
115 title: Some(Arc::<str>::from(issue.title.as_str())),
116 }
117 }
118}
119
120#[derive(Debug, Clone)]
121pub struct CommentView {
122 pub id: u64,
123 pub author: Arc<str>,
124 pub created_at: Arc<str>,
125 pub created_ts: i64,
126 pub body: Arc<str>,
127 pub reactions: Option<Vec<(ReactionContent, u64)>>,
128 pub my_reactions: Option<Vec<ReactionContent>>,
129}
130
131impl CommentView {
132 pub fn from_api(comment: ApiComment) -> Self {
133 let body = comment.body.unwrap_or_default();
134 Self {
135 id: comment.id.0,
136 author: Arc::<str>::from(comment.user.login.as_str()),
137 created_at: Arc::<str>::from(comment.created_at.format("%Y-%m-%d %H:%M").to_string()),
138 created_ts: comment.created_at.timestamp(),
139 body: Arc::<str>::from(body),
140 reactions: None,
141 my_reactions: None,
142 }
143 }
144}
145
146#[derive(Debug, Clone)]
147pub struct TimelineEventView {
148 pub id: u64,
149 pub created_at: Arc<str>,
150 pub created_ts: i64,
151 pub actor: Arc<str>,
152 pub event: IssueEvent,
153 pub icon: &'static str,
154 pub summary: Arc<str>,
155 pub details: Arc<str>,
156}
157
158impl TimelineEventView {
159 fn from_api(event: TimelineEvent, fallback_id: u64) -> Option<Self> {
160 if matches!(
161 event.event,
162 IssueEvent::Commented | IssueEvent::LineCommented | IssueEvent::CommentDeleted
163 ) {
164 return None;
165 }
166
167 let id = event.id.map(|id| id.0).unwrap_or(fallback_id);
168 let when = event.created_at.or(event.updated_at).or(event.submitted_at);
169 let created_ts = when.map(|d| d.timestamp()).unwrap_or(0);
170 let created_at = Arc::<str>::from(
171 when.map(|d| d.format("%Y-%m-%d %H:%M").to_string())
172 .unwrap_or_else(|| "unknown time".to_string()),
173 );
174 let actor = event
175 .actor
176 .as_ref()
177 .or(event.user.as_ref())
178 .map(|a| Arc::<str>::from(a.login.as_str()))
179 .unwrap_or_else(|| Arc::<str>::from("github"));
180 let (icon, action) = timeline_event_meta(&event.event);
181 let details = timeline_event_details(&event);
182 let summary = Arc::<str>::from(format!("{} {}", actor.as_ref(), action));
183
184 Some(Self {
185 id,
186 created_at,
187 created_ts,
188 actor,
189 event: event.event,
190 icon,
191 summary,
192 details: Arc::<str>::from(details),
193 })
194 }
195}
196
197pub struct IssueConversation {
198 title: Option<Arc<str>>,
199 action_tx: Option<tokio::sync::mpsc::Sender<Action>>,
200 current: Option<IssueConversationSeed>,
201 cache_number: Option<u64>,
202 cache_comments: Vec<CommentView>,
203 timeline_cache_number: Option<u64>,
204 cache_timeline: Vec<TimelineEventView>,
205 markdown_cache: HashMap<u64, MarkdownRender>,
206 body_cache: Option<MarkdownRender>,
207 body_cache_number: Option<u64>,
208 markdown_width: usize,
209 loading: HashSet<u64>,
210 timeline_loading: HashSet<u64>,
211 posting: bool,
212 error: Option<String>,
213 post_error: Option<String>,
214 reaction_error: Option<String>,
215 close_error: Option<String>,
216 timeline_error: Option<String>,
217 owner: String,
218 repo: String,
219 current_user: String,
220 list_state: ListState<RowSelection>,
221 message_keys: Vec<MessageKey>,
222 show_timeline: bool,
223 input_state: TextAreaState,
224 throbber_state: ThrobberState,
225 post_throbber_state: ThrobberState,
226 screen: MainScreen,
227 focus: FocusFlag,
228 area: Rect,
229 textbox_state: InputState,
230 paragraph_state: ParagraphState,
231 body_paragraph_state: ParagraphState,
232 reaction_mode: Option<ReactionMode>,
233 close_popup: Option<IssueClosePopupState>,
234 index: usize,
235}
236
237#[derive(Debug, Default, PartialEq, Eq)]
238enum InputState {
239 #[default]
240 Input,
241 Preview,
242}
243
244#[derive(Debug, Clone, Copy, PartialEq, Eq)]
245enum MessageKey {
246 IssueBody(u64),
247 Comment(u64),
248 Timeline(u64),
249}
250
251#[derive(Debug, Clone, Default)]
252struct MarkdownRender {
253 lines: Vec<Line<'static>>,
254 links: Vec<RenderedLink>,
255}
256
257#[derive(Debug, Clone)]
258struct RenderedLink {
259 line: usize,
260 col: usize,
261 label: String,
262 url: String,
263 width: usize,
264}
265
266#[derive(Debug, Clone)]
267enum ReactionMode {
268 Add {
269 comment_id: u64,
270 selected: usize,
271 },
272 Remove {
273 comment_id: u64,
274 selected: usize,
275 options: Vec<ReactionContent>,
276 },
277}
278
279impl InputState {
280 fn toggle(&mut self) {
281 *self = match self {
282 InputState::Input => InputState::Preview,
283 InputState::Preview => InputState::Input,
284 };
285 }
286}
287
288impl IssueConversation {
289 fn in_details_mode(&self) -> bool {
290 matches!(
291 self.screen,
292 MainScreen::Details | MainScreen::DetailsFullscreen
293 )
294 }
295
296 pub fn new(app_state: crate::ui::AppState) -> Self {
297 Self {
298 title: None,
299 action_tx: None,
300 current: None,
301 cache_number: None,
302 cache_comments: Vec::new(),
303 timeline_cache_number: None,
304 cache_timeline: Vec::new(),
305 markdown_cache: HashMap::new(),
306 paragraph_state: Default::default(),
307 body_cache: None,
308 body_cache_number: None,
309 markdown_width: 0,
310 loading: HashSet::new(),
311 timeline_loading: HashSet::new(),
312 posting: false,
313 error: None,
314 post_error: None,
315 reaction_error: None,
316 close_error: None,
317 timeline_error: None,
318 owner: app_state.owner,
319 repo: app_state.repo,
320 current_user: app_state.current_user,
321 list_state: ListState::default(),
322 message_keys: Vec::new(),
323 show_timeline: false,
324 input_state: TextAreaState::new(),
325 textbox_state: InputState::default(),
326 throbber_state: ThrobberState::default(),
327 post_throbber_state: ThrobberState::default(),
328 screen: MainScreen::default(),
329 focus: FocusFlag::new().with_name("issue_conversation"),
330 area: Rect::default(),
331 body_paragraph_state: ParagraphState::default(),
332 reaction_mode: None,
333 close_popup: None,
334 index: 0,
335 }
336 }
337
338 pub fn render(&mut self, area: Layout, buf: &mut Buffer) {
339 if self.screen == MainScreen::DetailsFullscreen {
340 self.area = area.main_content;
341 self.render_body(area.main_content, buf);
342 return;
343 }
344 self.area = area.main_content;
345 let title = self.title.clone().unwrap_or_default();
346 let wrapped_title = wrap(&title, area.main_content.width.saturating_sub(2) as usize);
347 let title_para_height = wrapped_title.len() as u16 + 2;
348 let last_item = wrapped_title.last();
349 let last_line = last_item
350 .as_ref()
351 .map(|l| {
352 line![
353 l.to_string(),
354 span!(
355 " #{}",
356 self.current.as_ref().map(|s| s.number).unwrap_or_default()
357 )
358 .dim()
359 ]
360 })
361 .unwrap_or_else(|| Line::from(""));
362 let wrapped_title_len = wrapped_title.len() as u16;
363 let title_para = Text::from_iter(
364 wrapped_title
365 .into_iter()
366 .take(wrapped_title_len as usize - 1)
367 .map(Line::from)
368 .chain(std::iter::once(last_line)),
369 );
370
371 let areas = vertical![==title_para_height, *=1, ==5].split(area.main_content);
372 let title_area = areas[0];
373 let content_area = areas[1];
374 let input_area = areas[2];
375 let content_split = horizontal![*=1, *=1].split(content_area);
376 let list_area = content_split[0];
377 let body_area = content_split[1];
378 let items = self.build_items(list_area, body_area);
379
380 let title_widget = widgets::Paragraph::new(title_para)
381 .block(Block::bordered().border_type(ratatui::widgets::BorderType::Rounded))
382 .style(Style::default().add_modifier(Modifier::BOLD));
383 title_widget.render(title_area, buf);
384
385 let mut list_block = Block::bordered()
386 .border_type(ratatui::widgets::BorderType::Rounded)
387 .border_style(get_border_style(&self.list_state));
388
389 if !self.is_loading_current() {
390 let mut title = format!("[{}] Conversation", self.index);
391 title.push_str(if self.show_timeline {
392 " | Timeline: ON"
393 } else {
394 " | Timeline: OFF"
395 });
396 if let Some(prompt) = self.reaction_mode_prompt() {
397 title.push_str(" | ");
398 title.push_str(&prompt);
399 } else if let Some(err) = &self.reaction_error {
400 title.push_str(" | ");
401 title.push_str(err);
402 } else if let Some(err) = &self.close_error {
403 title.push_str(" | ");
404 title.push_str(err);
405 } else if let Some(err) = &self.timeline_error {
406 title.push_str(" | ");
407 title.push_str(err);
408 }
409 list_block = list_block.title(title);
410 }
411
412 let list = rat_widget::list::List::<RowSelection>::new(items)
413 .block(list_block)
414 .style(Style::default())
415 .focus_style(Style::default().bold().reversed())
416 .select_style(Style::default().add_modifier(Modifier::BOLD));
417 list.render(list_area, buf, &mut self.list_state);
418 self.render_body(body_area, buf);
419 if self.is_loading_current() {
420 let title_area = Rect {
421 x: list_area.x + 1,
422 y: list_area.y,
423 width: 10,
424 height: 1,
425 };
426 let throbber = Throbber::default()
427 .label("Loading")
428 .style(Style::new().fg(Color::Cyan))
429 .throbber_set(BRAILLE_SIX_DOUBLE)
430 .use_type(WhichUse::Spin);
431 StatefulWidget::render(throbber, title_area, buf, &mut self.throbber_state);
432 }
433
434 match self.textbox_state {
435 InputState::Input => {
436 let input_title = if let Some(err) = &self.post_error {
437 format!("Comment (Ctrl+Enter to send) | {err}")
438 } else {
439 "Comment (Ctrl+Enter to send)".to_string()
440 };
441 let mut input_block = Block::bordered()
442 .border_type(ratatui::widgets::BorderType::Rounded)
443 .border_style(get_border_style(&self.input_state));
444 if !self.posting {
445 input_block = input_block.title(input_title);
446 }
447 let input_widget = TextArea::new()
448 .block(input_block)
449 .text_wrap(TextWrap::Word(4));
450 input_widget.render(input_area, buf, &mut self.input_state);
451 }
452 InputState::Preview => {
453 let rendered =
454 render_markdown_lines(&self.input_state.text(), self.markdown_width, 2);
455 let para = Paragraph::new(rendered)
456 .block(
457 Block::bordered()
458 .border_type(ratatui::widgets::BorderType::Rounded)
459 .border_style(get_border_style(&self.paragraph_state))
460 .title("Preview"),
461 )
462 .focus_style(Style::default())
463 .hide_focus(true)
464 .wrap(ratatui::widgets::Wrap { trim: true });
465
466 para.render(input_area, buf, &mut self.paragraph_state);
467 }
468 }
469
470 if self.posting {
471 let title_area = Rect {
472 x: input_area.x + 1,
473 y: input_area.y,
474 width: 10,
475 height: 1,
476 };
477 let throbber = Throbber::default()
478 .label("Sending")
479 .style(Style::new().fg(Color::Cyan))
480 .throbber_set(BRAILLE_SIX_DOUBLE)
481 .use_type(WhichUse::Spin);
482 StatefulWidget::render(throbber, title_area, buf, &mut self.post_throbber_state);
483 }
484 self.render_close_popup(area.main_content, buf);
485 }
486
487 fn build_items(&mut self, list_area: Rect, body_area: Rect) -> Vec<ListItem<'static>> {
488 let mut items = Vec::new();
489 let width = body_area.width.saturating_sub(4).max(10) as usize;
490 let preview_width = list_area.width.saturating_sub(12).max(8) as usize;
491 self.message_keys.clear();
492
493 if self.markdown_width != width {
494 self.markdown_width = width;
495 self.markdown_cache.clear();
496 self.body_cache = None;
497 self.body_cache_number = None;
498 }
499
500 if let Some(err) = &self.error {
501 items.push(ListItem::new(line![Span::styled(
502 err.clone(),
503 Style::new().fg(Color::Red)
504 )]));
505 }
506
507 let Some(seed) = &self.current else {
508 items.push(ListItem::new(line![Span::styled(
509 "Press Enter on an issue to view the conversation.".to_string(),
510 Style::new().dim()
511 )]));
512 self.list_state.clear_selection();
513 return items;
514 };
515
516 if let Some(body) = seed
517 .body
518 .as_ref()
519 .map(|b| b.as_ref())
520 .filter(|b| !b.trim().is_empty())
521 {
522 if self.body_cache_number != Some(seed.number) {
523 self.body_cache_number = Some(seed.number);
524 self.body_cache = None;
525 }
526 let body_lines = self
527 .body_cache
528 .get_or_insert_with(|| render_markdown(body, width, 2));
529 items.push(build_comment_preview_item(
530 seed.author.as_ref(),
531 seed.created_at.as_ref(),
532 &body_lines.lines,
533 preview_width,
534 seed.author.as_ref() == self.current_user,
535 None,
536 ));
537 self.message_keys.push(MessageKey::IssueBody(seed.number));
538 }
539
540 if self.cache_number == Some(seed.number) {
541 trace!(
542 "Rendering {} comments for #{}",
543 self.cache_comments.len(),
544 seed.number
545 );
546 let mut merged = self
547 .cache_comments
548 .iter()
549 .map(|comment| (comment.created_ts, MessageKey::Comment(comment.id)))
550 .collect::<Vec<_>>();
551
552 if self.show_timeline && self.timeline_cache_number == Some(seed.number) {
553 merged.extend(
554 self.cache_timeline
555 .iter()
556 .map(|entry| (entry.created_ts, MessageKey::Timeline(entry.id))),
557 );
558 }
559 merged.sort_by_key(|(created_ts, _)| *created_ts);
560
561 for (_, key) in merged {
562 match key {
563 MessageKey::Comment(comment_id) => {
564 if let Some(comment) =
565 self.cache_comments.iter().find(|c| c.id == comment_id)
566 {
567 let body_lines =
568 self.markdown_cache.entry(comment.id).or_insert_with(|| {
569 render_markdown(comment.body.as_ref(), width, 2)
570 });
571 items.push(build_comment_preview_item(
572 comment.author.as_ref(),
573 comment.created_at.as_ref(),
574 &body_lines.lines,
575 preview_width,
576 comment.author.as_ref() == self.current_user,
577 comment.reactions.as_deref(),
578 ));
579 self.message_keys.push(MessageKey::Comment(comment.id));
580 }
581 }
582 MessageKey::Timeline(event_id) => {
583 if let Some(entry) = self.cache_timeline.iter().find(|e| e.id == event_id) {
584 items.push(build_timeline_item(entry, preview_width));
585 self.message_keys.push(MessageKey::Timeline(entry.id));
586 }
587 }
588 MessageKey::IssueBody(_) => {}
589 }
590 }
591 }
592
593 if items.is_empty() {
594 self.list_state.clear_selection();
595 } else {
596 let selected = self.list_state.selected_checked().unwrap_or(0);
597 let clamped = selected.min(items.len() - 1);
598 let _ = self.list_state.select(Some(clamped));
599 }
600
601 items
602 }
603
604 fn render_body(&mut self, body_area: Rect, buf: &mut Buffer) {
605 let selected_body = self.selected_body_render().cloned();
606 let selected_timeline = self.selected_timeline().cloned();
607 let body_lines: Vec<Line<'static>> = if let Some(entry) = selected_timeline.as_ref() {
608 build_timeline_body_lines(entry)
609 } else {
610 selected_body
611 .as_ref()
612 .map(|v| v.lines.clone())
613 .unwrap_or_else(|| {
614 vec![Line::from(vec![Span::styled(
615 "Select a message to view full content.".to_string(),
616 Style::new().dim(),
617 )])]
618 })
619 };
620
621 let body = Paragraph::new(body_lines)
622 .block(
623 Block::bordered()
624 .border_type(ratatui::widgets::BorderType::Rounded)
625 .border_style(get_border_style(&self.body_paragraph_state))
626 .title(if self.screen == MainScreen::DetailsFullscreen {
627 "Message Body (PageUp/PageDown/Home/End | f/Esc: exit fullscreen)"
628 } else {
629 "Message Body (PageUp/PageDown/Home/End)"
630 }),
631 )
632 .focus_style(Style::default())
633 .hide_focus(true);
634
635 body.render(body_area, buf, &mut self.body_paragraph_state);
636
637 if let Some(render) = selected_body.as_ref() {
638 self.render_body_links(body_area, buf, render);
639 }
640 }
641
642 fn selected_body_render(&self) -> Option<&MarkdownRender> {
643 let selected = self.list_state.selected_checked()?;
644 let key = self.message_keys.get(selected)?;
645 match key {
646 MessageKey::IssueBody(number) => {
647 if self.body_cache_number == Some(*number) {
648 self.body_cache.as_ref()
649 } else {
650 None
651 }
652 }
653 MessageKey::Comment(id) => self.markdown_cache.get(id),
654 MessageKey::Timeline(_) => None,
655 }
656 }
657
658 fn selected_timeline(&self) -> Option<&TimelineEventView> {
659 let selected = self.list_state.selected_checked()?;
660 let key = self.message_keys.get(selected)?;
661 match key {
662 MessageKey::Timeline(id) => self.cache_timeline.iter().find(|entry| entry.id == *id),
663 _ => None,
664 }
665 }
666
667 fn render_body_links(&self, body_area: Rect, buf: &mut Buffer, render: &MarkdownRender) {
668 if render.links.is_empty() {
669 return;
670 }
671
672 let inner = Block::bordered()
673 .border_type(ratatui::widgets::BorderType::Rounded)
674 .inner(body_area);
675 if inner.width == 0 || inner.height == 0 {
676 return;
677 }
678
679 let line_offset = self.body_paragraph_state.line_offset();
680 for link in &render.links {
681 let start = link.label.len() - link.label.trim_start_matches(char::is_whitespace).len();
682 let end = link.label.trim_end_matches(char::is_whitespace).len();
683 let trimmed_label = if start < end {
684 &link.label[start..end]
685 } else {
686 continue;
687 };
688 let leading_ws_width = display_width(&link.label[..start]);
689 let link_col = link.col + leading_ws_width;
690 let link_width = display_width(trimmed_label);
691 if link_width == 0 {
692 continue;
693 }
694
695 if link.line < line_offset {
696 continue;
697 }
698
699 let local_y = link.line - line_offset;
700 if local_y >= inner.height as usize || link_col >= inner.width as usize {
701 continue;
702 }
703
704 let available = (inner.width as usize).saturating_sub(link_col);
705 if available == 0 {
706 continue;
707 }
708
709 let link_area = Rect {
710 x: inner.x + link_col as u16,
711 y: inner.y + local_y as u16,
712 width: (available.min(link_width)) as u16,
713 height: 1,
714 };
715 Link::new(trimmed_label, link.url.as_str())
716 .style(
717 Style::new()
718 .fg(Color::Blue)
719 .add_modifier(Modifier::UNDERLINED),
720 )
721 .render(link_area, buf);
722 }
723 }
724
725 fn selected_comment_id(&self) -> Option<u64> {
726 let selected = self.list_state.selected_checked()?;
727 match self.message_keys.get(selected)? {
728 MessageKey::Comment(id) => Some(*id),
729 MessageKey::IssueBody(_) => None,
730 MessageKey::Timeline(_) => None,
731 }
732 }
733
734 fn selected_comment(&self) -> Option<&CommentView> {
735 let id = self.selected_comment_id()?;
736 self.cache_comments.iter().find(|c| c.id == id)
737 }
738
739 async fn open_external_editor_for_comment(
740 &mut self,
741 issue_number: u64,
742 comment_id: u64,
743 initial_body: String,
744 ) {
745 let Some(action_tx) = self.action_tx.clone() else {
746 return;
747 };
748 if action_tx
749 .send(Action::EditorModeChanged(true))
750 .await
751 .is_err()
752 {
753 return;
754 }
755
756 tokio::spawn(async move {
757 let result = tokio::task::spawn_blocking(move || {
758 ratatui::restore();
759 let edited = edit::edit(&initial_body).map_err(|err| err.to_string());
760 let _ = ratatui::init();
761 edited
762 })
763 .await
764 .map_err(|err| err.to_string())
765 .and_then(|edited| edited.map_err(|err| err.replace('\n', " ")));
766
767 let _ = action_tx.send(Action::EditorModeChanged(false)).await;
768 let _ = action_tx
769 .send(Action::IssueCommentEditFinished {
770 issue_number,
771 comment_id,
772 result,
773 })
774 .await;
775 let _ = action_tx.send(Action::ForceRender).await;
776 });
777 }
778
779 async fn patch_comment(&mut self, issue_number: u64, comment_id: u64, body: String) {
780 let Some(action_tx) = self.action_tx.clone() else {
781 return;
782 };
783 let owner = self.owner.clone();
784 let repo = self.repo.clone();
785
786 tokio::spawn(async move {
787 let Some(client) = GITHUB_CLIENT.get() else {
788 let _ = action_tx
789 .send(Action::IssueCommentEditFinished {
790 issue_number,
791 comment_id,
792 result: Err("GitHub client not initialized.".to_string()),
793 })
794 .await;
795 return;
796 };
797
798 let handler = client.inner().issues(owner, repo);
799 match handler.update_comment(CommentId(comment_id), body).await {
800 Ok(comment) => {
801 let _ = action_tx
802 .send(Action::IssueCommentPatched {
803 issue_number,
804 comment: CommentView::from_api(comment),
805 })
806 .await;
807 }
808 Err(err) => {
809 let _ = action_tx
810 .send(Action::IssueCommentEditFinished {
811 issue_number,
812 comment_id,
813 result: Err(err.to_string().replace('\n', " ")),
814 })
815 .await;
816 }
817 }
818 });
819 }
820
821 fn reaction_mode_prompt(&self) -> Option<String> {
822 let mode = self.reaction_mode.as_ref()?;
823 match mode {
824 ReactionMode::Add { selected, .. } => Some(format!(
825 "Add reaction: {}",
826 format_reaction_picker(*selected, &reaction_add_options())
827 )),
828 ReactionMode::Remove {
829 selected, options, ..
830 } => Some(format!(
831 "Remove reaction: {}",
832 format_reaction_picker(*selected, options)
833 )),
834 }
835 }
836
837 fn open_close_popup(&mut self) {
838 let Some(seed) = &self.current else {
839 self.close_error = Some("No issue selected.".to_string());
840 return;
841 };
842 self.close_error = None;
843 self.close_popup = Some(IssueClosePopupState::new(seed.number));
844 }
845
846 fn render_close_popup(&mut self, area: Rect, buf: &mut Buffer) {
847 let Some(popup) = self.close_popup.as_mut() else {
848 return;
849 };
850 render_issue_close_popup(popup, area, buf);
851 }
852
853 async fn submit_close_popup(&mut self) {
854 let Some(popup) = self.close_popup.as_mut() else {
855 return;
856 };
857 if popup.loading {
858 return;
859 }
860 let reason = popup.selected_reason();
861 let number = popup.issue_number;
862 popup.loading = true;
863 popup.error = None;
864
865 let Some(action_tx) = self.action_tx.clone() else {
866 popup.loading = false;
867 popup.error = Some("Action channel unavailable.".to_string());
868 return;
869 };
870 let owner = self.owner.clone();
871 let repo = self.repo.clone();
872 tokio::spawn(async move {
873 let Some(client) = GITHUB_CLIENT.get() else {
874 let _ = action_tx
875 .send(Action::IssueCloseError {
876 number,
877 message: "GitHub client not initialized.".to_string(),
878 })
879 .await;
880 return;
881 };
882 let issues = client.inner().issues(owner, repo);
883 match issues
884 .update(number)
885 .state(IssueState::Closed)
886 .state_reason(reason.to_octocrab())
887 .send()
888 .await
889 {
890 Ok(issue) => {
891 let _ = action_tx
892 .send(Action::IssueCloseSuccess {
893 issue: Box::new(issue),
894 })
895 .await;
896 }
897 Err(err) => {
898 let _ = action_tx
899 .send(Action::IssueCloseError {
900 number,
901 message: err.to_string().replace('\n', " "),
902 })
903 .await;
904 }
905 }
906 });
907 }
908
909 async fn handle_close_popup_event(&mut self, event: &event::Event) -> bool {
910 let Some(popup) = self.close_popup.as_mut() else {
911 return false;
912 };
913 if popup.loading {
914 if matches!(event, ct_event!(keycode press Esc)) {
915 popup.loading = false;
916 }
917 return true;
918 }
919 if matches!(event, ct_event!(keycode press Esc)) {
920 self.close_popup = None;
921 return true;
922 }
923 if matches!(event, ct_event!(keycode press Up)) {
924 popup.select_prev_reason();
925 return true;
926 }
927 if matches!(event, ct_event!(keycode press Down)) {
928 popup.select_next_reason();
929 return true;
930 }
931 if matches!(event, ct_event!(keycode press Enter)) {
932 self.submit_close_popup().await;
933 return true;
934 }
935 true
936 }
937
938 fn start_add_reaction_mode(&mut self) {
939 let Some(comment_id) = self.selected_comment_id() else {
940 self.reaction_error = Some("Select a comment to add a reaction.".to_string());
941 return;
942 };
943 self.reaction_error = None;
944 self.reaction_mode = Some(ReactionMode::Add {
945 comment_id,
946 selected: 0,
947 });
948 }
949
950 fn start_remove_reaction_mode(&mut self) {
951 let Some(comment) = self.selected_comment() else {
952 self.reaction_error = Some("Select a comment to remove a reaction.".to_string());
953 return;
954 };
955 let comment_id = comment.id;
956 let mut options = comment.my_reactions.as_ref().cloned().unwrap_or_default();
957
958 options.sort_by_key(reaction_order);
959 options.dedup();
960 if options.is_empty() {
961 self.reaction_error = Some("No reactions available to remove.".to_string());
962 return;
963 }
964 self.reaction_error = None;
965 self.reaction_mode = Some(ReactionMode::Remove {
966 comment_id,
967 selected: 0,
968 options,
969 });
970 }
971
972 async fn handle_reaction_mode_event(&mut self, event: &event::Event) -> bool {
973 let Some(mode) = &mut self.reaction_mode else {
974 return false;
975 };
976
977 let mut submit: Option<(u64, ReactionContent, bool)> = None;
978 match event {
979 ct_event!(keycode press Esc) => {
980 self.reaction_mode = None;
981 return true;
982 }
983 ct_event!(keycode press Up) => match mode {
984 ReactionMode::Add { selected, .. } => {
985 let len = reaction_add_options().len();
986 if len > 0 {
987 *selected = if *selected == 0 {
988 len - 1
989 } else {
990 *selected - 1
991 };
992 }
993 return true;
994 }
995 ReactionMode::Remove {
996 selected, options, ..
997 } => {
998 let len = options.len();
999 if len > 0 {
1000 *selected = if *selected == 0 {
1001 len - 1
1002 } else {
1003 *selected - 1
1004 };
1005 }
1006 return true;
1007 }
1008 },
1009 ct_event!(keycode press Down) => match mode {
1010 ReactionMode::Add { selected, .. } => {
1011 let len = reaction_add_options().len();
1012 if len > 0 {
1013 *selected = (*selected + 1) % len;
1014 }
1015 return true;
1016 }
1017 ReactionMode::Remove {
1018 selected, options, ..
1019 } => {
1020 let len = options.len();
1021 if len > 0 {
1022 *selected = (*selected + 1) % len;
1023 }
1024 return true;
1025 }
1026 },
1027 ct_event!(keycode press Enter) => match mode {
1028 ReactionMode::Add {
1029 comment_id,
1030 selected,
1031 } => {
1032 let options = reaction_add_options();
1033 if let Some(content) = options.get(*selected).cloned() {
1034 submit = Some((*comment_id, content, true));
1035 }
1036 }
1037 ReactionMode::Remove {
1038 comment_id,
1039 selected,
1040 options,
1041 } => {
1042 if let Some(content) = options.get(*selected).cloned() {
1043 submit = Some((*comment_id, content, false));
1044 }
1045 }
1046 },
1047 _ => return false,
1048 }
1049
1050 if let Some((comment_id, content, add)) = submit {
1051 self.reaction_mode = None;
1052 self.reaction_error = None;
1053 if add {
1054 self.add_reaction(comment_id, content).await;
1055 } else {
1056 self.remove_reaction(comment_id, content).await;
1057 }
1058 return true;
1059 }
1060 true
1061 }
1062
1063 fn is_loading_current(&self) -> bool {
1064 self.current.as_ref().is_some_and(|seed| {
1065 self.loading.contains(&seed.number)
1066 || (self.show_timeline && self.timeline_loading.contains(&seed.number))
1067 })
1068 }
1069
1070 fn has_timeline_for(&self, number: u64) -> bool {
1071 self.timeline_cache_number == Some(number)
1072 }
1073
1074 async fn add_reaction(&mut self, comment_id: u64, content: ReactionContent) {
1075 let Some(action_tx) = self.action_tx.clone() else {
1076 return;
1077 };
1078 let owner = self.owner.clone();
1079 let repo = self.repo.clone();
1080 let current_user = self.current_user.clone();
1081 tokio::spawn(async move {
1082 let Some(client) = GITHUB_CLIENT.get() else {
1083 let _ = action_tx
1084 .send(Action::IssueReactionEditError {
1085 comment_id,
1086 message: "GitHub client not initialized.".to_string(),
1087 })
1088 .await;
1089 return;
1090 };
1091 let handler = client.inner().issues(owner, repo);
1092 if let Err(err) = handler.create_comment_reaction(comment_id, content).await {
1093 let _ = action_tx
1094 .send(Action::IssueReactionEditError {
1095 comment_id,
1096 message: err.to_string().replace('\n', " "),
1097 })
1098 .await;
1099 return;
1100 }
1101
1102 match handler.list_comment_reactions(comment_id).send().await {
1103 Ok(mut page) => {
1104 let (counts, mine) =
1105 to_reaction_snapshot(std::mem::take(&mut page.items), ¤t_user);
1106 let mut reactions = HashMap::new();
1107 let mut own_reactions = HashMap::new();
1108 reactions.insert(comment_id, counts);
1109 own_reactions.insert(comment_id, mine);
1110 let _ = action_tx
1111 .send(Action::IssueReactionsLoaded {
1112 reactions,
1113 own_reactions,
1114 })
1115 .await;
1116 }
1117 Err(err) => {
1118 let _ = action_tx
1119 .send(Action::IssueReactionEditError {
1120 comment_id,
1121 message: err.to_string().replace('\n', " "),
1122 })
1123 .await;
1124 }
1125 }
1126 });
1127 }
1128
1129 async fn remove_reaction(&mut self, comment_id: u64, content: ReactionContent) {
1130 let Some(action_tx) = self.action_tx.clone() else {
1131 return;
1132 };
1133 let owner = self.owner.clone();
1134 let repo = self.repo.clone();
1135 let current_user = self.current_user.clone();
1136 tokio::spawn(async move {
1137 let Some(client) = GITHUB_CLIENT.get() else {
1138 let _ = action_tx
1139 .send(Action::IssueReactionEditError {
1140 comment_id,
1141 message: "GitHub client not initialized.".to_string(),
1142 })
1143 .await;
1144 return;
1145 };
1146 let handler = client.inner().issues(owner, repo);
1147 match handler.list_comment_reactions(comment_id).send().await {
1148 Ok(mut page) => {
1149 let mut items = std::mem::take(&mut page.items);
1150 let to_delete = items
1151 .iter()
1152 .find(|reaction| {
1153 reaction.content == content
1154 && reaction.user.login.eq_ignore_ascii_case(¤t_user)
1155 })
1156 .map(|reaction| reaction.id);
1157
1158 let Some(reaction_id) = to_delete else {
1159 let _ = action_tx
1160 .send(Action::IssueReactionEditError {
1161 comment_id,
1162 message: "No matching reaction from current user.".to_string(),
1163 })
1164 .await;
1165 return;
1166 };
1167
1168 if let Err(err) = handler
1169 .delete_comment_reaction(comment_id, reaction_id)
1170 .await
1171 {
1172 let _ = action_tx
1173 .send(Action::IssueReactionEditError {
1174 comment_id,
1175 message: err.to_string().replace('\n', " "),
1176 })
1177 .await;
1178 return;
1179 }
1180
1181 let mut removed = false;
1182 let (counts, mine) = to_reaction_snapshot(
1183 items.drain(..).filter_map(|reaction| {
1184 if !removed
1185 && reaction.content == content
1186 && reaction.user.login.eq_ignore_ascii_case(¤t_user)
1187 {
1188 removed = true;
1189 None
1190 } else {
1191 Some(reaction)
1192 }
1193 }),
1194 ¤t_user,
1195 );
1196 let mut reactions = HashMap::new();
1197 let mut own_reactions = HashMap::new();
1198 reactions.insert(comment_id, counts);
1199 own_reactions.insert(comment_id, mine);
1200 let _ = action_tx
1201 .send(Action::IssueReactionsLoaded {
1202 reactions,
1203 own_reactions,
1204 })
1205 .await;
1206 }
1207 Err(err) => {
1208 let _ = action_tx
1209 .send(Action::IssueReactionEditError {
1210 comment_id,
1211 message: err.to_string().replace('\n', " "),
1212 })
1213 .await;
1214 }
1215 }
1216 });
1217 }
1218
1219 async fn fetch_comments(&mut self, number: u64) {
1220 if self.loading.contains(&number) {
1221 return;
1222 }
1223 let Some(action_tx) = self.action_tx.clone() else {
1224 return;
1225 };
1226 let owner = self.owner.clone();
1227 let repo = self.repo.clone();
1228 let current_user = self.current_user.clone();
1229 self.loading.insert(number);
1230 self.error = None;
1231
1232 tokio::spawn(async move {
1233 let Some(client) = GITHUB_CLIENT.get() else {
1234 let _ = action_tx
1235 .send(Action::IssueCommentsError {
1236 number,
1237 message: "GitHub client not initialized.".to_string(),
1238 })
1239 .await;
1240 return;
1241 };
1242 let handler = client.inner().issues(owner, repo);
1243 let page = handler
1244 .list_comments(number)
1245 .per_page(100u8)
1246 .page(1u32)
1247 .send()
1248 .await;
1249
1250 match page {
1251 Ok(mut p) => {
1252 let comments = std::mem::take(&mut p.items);
1253 let comment_ids = comments.iter().map(|c| c.id.0).collect::<Vec<_>>();
1254 let comments: Vec<CommentView> =
1255 comments.into_iter().map(CommentView::from_api).collect();
1256 trace!("Loaded {} comments for issue {}", comments.len(), number);
1257 let _ = action_tx
1258 .send(Action::IssueCommentsLoaded { number, comments })
1259 .await;
1260 let refer = &handler;
1261 let current_user = current_user.clone();
1262 let reaction_snapshots = stream::iter(comment_ids)
1263 .filter_map(|id| {
1264 let current_user = current_user.clone();
1265 async move {
1266 let reactions = refer.list_comment_reactions(id).send().await;
1267 let mut page = reactions.ok()?;
1268 Some((
1269 id,
1270 to_reaction_snapshot(
1271 std::mem::take(&mut page.items),
1272 ¤t_user,
1273 ),
1274 ))
1275 }
1276 })
1277 .collect::<HashMap<_, _>>()
1278 .await;
1279 let mut reactions = HashMap::with_capacity(reaction_snapshots.len());
1280 let mut own_reactions = HashMap::with_capacity(reaction_snapshots.len());
1281 for (id, (counts, mine)) in reaction_snapshots {
1282 reactions.insert(id, counts);
1283 own_reactions.insert(id, mine);
1284 }
1285 let _ = action_tx
1286 .send(Action::IssueReactionsLoaded {
1287 reactions,
1288 own_reactions,
1289 })
1290 .await;
1291 }
1292 Err(err) => {
1293 let _ = action_tx
1294 .send(Action::IssueCommentsError {
1295 number,
1296 message: err.to_string().replace('\n', " "),
1297 })
1298 .await;
1299 }
1300 }
1301 });
1302 }
1303
1304 async fn fetch_timeline(&mut self, number: u64) {
1305 if self.timeline_loading.contains(&number) {
1306 return;
1307 }
1308 let Some(action_tx) = self.action_tx.clone() else {
1309 return;
1310 };
1311 let owner = self.owner.clone();
1312 let repo = self.repo.clone();
1313 self.timeline_loading.insert(number);
1314 self.timeline_error = None;
1315
1316 tokio::spawn(async move {
1317 let Some(client) = GITHUB_CLIENT.get() else {
1318 let _ = action_tx
1319 .send(Action::IssueTimelineError {
1320 number,
1321 message: "GitHub client not initialized.".to_string(),
1322 })
1323 .await;
1324 return;
1325 };
1326 let handler = client.inner().issues(owner, repo);
1327 match handler
1328 .list_timeline_events(number)
1329 .per_page(100u8)
1330 .page(1u32)
1331 .send()
1332 .await
1333 {
1334 Ok(mut page) => {
1335 let events = std::mem::take(&mut page.items)
1336 .into_iter()
1337 .enumerate()
1338 .filter_map(|(idx, event)| {
1339 TimelineEventView::from_api(event, (number << 32) | idx as u64)
1340 })
1341 .collect::<Vec<_>>();
1342 let _ = action_tx
1343 .send(Action::IssueTimelineLoaded { number, events })
1344 .await;
1345 }
1346 Err(err) => {
1347 let _ = action_tx
1348 .send(Action::IssueTimelineError {
1349 number,
1350 message: err.to_string().replace('\n', " "),
1351 })
1352 .await;
1353 }
1354 }
1355 });
1356 }
1357
1358 async fn send_comment(&mut self, number: u64, body: String) {
1359 let Some(action_tx) = self.action_tx.clone() else {
1360 return;
1361 };
1362 let owner = self.owner.clone();
1363 let repo = self.repo.clone();
1364 self.posting = true;
1365 self.post_error = None;
1366
1367 tokio::spawn(async move {
1368 let Some(client) = GITHUB_CLIENT.get() else {
1369 let _ = action_tx
1370 .send(Action::IssueCommentPostError {
1371 number,
1372 message: "GitHub client not initialized.".to_string(),
1373 })
1374 .await;
1375 return;
1376 };
1377 let handler = client.inner().issues(owner, repo);
1378 match handler.create_comment(number, body).await {
1379 Ok(comment) => {
1380 let _ = action_tx
1381 .send(Action::IssueCommentPosted {
1382 number,
1383 comment: CommentView::from_api(comment),
1384 })
1385 .await;
1386 let _ = action_tx
1387 .send(toast_action("Comment Sent!", ToastType::Success))
1388 .await;
1389 }
1390 Err(err) => {
1391 let _ = action_tx
1392 .send(Action::IssueCommentPostError {
1393 number,
1394 message: err.to_string().replace('\n', " "),
1395 })
1396 .await;
1397 let _ = action_tx
1398 .send(toast_action("Failed to send comment", ToastType::Error))
1399 .await;
1400 }
1401 }
1402 });
1403 }
1404}
1405
1406#[async_trait(?Send)]
1407impl Component for IssueConversation {
1408 fn render(&mut self, area: Layout, buf: &mut Buffer) {
1409 self.render(area, buf);
1410 }
1411
1412 fn register_action_tx(&mut self, action_tx: tokio::sync::mpsc::Sender<Action>) {
1413 self.action_tx = Some(action_tx);
1414 }
1415
1416 async fn handle_event(&mut self, event: Action) -> Result<(), AppError> {
1417 match event {
1418 Action::AppEvent(ref event) => {
1419 if !self.in_details_mode() {
1420 return Ok(());
1421 }
1422 if self.screen == MainScreen::DetailsFullscreen
1423 && matches!(
1424 event,
1425 ct_event!(key press 'f') | ct_event!(keycode press Esc)
1426 )
1427 {
1428 if let Some(tx) = self.action_tx.clone() {
1429 let _ = tx
1430 .send(Action::ChangeIssueScreen(MainScreen::Details))
1431 .await;
1432 }
1433 return Ok(());
1434 }
1435 if self.handle_close_popup_event(event).await {
1436 return Ok(());
1437 }
1438 if self.handle_reaction_mode_event(event).await {
1439 return Ok(());
1440 }
1441
1442 match event {
1443 event::Event::Key(key)
1444 if key.code == event::KeyCode::Char('t')
1445 && key.modifiers == event::KeyModifiers::NONE
1446 && (self.list_state.is_focused()
1447 || self.body_paragraph_state.is_focused()) =>
1448 {
1449 self.show_timeline = !self.show_timeline;
1450 self.timeline_error = None;
1451 if self.show_timeline
1452 && let Some(seed) = self.current.as_ref()
1453 && !self.has_timeline_for(seed.number)
1454 {
1455 self.fetch_timeline(seed.number).await;
1456 }
1457 if let Some(tx) = self.action_tx.clone() {
1458 let _ = tx.send(Action::ForceRender).await;
1459 }
1460 return Ok(());
1461 }
1462 ct_event!(key press 'l')
1463 if self.body_paragraph_state.is_focused()
1464 || self.list_state.is_focused() =>
1465 {
1466 let Some(current) = self.current.as_ref() else {
1467 return Ok(());
1468 };
1469 let Some(selected_idx) = self.list_state.selected_checked() else {
1470 return Ok(());
1471 };
1472
1473 let Some(selected) = self.message_keys.get(selected_idx) else {
1474 return Ok(());
1475 };
1476
1477 match selected {
1478 MessageKey::IssueBody(i) => {
1479 assert_eq!(*i, current.number);
1480 let link = format!(
1481 "https://github.com/{}/{}/issues/{}",
1482 self.owner, self.repo, i
1483 );
1484 cli_clipboard::set_contents(link)
1485 .map_err(|_| anyhow!("Error copying to clipboard"))?;
1486 }
1487 MessageKey::Comment(id) => {
1488 let link = format!(
1489 "https://github.com/{}/{}/issues/{}#issuecomment-{}",
1490 self.owner, self.repo, current.number, id
1491 );
1492
1493 cli_clipboard::set_contents(link)
1494 .map_err(|_| anyhow!("Error copying to clipboard"))?;
1495 }
1496 _ => {
1497 return Ok(());
1498 }
1499 }
1500 if let Some(tx) = self.action_tx.clone() {
1501 tx.send(Action::ToastAction(ratatui_toaster::ToastMessage::Show {
1502 message: "Copied Link".to_string(),
1503 toast_type: ToastType::Success,
1504 position: ToastPosition::TopRight,
1505 }))
1506 .await?;
1507 tx.send(Action::ForceRender).await?;
1508 }
1509 }
1510 event::Event::Key(key)
1511 if key.code == event::KeyCode::Char('f')
1512 && key.modifiers == event::KeyModifiers::NONE
1513 && self.screen == MainScreen::Details
1514 && self.body_paragraph_state.is_focused() =>
1515 {
1516 if let Some(tx) = self.action_tx.clone() {
1517 let _ = tx
1518 .send(Action::ChangeIssueScreen(MainScreen::DetailsFullscreen))
1519 .await;
1520 }
1521 return Ok(());
1522 }
1523 event::Event::Key(key)
1524 if key.code == event::KeyCode::Char('e')
1525 && key.modifiers == event::KeyModifiers::NONE
1526 && (self.list_state.is_focused()
1527 || self.body_paragraph_state.is_focused()) =>
1528 {
1529 let seed = self.current.as_ref().ok_or_else(|| {
1530 AppError::Other(anyhow!("no issue selected for comment editing"))
1531 })?;
1532 let comment = self
1533 .selected_comment()
1534 .ok_or_else(|| AppError::Other(anyhow!("select a comment to edit")))?;
1535 self.open_external_editor_for_comment(
1536 seed.number,
1537 comment.id,
1538 comment.body.to_string(),
1539 )
1540 .await;
1541 return Ok(());
1542 }
1543 event::Event::Key(key)
1544 if key.code == event::KeyCode::Char('r')
1545 && key.modifiers == event::KeyModifiers::NONE
1546 && self.list_state.is_focused() =>
1547 {
1548 self.start_add_reaction_mode();
1549 return Ok(());
1550 }
1551 event::Event::Key(key)
1552 if key.code == event::KeyCode::Char('R')
1553 && self.list_state.is_focused() =>
1554 {
1555 self.start_remove_reaction_mode();
1556 return Ok(());
1557 }
1558 event::Event::Key(key)
1559 if key.code == event::KeyCode::Char('C')
1560 && (self.list_state.is_focused()
1561 || self.body_paragraph_state.is_focused()) =>
1562 {
1563 self.open_close_popup();
1564 return Ok(());
1565 }
1566 ct_event!(keycode press Tab) | ct_event!(keycode press BackTab)
1567 if self.input_state.is_focused() =>
1568 {
1569 let action_tx = self.action_tx.as_ref().ok_or_else(|| {
1570 AppError::Other(anyhow!(
1571 "issue conversation action channel unavailable"
1572 ))
1573 })?;
1574 action_tx.send(Action::ForceFocusChange).await?;
1575 }
1576 ct_event!(keycode press Esc) if self.body_paragraph_state.is_focused() => {
1577 let action_tx = self.action_tx.as_ref().ok_or_else(|| {
1578 AppError::Other(anyhow!(
1579 "issue conversation action channel unavailable"
1580 ))
1581 })?;
1582 action_tx.send(Action::ForceFocusChangeRev).await?;
1583 }
1584 ct_event!(keycode press Esc) if !self.body_paragraph_state.is_focused() => {
1585 if let Some(tx) = self.action_tx.clone() {
1586 let _ = tx.send(Action::ChangeIssueScreen(MainScreen::List)).await;
1587 }
1588 return Ok(());
1589 }
1590 ct_event!(key press CONTROL-'p') => {
1591 self.textbox_state.toggle();
1592 match self.textbox_state {
1593 InputState::Input => {
1594 self.input_state.focus.set(true);
1595 self.paragraph_state.focus.set(false);
1596 }
1597 InputState::Preview => {
1598 self.input_state.focus.set(false);
1599 self.paragraph_state.focus.set(true);
1600 }
1601 }
1602 if let Some(ref tx) = self.action_tx {
1603 let _ = tx.send(Action::ForceRender).await;
1604 }
1605 }
1606 ct_event!(keycode press Enter) if self.list_state.is_focused() => {
1607 let action_tx = self.action_tx.as_ref().ok_or_else(|| {
1608 AppError::Other(anyhow!(
1609 "issue conversation action channel unavailable"
1610 ))
1611 })?;
1612 action_tx.send(Action::ForceFocusChange).await?;
1613 }
1614 ct_event!(keycode press CONTROL-Enter) | ct_event!(keycode press ALT-Enter) => {
1615 let Some(seed) = &self.current else {
1616 return Ok(());
1617 };
1618 let body = self.input_state.text();
1619 let trimmed = body.trim();
1620 if trimmed.is_empty() {
1621 self.post_error = Some("Comment cannot be empty.".to_string());
1622 return Ok(());
1623 }
1624 self.input_state.set_text("");
1625 self.send_comment(seed.number, trimmed.to_string()).await;
1626 return Ok(());
1627 }
1628
1629 ct_event!(key press '>')
1630 if self.list_state.is_focused()
1631 || self.body_paragraph_state.is_focused() =>
1632 {
1633 if let Some(comment) = self.selected_comment() {
1634 let comment_body = comment.body.as_ref();
1635 let quoted = comment_body
1636 .lines()
1637 .map(|line| format!("> {}", line.trim()))
1638 .collect::<Vec<_>>()
1639 .join("\n");
1640 self.input_state.insert_str("ed);
1641 self.input_state.insert_newline();
1642 self.input_state.move_to_end(false);
1643 self.input_state.move_to_line_end(false);
1644 self.input_state.focus.set(true);
1645 self.list_state.focus.set(false);
1646 }
1647 }
1648
1649 event::Event::Key(key) if key.code != event::KeyCode::Tab => {
1650 let o = self.input_state.handle(event, rat_widget::event::Regular);
1651 let o2 = self
1652 .paragraph_state
1653 .handle(event, rat_widget::event::Regular);
1654 if matches!(
1655 event,
1656 ct_event!(keycode press Up)
1657 | ct_event!(keycode press Down)
1658 | ct_event!(keycode press Left)
1659 | ct_event!(keycode press Right)
1660 ) {
1661 let action_tx = self.action_tx.as_ref().ok_or_else(|| {
1662 AppError::Other(anyhow!(
1663 "issue conversation action channel unavailable"
1664 ))
1665 })?;
1666 action_tx.send(Action::ForceRender).await?;
1667 }
1668 if o == TextOutcome::TextChanged || o2 == Outcome::Changed {
1669 trace!("Input changed, forcing re-render");
1670 let action_tx = self.action_tx.as_ref().ok_or_else(|| {
1671 AppError::Other(anyhow!(
1672 "issue conversation action channel unavailable"
1673 ))
1674 })?;
1675 action_tx.send(Action::ForceRender).await?;
1676 }
1677 }
1678 event::Event::Paste(p) if self.input_state.is_focused() => {
1679 self.input_state.insert_str(p);
1680 let action_tx = self.action_tx.as_ref().ok_or_else(|| {
1681 AppError::Other(anyhow!(
1682 "issue conversation action channel unavailable"
1683 ))
1684 })?;
1685 action_tx.send(Action::ForceRender).await?;
1686 }
1687 _ => {}
1688 }
1689 self.body_paragraph_state
1690 .handle(event, rat_widget::event::Regular);
1691 let outcome = self.list_state.handle(event, rat_widget::event::Regular);
1692 if outcome == rat_widget::event::Outcome::Changed {
1693 self.body_paragraph_state.set_line_offset(0);
1694 }
1695 }
1696 Action::EnterIssueDetails { seed } => {
1697 let number = seed.number;
1698 self.title = seed.title.clone();
1699 self.current = Some(seed);
1700 self.post_error = None;
1701 self.reaction_error = None;
1702 self.close_error = None;
1703 self.reaction_mode = None;
1704 self.close_popup = None;
1705 self.timeline_error = None;
1706 self.body_cache = None;
1707 self.body_cache_number = Some(number);
1708 self.body_paragraph_state.set_line_offset(0);
1709 if self.cache_number != Some(number) {
1710 self.cache_number = None;
1711 self.cache_comments.clear();
1712 self.markdown_cache.clear();
1713 }
1714 if self.timeline_cache_number != Some(number) {
1715 self.timeline_cache_number = None;
1716 self.cache_timeline.clear();
1717 }
1718 if self.cache_number == Some(number) {
1719 self.loading.remove(&number);
1720 self.error = None;
1721 } else {
1722 self.fetch_comments(number).await;
1723 }
1724 if self.show_timeline {
1725 if self.has_timeline_for(number) {
1726 self.timeline_loading.remove(&number);
1727 } else {
1728 self.fetch_timeline(number).await;
1729 }
1730 }
1731 }
1732 Action::IssueCommentsLoaded { number, comments } => {
1733 self.loading.remove(&number);
1734 if self.current.as_ref().is_some_and(|s| s.number == number) {
1735 self.cache_number = Some(number);
1736 trace!("Setting {} comments for #{}", comments.len(), number);
1737 self.cache_comments = comments;
1738 self.markdown_cache.clear();
1739 self.body_cache = None;
1740 self.body_paragraph_state.set_line_offset(0);
1741 self.error = None;
1742 let action_tx = self.action_tx.as_ref().ok_or_else(|| {
1743 AppError::Other(anyhow!("issue conversation action channel unavailable"))
1744 })?;
1745 action_tx.send(Action::ForceRender).await?;
1746 }
1747 }
1748 Action::IssueReactionsLoaded {
1749 reactions,
1750 own_reactions,
1751 } => {
1752 self.reaction_error = None;
1753 for (id, reaction_content) in reactions {
1754 if let Some(comment) = self.cache_comments.iter_mut().find(|c| c.id == id) {
1755 comment.reactions = Some(reaction_content);
1756 comment.my_reactions =
1757 Some(own_reactions.get(&id).cloned().unwrap_or_else(Vec::new));
1758 }
1759 }
1760 }
1761 Action::IssueReactionEditError {
1762 comment_id: _,
1763 message,
1764 } => {
1765 self.reaction_error = Some(message);
1766 }
1767 Action::IssueCommentPosted { number, comment } => {
1768 self.posting = false;
1769 if self.current.as_ref().is_some_and(|s| s.number == number) {
1770 if self.cache_number == Some(number) {
1771 self.cache_comments.push(comment);
1772 } else {
1773 self.cache_number = Some(number);
1774 self.cache_comments.clear();
1775 self.cache_comments.push(comment);
1776 self.markdown_cache.clear();
1777 self.body_cache = None;
1778 }
1779 }
1780 }
1781 Action::IssueCommentsError { number, message } => {
1782 self.loading.remove(&number);
1783 if self.current.as_ref().is_some_and(|s| s.number == number) {
1784 self.error = Some(message);
1785 }
1786 }
1787 Action::IssueTimelineLoaded { number, events } => {
1788 self.timeline_loading.remove(&number);
1789 if self.current.as_ref().is_some_and(|s| s.number == number) {
1790 self.timeline_cache_number = Some(number);
1791 self.cache_timeline = events;
1792 self.timeline_error = None;
1793 if let Some(action_tx) = self.action_tx.as_ref() {
1794 let _ = action_tx.send(Action::ForceRender).await;
1795 }
1796 }
1797 }
1798 Action::IssueTimelineError { number, message } => {
1799 self.timeline_loading.remove(&number);
1800 if self.current.as_ref().is_some_and(|s| s.number == number) {
1801 self.timeline_error = Some(message);
1802 }
1803 }
1804 Action::IssueCommentPostError { number, message } => {
1805 self.posting = false;
1806 if self.current.as_ref().is_some_and(|s| s.number == number) {
1807 self.post_error = Some(message);
1808 }
1809 }
1810 Action::IssueCommentEditFinished {
1811 issue_number,
1812 comment_id,
1813 result,
1814 } => {
1815 if self
1816 .current
1817 .as_ref()
1818 .is_none_or(|seed| seed.number != issue_number)
1819 {
1820 return Ok(());
1821 }
1822 match result {
1823 Ok(body) => {
1824 let Some(existing) =
1825 self.cache_comments.iter().find(|c| c.id == comment_id)
1826 else {
1827 return Err(AppError::Other(anyhow!(
1828 "selected comment is no longer available"
1829 )));
1830 };
1831 if body == existing.body.as_ref() {
1832 return Ok(());
1833 }
1834 let trimmed = body.trim();
1835 if trimmed.is_empty() {
1836 return Err(AppError::Other(anyhow!(
1837 "comment cannot be empty after editing"
1838 )));
1839 }
1840 self.patch_comment(issue_number, comment_id, trimmed.to_string())
1841 .await;
1842 if let Some(action_tx) = self.action_tx.as_ref() {
1843 action_tx.send(Action::ForceRender).await?;
1844 }
1845 }
1846 Err(message) => {
1847 return Err(AppError::Other(anyhow!("comment edit failed: {message}")));
1848 }
1849 }
1850 }
1851 Action::IssueCommentPatched {
1852 issue_number,
1853 comment,
1854 } => {
1855 if self
1856 .current
1857 .as_ref()
1858 .is_some_and(|seed| seed.number == issue_number)
1859 && let Some(existing) =
1860 self.cache_comments.iter_mut().find(|c| c.id == comment.id)
1861 {
1862 let reactions = existing.reactions.clone();
1863 let my_reactions = existing.my_reactions.clone();
1864 *existing = comment;
1865 existing.reactions = reactions;
1866 existing.my_reactions = my_reactions;
1867 self.markdown_cache.remove(&existing.id);
1868 }
1869 }
1870 Action::IssueCloseSuccess { issue } => {
1871 let issue = *issue;
1872 let initiated_here = self
1873 .close_popup
1874 .as_ref()
1875 .is_some_and(|popup| popup.issue_number == issue.number);
1876 if initiated_here {
1877 self.close_popup = None;
1878 self.close_error = None;
1879 if let Some(action_tx) = self.action_tx.as_ref() {
1880 let _ = action_tx
1881 .send(Action::SelectedIssuePreview {
1882 seed: crate::ui::components::issue_detail::IssuePreviewSeed::from_issue(
1883 &issue,
1884 ),
1885 })
1886 .await;
1887 let _ = action_tx.send(Action::RefreshIssueList).await;
1888 }
1889 }
1890 }
1891 Action::IssueCloseError { number, message } => {
1892 if let Some(popup) = self.close_popup.as_mut()
1893 && popup.issue_number == number
1894 {
1895 popup.loading = false;
1896 popup.error = Some(message.clone());
1897 self.close_error = Some(message);
1898 }
1899 }
1900 Action::ChangeIssueScreen(screen) => {
1901 self.screen = screen;
1902 match screen {
1903 MainScreen::List => {
1904 self.input_state.focus.set(false);
1905 self.list_state.focus.set(false);
1906 self.reaction_mode = None;
1907 self.close_popup = None;
1908 }
1909 MainScreen::Details => {}
1910 MainScreen::DetailsFullscreen => {
1911 self.list_state.focus.set(false);
1912 self.input_state.focus.set(false);
1913 self.paragraph_state.focus.set(false);
1914 self.body_paragraph_state.focus.set(true);
1915 }
1916 MainScreen::CreateIssue => {
1917 self.input_state.focus.set(false);
1918 self.list_state.focus.set(false);
1919 self.reaction_mode = None;
1920 self.close_popup = None;
1921 }
1922 }
1923 }
1924 Action::Tick => {
1925 if self.is_loading_current() {
1926 self.throbber_state.calc_next();
1927 }
1928 if self.posting {
1929 self.post_throbber_state.calc_next();
1930 }
1931 if let Some(popup) = self.close_popup.as_mut()
1932 && popup.loading
1933 {
1934 popup.throbber_state.calc_next();
1935 }
1936 }
1937 _ => {}
1938 }
1939 Ok(())
1940 }
1941
1942 fn cursor(&self) -> Option<(u16, u16)> {
1943 self.input_state.screen_cursor()
1944 }
1945
1946 fn should_render(&self) -> bool {
1947 self.in_details_mode()
1948 }
1949
1950 fn is_animating(&self) -> bool {
1951 self.in_details_mode()
1952 && (self.is_loading_current()
1953 || self.posting
1954 || self.close_popup.as_ref().is_some_and(|popup| popup.loading))
1955 }
1956
1957 fn capture_focus_event(&self, event: &crossterm::event::Event) -> bool {
1958 if !self.in_details_mode() {
1959 return false;
1960 }
1961 if self.screen == MainScreen::DetailsFullscreen {
1962 return true;
1963 }
1964 if self.close_popup.is_some() {
1965 return true;
1966 }
1967 if self.input_state.is_focused() {
1968 return true;
1969 }
1970 match event {
1971 crossterm::event::Event::Key(key) => matches!(
1972 key.code,
1973 crossterm::event::KeyCode::Tab
1974 | crossterm::event::KeyCode::BackTab
1975 | crossterm::event::KeyCode::Char('q')
1976 ),
1977 _ => false,
1978 }
1979 }
1980 fn set_index(&mut self, index: usize) {
1981 self.index = index;
1982 }
1983
1984 fn set_global_help(&self) {
1985 if let Some(action_tx) = &self.action_tx {
1986 let _ = action_tx.try_send(Action::SetHelp(HELP));
1987 }
1988 }
1989}
1990
1991impl HasFocus for IssueConversation {
1992 fn build(&self, builder: &mut FocusBuilder) {
1993 let tag = builder.start(self);
1994 builder.widget(&self.list_state);
1995 builder.widget(&self.body_paragraph_state);
1996 match self.textbox_state {
1997 InputState::Input => builder.widget(&self.input_state),
1998 InputState::Preview => builder.widget(&self.paragraph_state),
1999 };
2000 builder.end(tag);
2001 }
2002
2003 fn focus(&self) -> FocusFlag {
2004 self.focus.clone()
2005 }
2006
2007 fn area(&self) -> Rect {
2008 self.area
2009 }
2010
2011 fn navigable(&self) -> Navigation {
2012 if self.in_details_mode() {
2013 Navigation::Regular
2014 } else {
2015 Navigation::None
2016 }
2017 }
2018}
2019
2020fn build_comment_item(
2021 author: &str,
2022 created_at: &str,
2023 preview: &str,
2024 is_self: bool,
2025 reactions: Option<&[(ReactionContent, u64)]>,
2026) -> ListItem<'static> {
2027 let author_style = if is_self {
2028 Style::new().fg(Color::Green).add_modifier(Modifier::BOLD)
2029 } else {
2030 Style::new().fg(Color::Cyan)
2031 };
2032 let header = Line::from(vec![
2033 Span::styled(author.to_string(), author_style),
2034 Span::raw(" "),
2035 Span::styled(created_at.to_string(), Style::new()),
2036 ]);
2037 let preview_line = Line::from(vec![
2038 Span::raw(" "),
2039 Span::styled(preview.to_string(), Style::new()),
2040 ]);
2041 let mut lines = vec![header, preview_line];
2042 if let Some(reactions) = reactions
2043 && !reactions.is_empty()
2044 {
2045 lines.push(build_reactions_line(reactions));
2046 }
2047 ListItem::new(lines)
2048}
2049
2050fn build_comment_preview_item(
2051 author: &str,
2052 created_at: &str,
2053 body_lines: &[Line<'static>],
2054 preview_width: usize,
2055 is_self: bool,
2056 reactions: Option<&[(ReactionContent, u64)]>,
2057) -> ListItem<'static> {
2058 let preview = extract_preview(body_lines, preview_width);
2059 build_comment_item(author, created_at, &preview, is_self, reactions)
2060}
2061
2062fn build_timeline_item(entry: &TimelineEventView, preview_width: usize) -> ListItem<'static> {
2063 let icon_style = timeline_event_style(&entry.event).add_modifier(Modifier::DIM);
2064 let dim_style = Style::new().dim();
2065 let header = Line::from(vec![
2066 Span::raw(" "),
2067 Span::styled("|", dim_style),
2068 Span::raw(" "),
2069 Span::styled(
2070 entry.icon.to_string(),
2071 icon_style.add_modifier(Modifier::BOLD),
2072 ),
2073 Span::styled(" ", dim_style),
2074 Span::styled(entry.summary.to_string(), icon_style),
2075 Span::styled(" ", dim_style),
2076 Span::styled(entry.created_at.to_string(), dim_style),
2077 ]);
2078 let details = Line::from(vec![
2079 Span::raw(" "),
2080 Span::styled("|", dim_style),
2081 Span::raw(" "),
2082 Span::styled(
2083 truncate_preview(entry.details.as_ref(), preview_width.max(12)),
2084 dim_style,
2085 ),
2086 ]);
2087 ListItem::new(vec![header, details])
2088}
2089
2090fn build_timeline_body_lines(entry: &TimelineEventView) -> Vec<Line<'static>> {
2091 vec![
2092 Line::from(vec![
2093 Span::styled("Event: ", Style::new().dim()),
2094 Span::styled(
2095 format!("{} {}", entry.icon, entry.summary),
2096 timeline_event_style(&entry.event),
2097 ),
2098 ]),
2099 Line::from(vec![
2100 Span::styled("When: ", Style::new().dim()),
2101 Span::raw(entry.created_at.to_string()),
2102 ]),
2103 Line::from(vec![
2104 Span::styled("Details: ", Style::new().dim()),
2105 Span::styled(entry.details.to_string(), Style::new().fg(Color::Gray)),
2106 ]),
2107 ]
2108}
2109
2110fn build_reactions_line(reactions: &[(ReactionContent, u64)]) -> Line<'static> {
2111 let mut ordered = reactions.iter().collect::<Vec<_>>();
2112 ordered.sort_by_key(|(content, _)| reaction_order(content));
2113
2114 let mut spans = vec![Span::raw(" ")];
2115 for (idx, (content, count)) in ordered.into_iter().enumerate() {
2116 if idx != 0 {
2117 spans.push(Span::raw(" "));
2118 }
2119 spans.push(Span::styled(
2120 reaction_label(content).to_string(),
2121 Style::new().fg(Color::Yellow),
2122 ));
2123 spans.push(Span::raw(" "));
2124 spans.push(Span::styled(count.to_string(), Style::new().dim()));
2125 }
2126 Line::from(spans)
2127}
2128
2129fn timeline_event_meta(event: &IssueEvent) -> (&'static str, &'static str) {
2130 match event {
2131 IssueEvent::Closed => ("x", "closed the issue"),
2132 IssueEvent::Reopened => ("o", "reopened the issue"),
2133 IssueEvent::Assigned => ("@", "assigned someone"),
2134 IssueEvent::Unassigned => ("@", "unassigned someone"),
2135 IssueEvent::Labeled => ("#", "added a label"),
2136 IssueEvent::Unlabeled => ("#", "removed a label"),
2137 IssueEvent::Milestoned => ("M", "set a milestone"),
2138 IssueEvent::Demilestoned => ("M", "removed the milestone"),
2139 IssueEvent::Locked => ("!", "locked the conversation"),
2140 IssueEvent::Unlocked => ("!", "unlocked the conversation"),
2141 IssueEvent::Referenced | IssueEvent::CrossReferenced => ("=>", "referenced this issue"),
2142 IssueEvent::Renamed => ("~", "renamed the title"),
2143 IssueEvent::ReviewRequested => ("R", "requested review"),
2144 IssueEvent::ReviewRequestRemoved => ("R", "removed review request"),
2145 IssueEvent::Merged => ("+", "merged"),
2146 IssueEvent::Committed => ("*", "pushed a commit"),
2147 _ => ("*", "updated the timeline"),
2148 }
2149}
2150
2151fn timeline_event_style(event: &IssueEvent) -> Style {
2152 match event {
2153 IssueEvent::Closed | IssueEvent::Locked => Style::new().fg(Color::Red),
2154 IssueEvent::Reopened | IssueEvent::Unlocked => Style::new().fg(Color::Green),
2155 IssueEvent::Labeled | IssueEvent::Unlabeled => Style::new().fg(Color::Yellow),
2156 IssueEvent::Assigned | IssueEvent::Unassigned => Style::new().fg(Color::Cyan),
2157 IssueEvent::Merged => Style::new().fg(Color::Magenta),
2158 _ => Style::new().fg(Color::Blue),
2159 }
2160}
2161
2162fn timeline_event_details(event: &TimelineEvent) -> String {
2163 match event.event {
2164 IssueEvent::Labeled | IssueEvent::Unlabeled => {
2165 if let Some(label) = event.label.as_ref() {
2166 return format!("label: {}", label.name);
2167 }
2168 }
2169 IssueEvent::Milestoned | IssueEvent::Demilestoned => {
2170 if let Some(milestone) = event.milestone.as_ref() {
2171 return format!("milestone: {}", milestone.title);
2172 }
2173 }
2174 IssueEvent::Renamed => {
2175 if let Some(rename) = event.rename.as_ref() {
2176 return format!("title: '{}' -> '{}'", rename.from, rename.to);
2177 }
2178 }
2179 IssueEvent::Assigned | IssueEvent::Unassigned => {
2180 if let Some(assignee) = event.assignee.as_ref() {
2181 return format!("assignee: @{}", assignee.login);
2182 }
2183 if let Some(assignees) = event.assignees.as_ref()
2184 && !assignees.is_empty()
2185 {
2186 let names = assignees
2187 .iter()
2188 .map(|a| format!("@{}", a.login))
2189 .collect::<Vec<_>>()
2190 .join(", ");
2191 return format!("assignees: {}", names);
2192 }
2193 }
2194 IssueEvent::ReviewRequested | IssueEvent::ReviewRequestRemoved => {
2195 if let Some(reviewer) = event.requested_reviewer.as_ref() {
2196 return format!("reviewer: @{}", reviewer.login);
2197 }
2198 }
2199 IssueEvent::Closed
2200 | IssueEvent::Merged
2201 | IssueEvent::Referenced
2202 | IssueEvent::Committed => {
2203 if let Some(reference) = format_reference_target(event) {
2204 return reference;
2205 }
2206 if let Some(commit_id) = event.commit_id.as_ref() {
2207 let short = commit_id.chars().take(8).collect::<String>();
2208 return format!("commit {}", short);
2209 }
2210 if let Some(sha) = event.sha.as_ref() {
2211 let short = sha.chars().take(8).collect::<String>();
2212 return format!("sha {}", short);
2213 }
2214 }
2215 IssueEvent::CrossReferenced | IssueEvent::Connected | IssueEvent::Disconnected => {
2216 if let Some(reference) = format_reference_target(event) {
2217 return reference;
2218 }
2219 }
2220 _ => {}
2221 }
2222
2223 if let Some(assignee) = event.assignee.as_ref() {
2224 return format!("assignee: @{}", assignee.login);
2225 }
2226 if let Some(assignees) = event.assignees.as_ref()
2227 && !assignees.is_empty()
2228 {
2229 let names = assignees
2230 .iter()
2231 .map(|a| format!("@{}", a.login))
2232 .collect::<Vec<_>>()
2233 .join(", ");
2234 return format!("assignees: {}", names);
2235 }
2236 if let Some(commit_id) = event.commit_id.as_ref() {
2237 let short = commit_id.chars().take(8).collect::<String>();
2238 return format!("commit {}", short);
2239 }
2240 if let Some(reference) = format_reference_target(event) {
2241 return reference;
2242 }
2243 if let Some(column) = event.column_name.as_ref() {
2244 if let Some(prev) = event.previous_column_name.as_ref() {
2245 return format!("moved from '{}' to '{}'", prev, column);
2246 }
2247 return format!("project column: {}", column);
2248 }
2249 if let Some(reason) = event.lock_reason.as_ref() {
2250 return format!("lock reason: {}", reason);
2251 }
2252 if let Some(message) = event.message.as_ref()
2253 && !message.trim().is_empty()
2254 {
2255 return truncate_preview(message.trim(), 96);
2256 }
2257 if let Some(body) = event.body.as_ref()
2258 && !body.trim().is_empty()
2259 {
2260 return truncate_preview(body.trim(), 96);
2261 }
2262 format!("{:?}", event.event)
2263}
2264
2265fn format_reference_target(event: &TimelineEvent) -> Option<String> {
2266 if let Some(url) = event.pull_request_url.as_ref() {
2267 if let Some(number) = extract_trailing_number(url.as_str()) {
2268 return Some(format!("pull request #{}", number));
2269 }
2270 return Some(format!("pull request {}", url));
2271 }
2272
2273 if let Some(url) = event.issue_url.as_deref() {
2274 if let Some(number) = extract_trailing_number(url) {
2275 return Some(format!("issue #{}", number));
2276 }
2277 return Some(format!("issue {}", url));
2278 }
2279
2280 None
2281}
2282
2283fn extract_trailing_number(url: &str) -> Option<u64> {
2284 let tail = url.trim_end_matches('/').rsplit('/').next()?;
2285 tail.parse::<u64>().ok()
2286}
2287
2288fn reaction_order(content: &ReactionContent) -> usize {
2289 match content {
2290 ReactionContent::PlusOne => 0,
2291 ReactionContent::Heart => 1,
2292 ReactionContent::Hooray => 2,
2293 ReactionContent::Laugh => 3,
2294 ReactionContent::Rocket => 4,
2295 ReactionContent::Eyes => 5,
2296 ReactionContent::Confused => 6,
2297 ReactionContent::MinusOne => 7,
2298 _ => usize::MAX,
2299 }
2300}
2301
2302fn reaction_label(content: &ReactionContent) -> &'static str {
2303 match content {
2304 ReactionContent::PlusOne => "+1",
2305 ReactionContent::MinusOne => "-1",
2306 ReactionContent::Laugh => "laugh",
2307 ReactionContent::Confused => "confused",
2308 ReactionContent::Heart => "heart",
2309 ReactionContent::Hooray => "hooray",
2310 ReactionContent::Rocket => "rocket",
2311 ReactionContent::Eyes => "eyes",
2312 _ => "other",
2313 }
2314}
2315
2316fn reaction_add_options() -> [ReactionContent; 8] {
2317 [
2318 ReactionContent::PlusOne,
2319 ReactionContent::Heart,
2320 ReactionContent::Hooray,
2321 ReactionContent::Laugh,
2322 ReactionContent::Rocket,
2323 ReactionContent::Eyes,
2324 ReactionContent::Confused,
2325 ReactionContent::MinusOne,
2326 ]
2327}
2328
2329fn format_reaction_picker(selected: usize, options: &[ReactionContent]) -> String {
2330 let mut out = String::new();
2331 let mut bracket_start = None;
2332 let mut bracket_end = None;
2333 const TOTAL_WIDTH: usize = 20;
2334 for (idx, content) in options.iter().enumerate() {
2335 if idx > 0 {
2336 out.push(' ');
2337 }
2338 let label = reaction_label(content);
2339 if idx == selected {
2340 bracket_start = Some(out.len());
2341 out.push('[');
2342 out.push_str(label);
2343 bracket_end = Some(out.len());
2344 out.push(']');
2345 } else {
2346 out.push_str(label);
2347 }
2348 }
2349 if let (Some(start), Some(end)) = (bracket_start, bracket_end) {
2350 let padding = TOTAL_WIDTH.saturating_sub(end - start + 1);
2351 let left_padding = padding / 2;
2352 let left_start = start.saturating_sub(left_padding);
2353 let right_padding = padding - left_padding;
2354 let right_end = (end + right_padding).min(out.len());
2355 return out[left_start..right_end].to_string();
2356 }
2357 out
2358}
2359
2360fn to_reaction_snapshot<I>(
2361 reactions: I,
2362 current_user: &str,
2363) -> (Vec<(ReactionContent, u64)>, Vec<ReactionContent>)
2364where
2365 I: IntoIterator<Item = octocrab::models::reactions::Reaction>,
2366{
2367 let mut mine = Vec::new();
2368 let counts = reactions
2369 .into_iter()
2370 .fold(HashMap::new(), |mut acc, reaction| {
2371 if reaction.user.login.eq_ignore_ascii_case(current_user) {
2372 mine.push(reaction.content.clone());
2373 }
2374 *acc.entry(reaction.content).or_insert(0) += 1_u64;
2375 acc
2376 });
2377 mine.sort_by_key(reaction_order);
2378 mine.dedup();
2379 (counts.into_iter().collect::<Vec<_>>(), mine)
2380}
2381
2382fn extract_preview(lines: &[Line<'static>], preview_width: usize) -> String {
2383 for line in lines {
2384 let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
2385 let trimmed = text.trim();
2386 if !trimmed.is_empty() {
2387 return truncate_preview(trimmed, preview_width.max(8));
2388 }
2389 }
2390 "(empty)".to_string()
2391}
2392
2393fn truncate_preview(input: &str, max_width: usize) -> String {
2394 if display_width(input) <= max_width {
2395 return input.to_string();
2396 }
2397 let mut out = String::new();
2398 for ch in input.chars() {
2399 let mut candidate = out.clone();
2400 candidate.push(ch);
2401 if display_width(&candidate) + 3 > max_width {
2402 break;
2403 }
2404 out.push(ch);
2405 }
2406 out.push_str("...");
2407 out
2408}
2409
2410pub(crate) fn render_markdown_lines(text: &str, width: usize, indent: usize) -> Vec<Line<'static>> {
2411 render_markdown(text, width, indent).lines
2412}
2413
2414fn render_markdown(text: &str, width: usize, indent: usize) -> MarkdownRender {
2415 let mut renderer = MarkdownRenderer::new(width, indent);
2416 let options = Options::ENABLE_GFM
2417 | Options::ENABLE_STRIKETHROUGH
2418 | Options::ENABLE_TASKLISTS
2419 | Options::ENABLE_TABLES
2420 | Options::ENABLE_FOOTNOTES
2421 | Options::ENABLE_SUPERSCRIPT
2422 | Options::ENABLE_SUBSCRIPT
2423 | Options::ENABLE_MATH;
2424 let parser = Parser::new_ext(text, options);
2425 let parser = TextMergeStream::new(parser);
2426 for event in parser {
2427 match event {
2428 MdEvent::Start(tag) => renderer.start_tag(tag),
2429 MdEvent::End(tag) => renderer.end_tag(tag),
2430 MdEvent::Text(text) => renderer.text(&text),
2431 MdEvent::Code(text) => renderer.inline_code(&text),
2432 MdEvent::InlineMath(text) | MdEvent::DisplayMath(text) => renderer.inline_math(&text),
2433 MdEvent::SoftBreak => renderer.soft_break(),
2434 MdEvent::HardBreak => renderer.hard_break(),
2435 MdEvent::Html(text) | MdEvent::InlineHtml(text) => renderer.text(&text),
2436 MdEvent::Rule => renderer.rule(),
2437 MdEvent::TaskListMarker(checked) => renderer.task_list_marker(checked),
2438 _ => {}
2439 }
2440 }
2441 renderer.finish()
2442}
2443
2444struct MarkdownRenderer {
2445 lines: Vec<Line<'static>>,
2446 links: Vec<RenderedLink>,
2447 current_line: Vec<Span<'static>>,
2448 current_width: usize,
2449 max_width: usize,
2450 indent: usize,
2451 style_stack: Vec<Style>,
2452 current_style: Style,
2453 in_block_quote: bool,
2454 block_quote_style: Option<AdmonitionStyle>,
2455 block_quote_title_pending: bool,
2456 in_code_block: bool,
2457 code_block_lang: Option<String>,
2458 code_block_buf: String,
2459 list_prefix: Option<String>,
2460 pending_space: bool,
2461 active_link_url: Option<String>,
2462}
2463
2464#[derive(Clone, Copy)]
2465struct AdmonitionStyle {
2466 marker: &'static str,
2467 default_title: &'static str,
2468 border_color: Color,
2469 title_style: Style,
2470}
2471
2472impl AdmonitionStyle {
2473 fn from_block_quote_kind(kind: BlockQuoteKind) -> Option<Self> {
2474 match kind {
2475 BlockQuoteKind::Note => Some(Self {
2476 marker: "NOTE",
2477 default_title: "Note",
2478 border_color: Color::Blue,
2479 title_style: Style::new().fg(Color::Blue).add_modifier(Modifier::BOLD),
2480 }),
2481 BlockQuoteKind::Tip => Some(Self {
2482 marker: "TIP",
2483 default_title: "Tip",
2484 border_color: Color::Green,
2485 title_style: Style::new().fg(Color::Green).add_modifier(Modifier::BOLD),
2486 }),
2487 BlockQuoteKind::Important => Some(Self {
2488 marker: "IMPORTANT",
2489 default_title: "Important",
2490 border_color: Color::Cyan,
2491 title_style: Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD),
2492 }),
2493 BlockQuoteKind::Warning => Some(Self {
2494 marker: "WARNING",
2495 default_title: "Warning",
2496 border_color: Color::Yellow,
2497 title_style: Style::new().fg(Color::Yellow).add_modifier(Modifier::BOLD),
2498 }),
2499 BlockQuoteKind::Caution => Some(Self {
2500 marker: "CAUTION",
2501 default_title: "Caution",
2502 border_color: Color::Red,
2503 title_style: Style::new().fg(Color::Red).add_modifier(Modifier::BOLD),
2504 }),
2505 }
2506 }
2507}
2508
2509impl MarkdownRenderer {
2510 fn new(max_width: usize, indent: usize) -> Self {
2511 Self {
2512 lines: Vec::new(),
2513 links: Vec::new(),
2514 current_line: Vec::new(),
2515 current_width: 0,
2516 max_width: max_width.max(10),
2517 indent,
2518 style_stack: Vec::new(),
2519 current_style: Style::new(),
2520 in_block_quote: false,
2521 block_quote_style: None,
2522 block_quote_title_pending: false,
2523 in_code_block: false,
2524 code_block_lang: None,
2525 code_block_buf: String::new(),
2526 list_prefix: None,
2527 pending_space: false,
2528 active_link_url: None,
2529 }
2530 }
2531
2532 fn start_tag(&mut self, tag: Tag) {
2533 match tag {
2534 Tag::Emphasis => self.push_style(Style::new().add_modifier(Modifier::ITALIC)),
2535 Tag::Strong => self.push_style(Style::new().add_modifier(Modifier::BOLD)),
2536 Tag::Strikethrough => self.push_style(Style::new().add_modifier(Modifier::CROSSED_OUT)),
2537 Tag::Superscript | Tag::Subscript => {
2538 self.push_style(Style::new().add_modifier(Modifier::ITALIC))
2539 }
2540 Tag::Link { dest_url, .. } => {
2541 self.active_link_url = Some(dest_url.to_string());
2542 self.push_style(
2543 Style::new()
2544 .fg(Color::Blue)
2545 .add_modifier(Modifier::UNDERLINED),
2546 );
2547 }
2548 Tag::Heading { .. } => {
2549 self.push_style(Style::new().add_modifier(Modifier::BOLD));
2550 }
2551 Tag::BlockQuote(kind) => {
2552 self.flush_line();
2553 self.in_block_quote = true;
2554 self.block_quote_style = kind.and_then(AdmonitionStyle::from_block_quote_kind);
2555 self.block_quote_title_pending = self.block_quote_style.is_some();
2556 }
2557 Tag::CodeBlock(kind) => {
2558 self.ensure_admonition_header();
2559 self.flush_line();
2560 self.in_code_block = true;
2561 self.code_block_lang = code_block_kind_lang(kind);
2562 self.code_block_buf.clear();
2563 }
2564 Tag::Item => {
2565 self.flush_line();
2566 self.list_prefix = Some("• ".to_string());
2567 }
2568 _ => {}
2569 }
2570 }
2571
2572 fn end_tag(&mut self, tag: TagEnd) {
2573 match tag {
2574 TagEnd::Emphasis
2575 | TagEnd::Strong
2576 | TagEnd::Strikethrough
2577 | TagEnd::Superscript
2578 | TagEnd::Subscript
2579 | TagEnd::Link => {
2580 if matches!(tag, TagEnd::Link) {
2581 self.active_link_url = None;
2582 }
2583 self.pop_style();
2584 }
2585 TagEnd::Heading(_) => {
2586 self.pop_style();
2587 self.flush_line();
2588 }
2589 TagEnd::BlockQuote(_) => {
2590 self.flush_line();
2591 self.in_block_quote = false;
2592 self.block_quote_style = None;
2593 self.block_quote_title_pending = false;
2594 self.push_blank_line();
2595 }
2596 TagEnd::CodeBlock => {
2597 self.render_code_block();
2598 self.flush_line();
2599 self.in_code_block = false;
2600 self.code_block_lang = None;
2601 self.code_block_buf.clear();
2602 self.push_blank_line();
2603 }
2604 TagEnd::Item => {
2605 self.flush_line();
2606 self.list_prefix = None;
2607 }
2608 TagEnd::Paragraph => {
2609 self.flush_line();
2610 self.push_blank_line();
2611 }
2612 _ => {}
2613 }
2614 }
2615
2616 fn text(&mut self, text: &str) {
2617 if self.in_block_quote && self.block_quote_title_pending {
2618 if let Some(style) = self.block_quote_style
2619 && let Some(title) = extract_admonition_title(text, style.marker)
2620 {
2621 let title = if title.is_empty() {
2622 style.default_title
2623 } else {
2624 title
2625 };
2626 self.push_admonition_header(title, style);
2627 self.block_quote_title_pending = false;
2628 return;
2629 }
2630 self.ensure_admonition_header();
2631 }
2632 if self.in_code_block {
2633 self.code_block_text(text);
2634 } else {
2635 let style = self.current_style;
2636 self.push_text(text, style);
2637 }
2638 }
2639
2640 fn inline_code(&mut self, text: &str) {
2641 self.ensure_admonition_header();
2642 let style = self
2643 .current_style
2644 .patch(Style::new().fg(Color::Yellow).add_modifier(Modifier::BOLD));
2645 self.push_text(text, style);
2646 }
2647
2648 fn inline_math(&mut self, text: &str) {
2649 self.ensure_admonition_header();
2650 let style = self.current_style.patch(
2651 Style::new()
2652 .fg(Color::LightMagenta)
2653 .add_modifier(Modifier::ITALIC),
2654 );
2655 self.push_text(text, style);
2656 }
2657
2658 fn soft_break(&mut self) {
2659 self.ensure_admonition_header();
2660 if self.in_code_block {
2661 self.code_block_buf.push('\n');
2662 } else {
2663 self.pending_space = true;
2664 }
2665 }
2666
2667 fn hard_break(&mut self) {
2668 self.ensure_admonition_header();
2669 if self.in_code_block {
2670 self.code_block_buf.push('\n');
2671 return;
2672 }
2673 self.flush_line();
2674 }
2675
2676 fn task_list_marker(&mut self, checked: bool) {
2677 self.ensure_admonition_header();
2678 let marker = if checked { "[x] " } else { "[ ] " };
2679 self.push_text(marker, self.current_style);
2680 }
2681
2682 fn rule(&mut self) {
2683 self.flush_line();
2684 self.start_line();
2685 let width = self.max_width.saturating_sub(self.prefix_width()).max(8);
2686 let bar = "─".repeat(width);
2687 self.current_line
2688 .push(Span::styled(bar.clone(), Style::new().fg(Color::DarkGray)));
2689 self.current_width += display_width(&bar);
2690 self.flush_line();
2691 self.push_blank_line();
2692 }
2693
2694 fn push_text(&mut self, text: &str, style: Style) {
2695 let mut buffer = String::new();
2696 for ch in text.chars() {
2697 if ch == '\n' {
2698 if !buffer.is_empty() {
2699 self.push_word(&buffer, style);
2700 buffer.clear();
2701 }
2702 self.flush_line();
2703 continue;
2704 }
2705 if ch.is_whitespace() {
2706 if !buffer.is_empty() {
2707 self.push_word(&buffer, style);
2708 buffer.clear();
2709 }
2710 self.pending_space = true;
2711 } else {
2712 buffer.push(ch);
2713 }
2714 }
2715 if !buffer.is_empty() {
2716 self.push_word(&buffer, style);
2717 }
2718 }
2719
2720 fn push_word(&mut self, word: &str, style: Style) {
2721 let prefix_width = self.prefix_width();
2722 let max_width = self.max_width;
2723 let word_width = display_width(word);
2724 let space_width = if self.pending_space && self.current_width > prefix_width {
2725 1
2726 } else {
2727 0
2728 };
2729
2730 if word_width > max_width.saturating_sub(prefix_width) {
2731 self.push_long_word(word, style);
2732 self.pending_space = false;
2733 return;
2734 }
2735
2736 if self.current_line.is_empty() {
2737 self.start_line();
2738 }
2739
2740 if self.current_width + space_width + word_width > max_width
2741 && self.current_width > prefix_width
2742 {
2743 self.flush_line();
2744 self.start_line();
2745 }
2746
2747 if self.pending_space && self.current_width > prefix_width {
2748 let space_col = self.current_width;
2749 self.current_line.push(Span::raw(" "));
2750 self.current_width += 1;
2751 if self.should_attach_space_to_active_link(space_col) {
2752 self.push_link_segment(" ", space_col, 1);
2753 }
2754 }
2755 self.pending_space = false;
2756
2757 let link_start_col = self.current_width;
2758 self.current_line
2759 .push(Span::styled(word.to_string(), style));
2760 self.current_width += word_width;
2761 self.push_link_segment(word, link_start_col, word_width);
2762 }
2763
2764 fn push_long_word(&mut self, word: &str, style: Style) {
2765 let available = self.max_width.saturating_sub(self.prefix_width()).max(1);
2766 let wrapped = textwrap::wrap(word, textwrap::Options::new(available).break_words(true));
2767 for (idx, part) in wrapped.iter().enumerate() {
2768 if idx > 0 {
2769 self.flush_line();
2770 }
2771 if self.current_line.is_empty() {
2772 self.start_line();
2773 }
2774 let link_start_col = self.current_width;
2775 let part_width = display_width(part);
2776 self.current_line
2777 .push(Span::styled(part.to_string(), style));
2778 self.current_width += part_width;
2779 self.push_link_segment(part, link_start_col, part_width);
2780 }
2781 }
2782
2783 fn push_link_segment(&mut self, label: &str, col: usize, width: usize) {
2784 let Some(url) = self.active_link_url.as_ref() else {
2785 return;
2786 };
2787 if label.is_empty() || width == 0 {
2788 return;
2789 }
2790
2791 let line = self.current_line_index();
2792 if let Some(last) = self.links.last_mut()
2793 && last.url == *url
2794 && last.line == line
2795 && last.col + last.width == col
2796 {
2797 last.label.push_str(label);
2798 last.width += width;
2799 return;
2800 }
2801
2802 self.links.push(RenderedLink {
2803 line,
2804 col,
2805 label: label.to_string(),
2806 url: url.clone(),
2807 width,
2808 });
2809 }
2810
2811 fn should_attach_space_to_active_link(&self, space_col: usize) -> bool {
2812 let Some(url) = self.active_link_url.as_ref() else {
2813 return false;
2814 };
2815 let line = self.current_line_index();
2816 self.links.last().is_some_and(|last| {
2817 last.url == *url && last.line == line && last.col + last.width == space_col
2818 })
2819 }
2820
2821 fn current_line_index(&self) -> usize {
2822 self.lines.len()
2823 }
2824
2825 fn code_block_text(&mut self, text: &str) {
2826 self.code_block_buf.push_str(text);
2827 }
2828
2829 fn render_code_block(&mut self) {
2830 if self.code_block_buf.is_empty() {
2831 return;
2832 }
2833
2834 let code = std::mem::take(&mut self.code_block_buf);
2835 let assets = syntect_assets();
2836 let syntax = resolve_syntax(&assets.syntaxes, self.code_block_lang.as_deref());
2837 let mut highlighter = HighlightLines::new(syntax, &assets.theme);
2838 let fallback_style = Style::new().light_yellow();
2839
2840 for raw_line in code.split('\n') {
2841 self.flush_line();
2842 self.start_line();
2843 match highlighter.highlight_line(raw_line, &assets.syntaxes) {
2844 Ok(regions) => {
2845 for (syn_style, fragment) in regions {
2846 if fragment.is_empty() {
2847 continue;
2848 }
2849 self.current_line.push(Span::styled(
2850 fragment.to_string(),
2851 syntect_style_to_ratatui(syn_style),
2852 ));
2853 self.current_width += display_width(fragment);
2854 }
2855 }
2856 Err(_) => {
2857 if !raw_line.is_empty() {
2858 self.current_line
2859 .push(Span::styled(raw_line.to_string(), fallback_style));
2860 self.current_width += display_width(raw_line);
2861 }
2862 }
2863 }
2864 self.flush_line();
2865 }
2866 }
2867
2868 fn start_line(&mut self) {
2869 if !self.current_line.is_empty() {
2870 return;
2871 }
2872 if self.indent > 0 {
2873 let indent = " ".repeat(self.indent);
2874 self.current_width += self.indent;
2875 self.current_line.push(Span::raw(indent));
2876 }
2877 if self.in_block_quote {
2878 self.current_width += 2;
2879 let border_style = self
2880 .block_quote_style
2881 .map(|s| Style::new().fg(s.border_color))
2882 .unwrap_or_else(|| Style::new().fg(Color::DarkGray));
2883 self.current_line.push(Span::styled("│ ", border_style));
2884 }
2885 if let Some(prefix) = &self.list_prefix {
2886 self.current_width += display_width(prefix);
2887 self.current_line.push(Span::raw(prefix.clone()));
2888 }
2889 }
2890
2891 fn prefix_width(&self) -> usize {
2892 let mut width = self.indent;
2893 if self.in_block_quote {
2894 width += 2;
2895 }
2896 if let Some(prefix) = &self.list_prefix {
2897 width += display_width(prefix);
2898 }
2899 width
2900 }
2901
2902 fn flush_line(&mut self) {
2903 if self.current_line.is_empty() {
2904 self.pending_space = false;
2905 return;
2906 }
2907 let line = Line::from(std::mem::take(&mut self.current_line));
2908 self.lines.push(line);
2909 self.current_width = 0;
2910 self.pending_space = false;
2911 }
2912
2913 fn push_blank_line(&mut self) {
2914 if self.lines.last().is_some_and(|line| line.spans.is_empty()) {
2915 return;
2916 }
2917 self.lines.push(Line::from(Vec::<Span<'static>>::new()));
2918 }
2919
2920 fn push_style(&mut self, style: Style) {
2921 self.style_stack.push(self.current_style);
2922 self.current_style = self.current_style.patch(style);
2923 }
2924
2925 fn pop_style(&mut self) {
2926 if let Some(prev) = self.style_stack.pop() {
2927 self.current_style = prev;
2928 }
2929 }
2930
2931 fn finish(mut self) -> MarkdownRender {
2932 self.flush_line();
2933 while self.lines.last().is_some_and(|line| line.spans.is_empty()) {
2934 self.lines.pop();
2935 }
2936 if self.lines.is_empty() {
2937 self.lines.push(Line::from(vec![Span::raw("")]));
2938 }
2939 MarkdownRender {
2940 lines: self.lines,
2941 links: self.links,
2942 }
2943 }
2944
2945 fn ensure_admonition_header(&mut self) {
2946 if !self.block_quote_title_pending {
2947 return;
2948 }
2949 if let Some(style) = self.block_quote_style {
2950 self.push_admonition_header(style.default_title, style);
2951 }
2952 self.block_quote_title_pending = false;
2953 }
2954
2955 fn push_admonition_header(&mut self, title: &str, style: AdmonitionStyle) {
2956 self.flush_line();
2957 self.start_line();
2958 self.current_line
2959 .push(Span::styled(title.to_string(), style.title_style));
2960 self.current_width += display_width(title);
2961 self.flush_line();
2962 }
2963}
2964
2965fn extract_admonition_title<'a>(text: &'a str, marker: &str) -> Option<&'a str> {
2966 let trimmed = text.trim_start();
2967 let min_len = marker.len() + 3;
2968 if trimmed.len() < min_len {
2969 return None;
2970 }
2971 let bytes = trimmed.as_bytes();
2972 if bytes[0] != b'[' || bytes[1] != b'!' {
2973 return None;
2974 }
2975 let marker_end = 2 + marker.len();
2976 if bytes.get(marker_end) != Some(&b']') {
2977 return None;
2978 }
2979 if !trimmed[2..marker_end].eq_ignore_ascii_case(marker) {
2980 return None;
2981 }
2982 Some(trimmed[marker_end + 1..].trim())
2983}
2984
2985fn code_block_kind_lang(kind: CodeBlockKind<'_>) -> Option<String> {
2986 match kind {
2987 CodeBlockKind::Indented => None,
2988 CodeBlockKind::Fenced(info) => parse_fenced_language(&info).map(|lang| lang.to_lowercase()),
2989 }
2990}
2991
2992fn parse_fenced_language(info: &str) -> Option<&str> {
2993 let token = info
2994 .split_ascii_whitespace()
2995 .next()
2996 .unwrap_or_default()
2997 .split(',')
2998 .next()
2999 .unwrap_or_default()
3000 .trim_matches(|c| c == '{' || c == '}');
3001 let token = token.strip_prefix('.').unwrap_or(token);
3002 if token.is_empty() { None } else { Some(token) }
3003}
3004
3005fn resolve_syntax<'a>(syntaxes: &'a SyntaxSet, lang: Option<&str>) -> &'a SyntaxReference {
3006 if let Some(lang) = lang {
3007 if let Some(syntax) = syntaxes.find_syntax_by_token(lang) {
3008 return syntax;
3009 }
3010 if let Some(stripped) = lang.strip_prefix("language-")
3011 && let Some(syntax) = syntaxes.find_syntax_by_token(stripped)
3012 {
3013 return syntax;
3014 }
3015 if let Some(syntax) = syntaxes.find_syntax_by_extension(lang) {
3016 return syntax;
3017 }
3018 }
3019 syntaxes.find_syntax_plain_text()
3020}
3021
3022fn syntect_style_to_ratatui(style: syntect::highlighting::Style) -> Style {
3023 let mut out = Style::new().fg(Color::Rgb(
3024 style.foreground.r,
3025 style.foreground.g,
3026 style.foreground.b,
3027 ));
3028 if style.font_style.contains(FontStyle::BOLD) {
3029 out = out.add_modifier(Modifier::BOLD);
3030 }
3031 if style.font_style.contains(FontStyle::ITALIC) {
3032 out = out.add_modifier(Modifier::ITALIC);
3033 }
3034 if style.font_style.contains(FontStyle::UNDERLINE) {
3035 out = out.add_modifier(Modifier::UNDERLINED);
3036 }
3037 out
3038}
3039
3040#[cfg(test)]
3041mod tests {
3042 use super::render_markdown;
3043
3044 fn line_text(rendered: &super::MarkdownRender, idx: usize) -> String {
3045 rendered.lines[idx]
3046 .spans
3047 .iter()
3048 .map(|s| s.content.as_ref())
3049 .collect()
3050 }
3051
3052 #[test]
3053 fn extracts_link_segments_with_urls() {
3054 let rendered = render_markdown("Go to [ratatui docs](https://github.com/ratatui/).", 80, 0);
3055
3056 assert!(!rendered.links.is_empty());
3057 assert!(
3058 rendered
3059 .links
3060 .iter()
3061 .all(|link| link.url == "https://github.com/ratatui/")
3062 );
3063 }
3064
3065 #[test]
3066 fn wraps_long_links_into_multiple_segments() {
3067 let rendered = render_markdown("[A very long linked label](https://example.com)", 12, 2);
3068
3069 assert!(rendered.links.len() >= 2);
3070 }
3071
3072 #[test]
3073 fn keeps_spaces_around_plain_links() {
3074 let rendered = render_markdown("left https://google.com right", 80, 0);
3075
3076 assert_eq!(line_text(&rendered, 0), "left https://google.com right");
3077 assert!(
3078 rendered
3079 .links
3080 .iter()
3081 .all(|link| !link.label.starts_with(' ') && !link.label.ends_with(' '))
3082 );
3083 }
3084}