rat_ftable/
util.rs

1use ratatui_core::buffer::Buffer;
2use ratatui_core::layout::Rect;
3use ratatui_core::style::Style;
4use std::mem;
5
6/// Clear an area of the buffer.
7pub(crate) fn clear_buf_area(area: Rect, buf: &mut Buffer) {
8    for y in area.top()..area.bottom() {
9        for x in area.left()..area.right() {
10            if let Some(cell) = buf.cell_mut((x, y)) {
11                cell.set_symbol(" ");
12            }
13        }
14    }
15}
16
17pub(crate) fn revert_style(mut style: Style) -> Style {
18    if style.fg.is_some() && style.bg.is_some() {
19        mem::swap(&mut style.fg, &mut style.bg);
20        style
21    } else {
22        style.black().on_white()
23    }
24}
25
26/// Fallback for select style.
27pub(crate) fn fallback_select_style(style: Style) -> Style {
28    if style.fg.is_some() || style.bg.is_some() {
29        style
30    } else {
31        style.underlined()
32    }
33}
34
35/// Move a tmp-buffer to a target.
36/// All cells in the tmp-buffer are reset to defaults.
37///
38/// * tmp: Temporary buffer
39/// * h_offset: Left shift of the tmp-buffer.
40/// * view_area: clipped area in the target buffer.
41/// * buf: Target buffer
42#[allow(clippy::collapsible_if)]
43pub(crate) fn transfer_buffer(tmp: &mut Buffer, h_offset: u16, view_area: Rect, buf: &mut Buffer) {
44    // copy buffer
45    for (cell_offset, cell) in tmp.content.iter_mut().enumerate() {
46        let tmp_row = cell_offset as u16 / tmp.area.width;
47        let tmp_col = cell_offset as u16 % tmp.area.width;
48
49        let cell = mem::take(cell);
50
51        // ensure tmp_col-h_offset doesn't underflow.
52        if tmp_col >= h_offset {
53            let buf_row = view_area.y + tmp_row;
54            let buf_col = view_area.x + tmp_col - h_offset;
55
56            if view_area.contains((buf_col, buf_row).into()) {
57                if let Some(buf_cell) = buf.cell_mut((buf_col, buf_row)) {
58                    *buf_cell = cell
59                }
60            }
61        }
62    }
63}