Skip to content

Commit

Permalink
feat(highlight): add highlight for buffer and show selected line on find
Browse files Browse the repository at this point in the history
files command
  • Loading branch information
metiftikci committed Oct 22, 2023
1 parent 6e357d0 commit a4ac6f9
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 304 deletions.
8 changes: 7 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod highlight;
pub mod maps;
pub mod mode;
pub mod movement;
Expand All @@ -11,6 +12,7 @@ use std::collections::HashMap;

use crate::{
buffer::{
highlight::Highlight,
maps::{
get_default_command_maps, get_default_insert_maps, get_default_normal_maps,
get_default_visual_maps,
Expand All @@ -19,7 +21,7 @@ use crate::{
options::BufferOptions,
select::Select,
},
core::{Point, Rectangle},
core::{Point, Rectangle, Style},
editor::Editor,
};

Expand All @@ -42,6 +44,8 @@ pub struct Buffer {
pub popups: Vec<Buffer>,
pub active_popup: Option<usize>,
pub options: BufferOptions,
pub styles: HashMap<&'static str, Style>,
pub highlights: Vec<Highlight>,
}

impl Buffer {
Expand All @@ -65,6 +69,8 @@ impl Buffer {
popups: vec![],
active_popup: None,
options: BufferOptions::default(),
styles: HashMap::new(),
highlights: vec![],
};

buffer.set_size(area);
Expand Down
82 changes: 82 additions & 0 deletions src/buffer/highlight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::{buffer::Buffer, core::Rectangle};

pub struct Highlight {
pub name: &'static str,
pub row: usize,
pub start: usize,
pub end: usize,
}

impl Highlight {
pub fn is_visible_in_area(&self, area: Rectangle<usize>) -> bool {
!(self.row < area.y
|| area.y + area.height <= self.row
|| self.end < area.x
|| area.x + area.width <= self.start)
}
}

impl Buffer {
pub fn get_dynamic_highlights(&self) -> Vec<Highlight> {
let mut list: Vec<Highlight> = vec![];

list.push(Highlight {
name: "SelectedLine",
row: self.cursor.x,
start: self.scroll.x,
end: self.text_area.width as usize,
});

if self.lines.len() == 0 {
panic!("I have no lines");
}

let mut end = self.get_line(self.cursor.y).len();

if end > 0 {
end -= 1;
}

list.push(Highlight {
name: "SelectedLineText",
row: self.cursor.y,
start: 0,
end,
});

list
}
}

#[cfg(test)]
mod tests {
use crate::{buffer::Highlight, core::Rectangle};

fn test1(row: usize, start: usize, end: usize) -> bool {
let area = Rectangle {
x: 0,
y: 0,
width: 10,
height: 10,
};

let highlight = Highlight {
name: "Test",
row,
start,
end,
};

highlight.is_visible_in_area(area)
}

#[test]
fn is_visible_in_area_test() {
assert_eq!(true, test1(0, 0, 8));
assert_eq!(true, test1(0, 1, 9));
assert_eq!(true, test1(0, 0, 9));
assert_eq!(true, test1(0, 0, 10));
assert_eq!(false, test1(0, 10, 10));
assert_eq!(false, test1(10, 0, 1));
}
}
17 changes: 16 additions & 1 deletion src/commands/find_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::process::Command;

use crate::{buffer::Buffer, core::Rectangle, editor::Editor};
use crate::{
buffer::Buffer,
core::{Rectangle, Style},
editor::Editor,
theme::SILVER,
};

pub struct FindFileCommand {}

Expand All @@ -16,6 +21,16 @@ impl FindFileCommand {
files_popup.options.show_border = true;
files_popup.options.show_info_column = false;
files_popup.set_size(files_popup.area.clone()); // TODO: Move
files_popup.styles.insert(
"SelectedLineText",
Style {
fg: (88, 129, 87),
bg: SILVER,
bold: false,
italic: false,
underline: false,
},
);

let mut textbox_popup = Buffer::new(Rectangle {
x: files_popup.area.x,
Expand Down
11 changes: 11 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ pub struct Size<T> {
pub height: T,
}

pub type Color = (u8, u8, u8);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Style {
pub fg: Color,
pub bg: Color,
pub bold: bool,
pub underline: bool,
pub italic: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rectangle<T> {
pub x: T,
Expand Down
Loading

0 comments on commit a4ac6f9

Please sign in to comment.