tij 0.5.1

Text-mode interface for Jujutsu - a TUI for jj version control
Documentation
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
//! Rendering for DiffView

use ratatui::{
    prelude::*,
    style::Stylize,
    text::{Line, Span},
    widgets::Paragraph,
};

use crate::model::{
    CompareInfo, DiffDisplayFormat, DiffLine, DiffLineKind, DiffMode, Notification,
};
use crate::ui::{components, theme};

use super::DiffView;

impl DiffView {
    /// Render the diff view (without status bar - rendered by App)
    pub fn render(&self, frame: &mut Frame, area: Rect, notification: Option<&Notification>) {
        if self.compare_info.is_some() {
            self.render_compare(frame, area, notification);
        } else {
            self.render_normal(frame, area, notification);
        }
    }

    /// Render normal single-revision diff.
    ///
    /// Layout: header (dynamic via `header_height`) + context bar (1) + diff (rest).
    /// Two modes for the header:
    ///   - collapsed (default): cap so diff keeps at least 8 rows and header
    ///     never exceeds half the viewport. Overflowing description rows are
    ///     shown as a "(+N more)" hint by `render_header`.
    ///   - expanded (toggled with `t`): grow header to fit full description,
    ///     reserving only context bar + a small diff floor so users can read
    ///     the entire message in-place.
    fn render_normal(&self, frame: &mut Frame, area: Rect, notification: Option<&Notification>) {
        let header_height = self.header_height(area.height);
        let chunks = Layout::vertical([
            Constraint::Length(header_height),
            Constraint::Length(1),
            Constraint::Min(1),
        ])
        .split(area);

        self.render_header(frame, chunks[0], notification);
        self.render_context_bar(frame, chunks[1]);
        self.render_diff_content(frame, chunks[2]);
    }

    /// Render compare (two-revision) diff
    fn render_compare(&self, frame: &mut Frame, area: Rect, notification: Option<&Notification>) {
        let compare_info = self.compare_info.as_ref().unwrap();

        let header_height = self.header_height(area.height);
        let chunks = Layout::vertical([
            Constraint::Length(header_height),
            Constraint::Length(1),
            Constraint::Min(1),
        ])
        .split(area);

        self.render_compare_header(frame, chunks[0], compare_info, notification);
        self.render_context_bar(frame, chunks[1]);
        self.render_diff_content(frame, chunks[2]);
    }

    /// Render the header (commit info including description)
    fn render_header(&self, frame: &mut Frame, area: Rect, notification: Option<&Notification>) {
        let format_suffix = match self.display_format {
            DiffDisplayFormat::ColorWords => String::new(),
            fmt => format!(" [{}]", fmt.label()),
        };
        let mut title_spans = vec![
            Span::raw(" Tij - Diff View ").bold(),
            Span::raw("["),
            Span::styled(
                self.revision.chars().take(8).collect::<String>(),
                Style::default().fg(theme::log_view::CHANGE_ID),
            ),
            Span::raw("]"),
            Span::styled(format_suffix, Style::default().fg(Color::Yellow).bold()),
        ];
        if self.description_expanded {
            title_spans.push(Span::styled(
                " [full desc]",
                Style::default().fg(Color::Cyan).bold(),
            ));
        }
        title_spans.push(Span::raw(" "));
        let title = Line::from(title_spans).centered();

        // Build notification line for title bar (right-aligned)
        let title_width = title.width();
        let available_for_notif = area.width.saturating_sub(title_width as u16 + 4) as usize;
        let notif_line = notification
            .filter(|n| !n.is_expired())
            .map(|n| components::build_notification_title(n, Some(available_for_notif)))
            .filter(|line| !line.spans.is_empty());

        let mut header_text = vec![
            Line::from(vec![
                Span::raw("Commit: "),
                Span::styled(
                    self.content.commit_id.chars().take(40).collect::<String>(),
                    Style::default().fg(theme::log_view::CHANGE_ID),
                ),
            ]),
            Line::from(vec![
                Span::raw("Author: "),
                Span::raw(&self.content.author),
                Span::raw("  "),
                Span::styled(
                    &self.content.timestamp,
                    Style::default().fg(Color::DarkGray),
                ),
            ]),
        ];

        // Show description, truncating with a hint if it would not fit in the
        // header height we were given (Paragraph silently clips overflowing
        // lines, which would otherwise hide the fact that the message was cut).
        // area.height = top border (1) + commit (1) + author (1) + description rows.
        let desc_rows_available = (area.height as usize).saturating_sub(3);
        if self.content.description.is_empty() {
            header_text.push(Line::from(vec![Span::styled(
                "(no description)",
                Style::default().fg(Color::DarkGray).italic(),
            )]));
        } else {
            let lines: Vec<&str> = self.content.description.lines().collect();
            let total = lines.len();
            let truncated = total > desc_rows_available;
            let take_n = if truncated {
                desc_rows_available.saturating_sub(1) // leave a row for the hint
            } else {
                total
            };
            for line in lines.iter().take(take_n) {
                header_text.push(Line::from(vec![Span::styled(
                    line.to_string(),
                    Style::default().fg(Color::White).bold(),
                )]));
            }
            if truncated {
                let hidden = total - take_n;
                let suffix = if self.description_expanded {
                    // Already expanded but the terminal is too short to fit
                    // everything; pressing 't' would only collapse, so steer
                    // the user toward a different remedy.
                    " — terminal too small; resize or yank with 'y'"
                } else {
                    " — press 't' to expand"
                };
                header_text.push(Line::from(vec![Span::styled(
                    format!(
                        "(+{} more line{} hidden{})",
                        hidden,
                        if hidden == 1 { "" } else { "s" },
                        suffix,
                    ),
                    Style::default().fg(Color::DarkGray).italic(),
                )]));
            }
        }

        // Use header_block with notification on right
        let block = if let Some(notif) = notif_line {
            components::header_block(title).title(notif.right_aligned())
        } else {
            components::header_block(title)
        };

        let header = Paragraph::new(header_text).block(block);

        frame.render_widget(header, area);
    }

