refactor: ui add fold markers

This commit is contained in:
graelo 2021-03-22 06:48:09 +01:00
parent 863cd2e082
commit 9611b8a36d

View file

@ -25,6 +25,8 @@ pub struct ViewController<'a> {
} }
impl<'a> ViewController<'a> { impl<'a> ViewController<'a> {
// Initialize {{{1
pub fn new( pub fn new(
model: &'a mut textbuf::Model<'a>, model: &'a mut textbuf::Model<'a>,
unique_hint: bool, unique_hint: bool,
@ -56,6 +58,9 @@ impl<'a> ViewController<'a> {
} }
} }
// }}}
// Coordinates {{{1
/// Convert the `Match` text into the coordinates of the wrapped lines. /// Convert the `Match` text into the coordinates of the wrapped lines.
/// ///
/// Compute the new x offset of the text as the remainder of the line width /// Compute the new x offset of the text as the remainder of the line width
@ -65,7 +70,7 @@ impl<'a> ViewController<'a> {
/// Compute the new y offset of the text as the initial y offset plus any /// Compute the new y offset of the text as the initial y offset plus any
/// additional offset due to previous split lines. This is obtained thanks to /// additional offset due to previous split lines. This is obtained thanks to
/// the `offset_per_line` member. /// the `offset_per_line` member.
pub fn map_coords_to_wrapped_space(&self, offset_x: usize, offset_y: usize) -> (usize, usize) { fn map_coords_to_wrapped_space(&self, offset_x: usize, offset_y: usize) -> (usize, usize) {
let line_width = self.term_width as usize; let line_width = self.term_width as usize;
let new_offset_x = offset_x % line_width; let new_offset_x = offset_x % line_width;
@ -75,6 +80,27 @@ impl<'a> ViewController<'a> {
(new_offset_x, new_offset_y) (new_offset_x, new_offset_y)
} }
/// Returns screen offset of a given `Match`.
///
/// If multibyte characters occur before the hint (in the "prefix"), then
/// their compouding takes less space on screen when printed: for
/// instance ´ + e = é. Consequently the hint offset has to be adjusted
/// to the left.
fn match_offsets(&self, mat: &textbuf::Match<'a>) -> (usize, usize) {
let offset_x = {
let line = &self.model.lines[mat.y as usize];
let prefix = &line[0..mat.x as usize];
let adjust = prefix.len() - prefix.chars().count();
(mat.x as usize) - (adjust)
};
let offset_y = mat.y as usize;
(offset_x, offset_y)
}
// }}}
// Focus management {{{1
/// Move focus onto the previous hint, returning both the index of the /// Move focus onto the previous hint, returning both the index of the
/// previously focused match, and the index of the newly focused one. /// previously focused match, and the index of the newly focused one.
fn prev_focus_index(&mut self) -> (usize, usize) { fn prev_focus_index(&mut self) -> (usize, usize) {
@ -109,23 +135,8 @@ impl<'a> ViewController<'a> {
(old_index, new_index) (old_index, new_index)
} }
/// Returns screen offset of a given `Match`. // }}}
/// // Rendering {{{1
/// If multibyte characters occur before the hint (in the "prefix"), then
/// their compouding takes less space on screen when printed: for
/// instance ´ + e = é. Consequently the hint offset has to be adjusted
/// to the left.
fn match_offsets(&self, mat: &textbuf::Match<'a>) -> (usize, usize) {
let offset_x = {
let line = &self.model.lines[mat.y as usize];
let prefix = &line[0..mat.x as usize];
let adjust = prefix.len() - prefix.chars().count();
(mat.x as usize) - (adjust)
};
let offset_y = mat.y as usize;
(offset_x, offset_y)
}
/// Render entire model lines on provided writer. /// Render entire model lines on provided writer.
/// ///
@ -398,6 +409,9 @@ impl<'a> ViewController<'a> {
stdout.flush().unwrap(); stdout.flush().unwrap();
} }
// }}}
// Listening {{{1
/// Listen to keys entered on stdin, moving focus accordingly, or /// Listen to keys entered on stdin, moving focus accordingly, or
/// selecting one match. /// selecting one match.
/// ///
@ -551,6 +565,9 @@ impl<'a> ViewController<'a> {
Event::Exit Event::Exit
} }
// }}}
// Presenting {{{1
/// Configure the terminal and display the `Ui`. /// Configure the terminal and display the `Ui`.
/// ///
/// - Setup steps: switch to alternate screen, switch to raw mode, hide the cursor. /// - Setup steps: switch to alternate screen, switch to raw mode, hide the cursor.
@ -578,6 +595,8 @@ impl<'a> ViewController<'a> {
selection selection
} }
// }}}
} }
/// Compute each line's actual y offset if displayed in a terminal of width /// Compute each line's actual y offset if displayed in a terminal of width