diff: Remove TUI header

e0f6f1b9a4c92297d5d5a232026294437313fa0a
Alexis Sellier committed ago 1 parent 19f0e9ba
crates/diff/src/bin/rustik-diff.rs +11 -78
1 1
//! Binary entry point for the `rustik-diff` pager.
2 2
3 3
use std::env;
4 4
use std::ffi::OsString;
5 5
use std::io::{self, IsTerminal as _, Read as _, Write as _};
6 -
use std::path::{Path, PathBuf};
6 +
use std::path::Path;
7 7
use std::process::{Command, ExitCode, Output};
8 8
use std::sync::mpsc;
9 9
use std::thread;
10 10
11 11
use rustik_diff::{
24 24
const STATIC_COLS: usize = 100;
25 25
/// Maximum static render rows for non-terminal output.
26 26
const STATIC_MAX_ROWS: usize = 10_000;
27 27
/// Number of body rows moved by line-scroll inputs.
28 28
const LINE_SCROLL_ROWS: isize = 3;
29 -
/// Number of rows reserved for the top header when possible.
30 -
const HEADER_ROWS: usize = 1;
31 29
/// Number of rows reserved for the bottom footer when possible.
32 30
const FOOTER_ROWS: usize = 1;
33 31
/// Foreground color for dim footer text.
34 32
const FOOTER_FG: Color = Color::Fixed(240);
35 33
/// Background color for the footer row.
110 108
    }