    /// Render compare header with From/To revision info
    fn render_compare_header(
        &self,
        frame: &mut Frame,
        area: Rect,
        compare_info: &CompareInfo,
        notification: Option<&Notification>,
    ) {
        let format_suffix = match self.display_format {
            DiffDisplayFormat::ColorWords => String::new(),
            fmt => format!(" [{}]", fmt.label()),
        };
        let mode_label = match self.mode {
            DiffMode::Interdiff => "Interdiff",
            _ => "Compare Diff",
        };
        let title_text = format!(" Tij - {}{} ", mode_label, format_suffix);
        let title = Line::from(title_text).bold().cyan().centered();

        // Build notification for title bar
        let title_width = title.width();
        let available_for_notif = area.width.saturating_sub(title_width as u16 + 4) as usize;
        let notif_line = notification
            .filter(|n| !n.is_expired())
            .map(|n| components::build_notification_title(n, Some(available_for_notif)))
            .filter(|line| !line.spans.is_empty());

        // Build from/to lines
        let from = &compare_info.from;
        let to = &compare_info.to;

        let from_bookmarks = if from.bookmarks.is_empty() {
            String::new()
        } else {
            format!(" ({})", from.bookmarks.join(", "))
        };
        let to_bookmarks = if to.bookmarks.is_empty() {
            String::new()
        } else {
            format!(" ({})", to.bookmarks.join(", "))
        };

        let from_desc = if from.description.is_empty() {
            "(no description)".to_string()
        } else {
            from.description.clone()
        };
        let to_desc = if to.description.is_empty() {
            "(no description)".to_string()
        } else {
            to.description.clone()
        };

        let (from_label, to_label) = match self.mode {
            DiffMode::Interdiff => ("Interdiff From: ", "Interdiff To:   "),
            _ => ("From: ", "To:   "),
        };

        let header_text = vec![
            Line::from(vec![
                Span::styled(from_label, Style::default().fg(Color::Red).bold()),
                Span::styled(
                    from.change_id.to_string(),
                    Style::default().fg(theme::log_view::CHANGE_ID),
                ),
                Span::styled(from_bookmarks, Style::default().fg(Color::Magenta)),
                Span::raw(format!(" {} ", from.author)),
                Span::raw(from_desc),
            ]),
            Line::from(vec![
                Span::styled(to_label, Style::default().fg(Color::Green).bold()),
                Span::styled(
                    to.change_id.to_string(),
                    Style::default().fg(theme::log_view::CHANGE_ID),
                ),
                Span::styled(to_bookmarks, Style::default().fg(Color::Magenta)),
                Span::raw(format!(" {} ", to.author)),
                Span::raw(to_desc),
            ]),
            Line::from(vec![Span::styled(
                format!("{} file(s) changed", self.file_count()),
                Style::default().fg(Color::DarkGray),
            )]),
        ];

        let block = if let Some(notif) = notif_line {
            components::header_block(title).title(notif.right_aligned())
        } else {
            components::header_block(title)
        };

        let header = Paragraph::new(header_text).block(block);
        frame.render_widget(header, area);
    }

