ratatui_toolkit/widgets/code_diff/widget/methods/helpers/
render_hunk_header.rs

1use ratatui::buffer::Buffer;
2use ratatui::layout::{Position, Rect};
3use ratatui::style::Style;
4
5use crate::widgets::code_diff::diff_config::DiffConfig;
6use crate::widgets::code_diff::diff_hunk::DiffHunk;
7
8/// Renders a hunk header line (e.g., "@@ -1,4 +1,5 @@ fn main()").
9///
10/// # Arguments
11///
12/// * `hunk` - The diff hunk whose header to render
13/// * `config` - Display configuration
14/// * `area` - The area to render in (single line)
15/// * `buf` - The buffer to render to
16#[allow(dead_code)]
17pub fn render_hunk_header(hunk: &DiffHunk, config: &DiffConfig, area: Rect, buf: &mut Buffer) {
18    if area.height == 0 || area.width == 0 {
19        return;
20    }
21
22    let style = Style::default()
23        .bg(config.hunk_header_bg)
24        .fg(config.hunk_header_fg);
25
26    // Clear the entire row with the hunk header style
27    for x in area.x..area.x + area.width {
28        if let Some(cell) = buf.cell_mut(Position::new(x, area.y)) {
29            cell.set_style(style);
30            cell.set_char(' ');
31        }
32    }
33
34    // Render the header text
35    let header_text = hunk.header_text();
36    let mut x = area.x + 1;
37
38    for ch in header_text.chars() {
39        if x >= area.x + area.width {
40            break;
41        }
42        if let Some(cell) = buf.cell_mut(Position::new(x, area.y)) {
43            cell.set_char(ch);
44            cell.set_style(style);
45        }
46        x += 1;
47    }
48}