111 109
}
112 110
113 111
/// Runs the binary with forwarded Git arguments.
114 112
fn run(args: Vec<OsString>) -> Result<(), Error> {
115 -
    let cwd = env::current_dir()?;
116 113
    if io::stdin().is_terminal() {
114 +
        let cwd = env::current_dir()?;
117 115
        let output = git_diff_in(&args, &cwd)?;
118 116
        if !output.status.success() {
119 117
            io::stderr().write_all(&output.stderr)?;
120 118
            return Err(Error::Git(output.status.code().unwrap_or(1)));
121 119
        }
122 -
        return run_or_forward(&output.stdout, Header::git_diff(cwd, &args));
120 +
        return run_or_forward(&output.stdout);
123 121
    }
124 122
    let mut input = Vec::new();
125 123
    io::stdin().read_to_end(&mut input)?;
126 124
127 -
    run_or_forward(&input, Header::stdin(cwd))
125 +
    run_or_forward(&input)
128 126
}
129 127
130 128
/// Renders diff input or passes non-diff data through unchanged.
131 -
fn run_or_forward(input: &[u8], header: Header) -> Result<(), Error> {
129 +
fn run_or_forward(input: &[u8]) -> Result<(), Error> {
132 130
    let text = match std::str::from_utf8(input) {
133 131
        Ok(text) => text,
134 132
        Err(_) if !contains_diff(input) => {
135 133
            io::stdout().write_all(input)?;
136 134
            return Ok(());
174 172
    let mut viewer = DiffViewer::new(document, options);
175 173
176 174
    terminal.clear()?;
177 175
    terminal.set_cursor_visible(false)?;
178 176
179 -
    let result = terminal_loop(&mut terminal, &mut viewer, header, highlighter);
177 +
    let result = terminal_loop(&mut terminal, &mut viewer, highlighter);
180 178
    let cursor = terminal.set_cursor_visible(true);
181 179
182 180
    result.and(cursor).map_err(Error::Io)
183 181
}
184 182
185 183
/// Runs the terminal event loop.
186 184
fn terminal_loop(
187 185
    terminal: &mut DiffTerminal,
188 186
    viewer: &mut DiffViewer,
189 -
    header: Header,
190 187
    highlighter: BuiltinHighlighter,
191 188
) -> io::Result<()> {
192 189
    let (events_tx, events_rx) = mpsc::channel();
193 190
    TerminalEvent::spawn_input_worker(events_tx.clone());
194 191
195 192
    let highlights_tx = TerminalEvent::spawn_highlight_worker(events_tx, highlighter);
196 193
    let mut session = TerminalSession {
197 194
        terminal,
198 195
        viewer,
199 196
        highlights_tx,
200 -
        header,
201 197
    };
202 198
    session.redraw()?;
203 199
204 200
    while let Ok(event) = events_rx.recv() {
205 201
        match event {
228 224
    terminal: &'a mut DiffTerminal,
229 225
    /// Diff viewer state.
230 226
    viewer: &'a mut DiffViewer,
231 227
    /// Visible-row highlight request channel.
232 228
    highlights_tx: mpsc::Sender<Vec<HighlightRequest>>,
233 -
    /// Top chrome describing the current diff session.
234 -
    header: Header,
235 229
}
236 230
237 231
impl TerminalSession<'_> {
238 232
    /// Draws a frame and requests highlights for visible rows.
239 233
    fn redraw(&mut self) -> io::Result<()> {
253 247
254 248
    /// Draws one terminal frame without scheduling highlight work.
255 249
    fn draw_frame(&mut self) -> io::Result<()> {
256 250
        self.terminal.draw(|frame| {
257 251
            let body = viewer_area(frame.area);
258 -
            draw_header(frame, &self.header);
259 252
            frame.render_widget(&mut *self.viewer, body);
260 253
            draw_status(frame);
261 254
        })
262 255
    }
263 256
}
264 257
265 -
/// Top header metadata for an interactive diff session.
266 -
#[derive(Clone, Debug, Eq, PartialEq)]
267 -
struct Header {
268 -
    /// Current working directory.
269 -
    cwd: PathBuf,
270 -
    /// Human-readable action being displayed.
271 -
    action: String,
272 -
}
273 -
274 -
impl Header {
275 -
    /// Returns metadata for a direct `git diff` invocation.
276 -
    fn git_diff(cwd: PathBuf, args: &[OsString]) -> Self {
277 -
        let mut action = String::from("git diff");
278 -
        for arg in args {
279 -
            action.push(' ');
280 -
            action.push_str(&arg.to_string_lossy());
281 -
        }
282 -
283 -
        Self { cwd, action }
284 -
    }
285 -
286 -
    /// Returns metadata for diff input read from standard input.
287 -
    fn stdin(cwd: PathBuf) -> Self {
288 -
        Self {
289 -
            cwd,
290 -
            action: String::from("stdin diff"),
291 -
        }
292 -
    }
293 -
}
294 -
295 258
/// Event delivered to the terminal loop.
296 259
enum TerminalEvent {
297 260
    /// Terminal input event.
298 261
    Input(io::Result<Event<()>>),
299 262
    /// Background highlight results.
331 294
        });
332 295
        requests_tx
333 296
    }
334 297
}
335 298
336 -
/// Returns the viewer area between header and footer chrome.
299 +
/// Returns the viewer area above the footer chrome.
337 300
fn viewer_area(area: Rect) -> Rect {
338 -
    let header_rows = HEADER_ROWS.min(area.rows);
339 -
    let footer_rows = FOOTER_ROWS.min(area.rows.saturating_sub(header_rows));
301 +
    let footer_rows = FOOTER_ROWS.min(area.rows);
340 302
341 303
    Rect::new(
342 304
        area.x,
343 -
        area.y + header_rows,
305 +
        area.y,
344 306
        area.cols,
345 -
        area.rows.saturating_sub(header_rows + footer_rows),
307 +
        area.rows.saturating_sub(footer_rows),
346 308
    )
347 309
}
348 310
349 -
/// Draws the top header row.
350 -
fn draw_header(frame: &mut tui::Frame<'_>, header: &Header) {
351 -
    let area = frame.area;
352 -
    if area.rows == 0 {
353 -
        return;
354 -
    }
355 -
    let row = Rect::new(area.x, area.y, area.cols, 1);
356 -
357 -
    frame.render_widget(header_line(header, area.cols), row);
358 -
}
359 -
360 -
/// Returns the padded top header line.
361 -
fn header_line(header: &Header, cols: usize) -> Line {
362 -
    let folder = Label::new(&header.cwd.display().to_string()).style(header_path_style());
363 -
    let action = Label::new(&header.action).fg(FOOTER_FG);
364 -
    let occupied = Line::from(vec![folder.clone(), action.clone()]).width();
365 -
    let spacer = cols.saturating_sub(occupied);
366 -
    let mut line = Line::new(folder);
367 -
368 -
    line.push(Label::new(&" ".repeat(spacer)));
369 -
    line.push(action);
370 -
    line.style(Style::default().bg(FOOTER_BG)).padded(cols)
371 -
}
372 -
373 -
/// Returns the style for the current path in the top header.
374 -
fn header_path_style() -> Style {
375 -
    Style::new(FOOTER_FG).bg(FOOTER_BG).bold()
376 -
}
377 -
378 311
/// Draws the bottom status row.
379 312
fn draw_status(frame: &mut tui::Frame<'_>) {
380 313
    let area = frame.area;
381 -
    if area.rows <= HEADER_ROWS {
314 +
    if area.rows == 0 {
382 315
        return;
383 316
    }
384 317
    let row = Rect::new(area.x, area.bottom().saturating_sub(1), area.cols, 1);
385 318
386 319
    frame.render_widget(status_line(area.cols), row);
+0 -0
+0 -0
a.rs +0 -0
a.rs\x1b[m +0 -0