    /// Render the context bar (current file name + progress)
    fn render_context_bar(&self, frame: &mut Frame, area: Rect) {
        let file_info = if self.file_count() > 0 {
            let file_name = self.current_file_name().unwrap_or("(unknown)");
            format!(
                " {} [{}/{}]",
                file_name,
                self.current_file_index + 1,
                self.file_count()
            )
        } else {
            " (no files)".to_string()
        };

        let bar = Paragraph::new(Line::from(vec![Span::styled(
            file_info,
            Style::default().fg(Color::Cyan).bold(),
        )]))
        .block(components::side_borders_block());

        frame.render_widget(bar, area);
    }

    /// Render the diff content (scrollable)
    fn render_diff_content(&self, frame: &mut Frame, area: Rect) {
        // No top/bottom borders, only left/right, so use full height
        let inner_height = area.height as usize;

        if self.content.lines.is_empty() {
            // Empty state
            let empty_msg = components::no_changes_state().block(components::side_borders_block());
            frame.render_widget(empty_msg, area);
            return;
        }

        // Build visible lines
        let lines: Vec<Line> = self
            .content
            .lines
            .iter()
            .skip(self.scroll_offset)
            .take(inner_height)
            .map(|diff_line| self.render_diff_line(diff_line))
            .collect();

        let diff = Paragraph::new(lines).block(components::side_borders_block());

        frame.render_widget(diff, area);
    }

    /// Render a single diff line
    fn render_diff_line(&self, line: &DiffLine) -> Line<'static> {
        let show_line_nums = self.display_format == DiffDisplayFormat::ColorWords;

        match line.kind {
            DiffLineKind::FileHeader => Line::from(Span::styled(
                format!("── {} ──", line.content),
                Style::default().fg(theme::diff_view::FILE_HEADER).bold(),
            )),
            DiffLineKind::Separator => Line::from(""),
            DiffLineKind::Context => {
                if show_line_nums {
                    let line_nums = self.format_line_numbers(line.line_numbers);
                    Line::from(vec![
                        Span::styled(
                            line_nums,
                            Style::default().fg(theme::diff_view::LINE_NUMBER),
                        ),
                        Span::raw("  "),
                        Span::raw(line.content.clone()),
                    ])
                } else {
                    Line::from(Span::raw(format!(" {}", line.content)))
                }
            }
            DiffLineKind::Added => {
                if show_line_nums {
                    let line_nums = self.format_line_numbers(line.line_numbers);
                    Line::from(vec![
                        Span::styled(
                            line_nums,
                            Style::default().fg(theme::diff_view::LINE_NUMBER),
                        ),
                        Span::styled(" +", Style::default().fg(theme::diff_view::ADDED)),
                        Span::styled(
                            line.content.clone(),
                            Style::default().fg(theme::diff_view::ADDED),
                        ),
                    ])
                } else {
                    Line::from(Span::styled(
                        format!(" +{}", line.content),
                        Style::default().fg(theme::diff_view::ADDED),
                    ))
                }
            }
            DiffLineKind::Deleted => {
                if show_line_nums {
                    let line_nums = self.format_line_numbers(line.line_numbers);
                    Line::from(vec![
                        Span::styled(
                            line_nums,
                            Style::default().fg(theme::diff_view::LINE_NUMBER),
                        ),
                        Span::styled(" -", Style::default().fg(theme::diff_view::DELETED)),
                        Span::styled(
                            line.content.clone(),
                            Style::default().fg(theme::diff_view::DELETED),
                        ),
                    ])
                } else {
                    Line::from(Span::styled(
                        format!(" -{}", line.content),
                        Style::default().fg(theme::diff_view::DELETED),
                    ))
                }
            }
        }
    }

    /// Format line numbers for display
    fn format_line_numbers(&self, line_nums: Option<(Option<usize>, Option<usize>)>) -> String {
        match line_nums {
            Some((old, new)) => {
                let old_str = old
                    .map(|n| format!("{:4}", n))
                    .unwrap_or_else(|| "    ".to_string());
                let new_str = new
                    .map(|n| format!("{:4}", n))
                    .unwrap_or_else(|| "    ".to_string());
                format!("{} {}", old_str, new_str)
            }
            None => "         ".to_string(),
        }
    }
}