Skip to content

Commit

Permalink
fix(info_column): calculate info column width
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 15, 2023
1 parent f38f87e commit 4a2ad1c
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 83 deletions.
28 changes: 22 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub mod text;

use std::collections::HashMap;

use crate::core::{editable_text::EditableText, Point, Size};
use crate::core::Rectangle;
use crate::core::{editable_text::EditableText, Point};
use crate::{
buffer::{
maps::{
Expand All @@ -28,7 +29,9 @@ pub type ActionMap = HashMap<&'static str, fn(&mut Editor)>;
pub struct Buffer {
pub file_name: Option<String>,
pub mode: BufferMode,
pub area: Size<u16>,
pub area: Rectangle<u16>,
pub info_area: Rectangle<u16>,
pub text_area: Rectangle<u16>,
pub scroll: Point<usize>,
pub cursor: Point<usize>,
pub lines: Vec<String>,
Expand All @@ -41,11 +44,13 @@ pub struct Buffer {
}

impl Buffer {
pub fn new(area: Size<u16>) -> Buffer {
Buffer {
pub fn new(area: Rectangle<u16>) -> Buffer {
let mut buffer = Buffer {
file_name: None,
mode: BufferMode::Normal,
area,
area: area.clone(),
info_area: Rectangle::zero(),
text_area: Rectangle::zero(),
scroll: Point { x: 0, y: 0 },
cursor: Point { x: 0, y: 0 },
lines: vec![String::new()],
Expand All @@ -60,6 +65,17 @@ impl Buffer {
actions_insert: get_default_insert_maps(),
actions_normal: get_default_normal_maps(),
actions_visual: get_default_visual_maps(),
}
};

buffer.set_size(area);
buffer
}

pub fn set_size(&mut self, area: Rectangle<u16>) {
self.info_area = area.clone();
self.text_area = area.clone();
self.info_area.width = 2 + self.lines.len().to_string().len() as u16;
self.text_area.x = self.info_area.x + self.info_area.width;
self.text_area.width = area.width - self.info_area.width;
}
}
10 changes: 5 additions & 5 deletions src/buffer/actions/find_next_word_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ pub fn find_next_word_position(buffer: &Buffer) -> Point<usize> {
mod tests {
use crate::{
buffer::{actions::find_next_word_position::find_next_word_position, Buffer},
core::Size,
core::Rectangle,
};

#[test]
fn test() {
let mut buffer = Buffer::new(Size {
width: 10,
height: 10,
});
let mut area = Rectangle::<u16>::zero();
area.width = 10;
area.height = 10;
let mut buffer = Buffer::new(area);

let mut position = find_next_word_position(&buffer);

Expand Down
10 changes: 2 additions & 8 deletions src/buffer/maps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use crate::{
buffer::actions::find_next_word_position::find_next_word_position, core::Size,
buffer::actions::find_next_word_position::find_next_word_position,
plugins::explorer::explorer_buffer::create_explorer_buffer,
};

Expand Down Expand Up @@ -111,13 +111,7 @@ pub fn get_default_command_maps() -> ActionMap {
let command = editor.get_active_buffer().command.text.trim();
if command == "e" {
let tab = editor.get_active_tab();
let buffer = create_explorer_buffer(
String::from("."),
Size {
width: tab.size.width,
height: tab.size.height,
},
);
let buffer = create_explorer_buffer(String::from("."), tab.area.clone());
let tab = editor.get_active_tab_mut();
tab.buffers.push(buffer);
tab.active_buffer = tab.buffers.len() - 1;
Expand Down
18 changes: 9 additions & 9 deletions src/buffer/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ impl Buffer {

if self.cursor.x < self.scroll.x {
self.scroll.x = self.cursor.x
} else if self.scroll.x + (self.area.width as usize) <= self.cursor.x {
self.scroll.x = self.cursor.x - (self.area.width as usize) + 1;
} else if self.scroll.x + (self.text_area.width as usize) <= self.cursor.x {
self.scroll.x = self.cursor.x - (self.text_area.width as usize) + 1;
}

if self.cursor.y < self.scroll.y {
self.scroll.y = self.cursor.y;
} else if self.scroll.y + (self.area.height as usize) <= self.cursor.y {
self.scroll.y = self.cursor.y - (self.area.height as usize) + 1;
} else if self.scroll.y + (self.text_area.height as usize) <= self.cursor.y {
self.scroll.y = self.cursor.y - (self.text_area.height as usize) + 1;
}
}

Expand Down Expand Up @@ -59,13 +59,13 @@ impl Buffer {

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

fn create_buffer() -> Buffer {
Buffer::new(Size {
width: 5,
height: 5,
})
let mut area = Rectangle::<u16>::zero();
area.width = 8;
area.height = 5;
Buffer::new(area)
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions src/buffer/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Buffer {
} else {
line.remove(column);
self.move_cursor(row, column);
self.set_size(self.area.clone());
}
}

Expand All @@ -48,6 +49,7 @@ impl Buffer {
pub fn insert_newline(&mut self, row: usize) {
self.lines.insert(row, String::new());
self.move_cursor(row, 0);
self.set_size(self.area.clone());
}

pub fn split_line(&mut self, row: usize, column: usize) {
Expand All @@ -61,13 +63,15 @@ impl Buffer {
self.lines.insert(row + 1, right_string);

self.move_cursor(row + 1, 0);
self.set_size(self.area.clone());
}

pub fn join_lines(&mut self, row1: usize, row2: usize) {
let line2 = self.get_line(row2).clone();
let line1 = self.get_line_mut(row1);
line1.push_str(&line2);
self.lines.remove(row2);
self.set_size(self.area.clone());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/buffer/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::buffer::Buffer;

impl Buffer {
pub fn column_to_visible_x(&self, column: usize) -> u16 {
(column - self.scroll.x) as u16
self.text_area.width + (column - self.scroll.x) as u16
}

pub fn row_to_visible_y(&self, row: usize) -> u16 {
(row - self.scroll.y) as u16
self.text_area.height + (row - self.scroll.y) as u16
}
}
5 changes: 1 addition & 4 deletions src/buffer/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ impl Buffer {
if line.len() <= start_index {
return Some(String::new());
}
let end_index = min(
line.len(),
self.scroll.x + (self.area.width as usize),
);
let end_index = min(line.len(), self.scroll.x + (self.text_area.width as usize));

Some(line[start_index..end_index].to_string())
}
Expand Down
41 changes: 41 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,44 @@ pub struct Size<T> {
pub width: T,
pub height: T,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rectangle<T> {
pub x: T,
pub y: T,
pub width: T,
pub height: T,
}

impl<T> Rectangle<T> {
pub fn from(x: T, y: T, width: T, height: T) -> Self {
Self {
x,
y,
width,
height,
}
}
}

impl<T: From<u16>> Rectangle<T> {
pub fn zero() -> Self {
Self {
x: 0.into(),
y: 0.into(),
width: 0.into(),
height: 0.into(),
}
}
}

impl<T: From<u16>> Rectangle<T> {
pub fn from_size(size: Size<T>) -> Self {
Self {
x: 0.into(),
y: 0.into(),
width: size.width,
height: size.height,
}
}
}
43 changes: 26 additions & 17 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
use crate::{
buffer::{mode::BufferMode, Buffer},
core::{key::Key, Size},
core::{key::Key, Rectangle},
tab::Tab,
};

pub struct Editor {
pub size: Size<u16>,
pub area: Rectangle<u16>,
pub tabs_area: Rectangle<u16>,
pub document_area: Rectangle<u16>,
pub status_area: Rectangle<u16>,
pub tabs: Vec<Tab>,
pub active_tab: usize,
pub document_area: Size<u16>,
}

impl Editor {
pub fn new(size: Size<u16>) -> Self {
Self {
size: size.clone(),
pub fn new(area: Rectangle<u16>) -> Self {
let mut editor = Self {
area: area.clone(),
tabs_area: Rectangle::zero(),
document_area: Rectangle::zero(),
status_area: Rectangle::zero(),
tabs: vec![],
active_tab: 0,
document_area: Size {
width: size.width,
height: size.height - 2,
},
}
};

editor.set_size(area);
editor
}

pub fn create_new_tab(&mut self) -> &mut Tab {
Expand All @@ -48,14 +52,19 @@ impl Editor {
self.get_active_tab_mut().get_active_buffer_mut()
}

pub fn set_size(&mut self, width: u16, height: u16) {
self.size.width = width;
self.size.height = height;
self.document_area.width = width;
self.document_area.height = height - 2;
pub fn set_size(&mut self, area: Rectangle<u16>) {
self.area = area.clone();
self.tabs_area = area.clone();
self.tabs_area.height = 1;
self.document_area = area.clone();
self.document_area.y += self.tabs_area.height;
self.document_area.height -= 2;
self.status_area = area.clone();
self.status_area.y = self.document_area.y + self.document_area.height;
self.status_area.height = 1;

for tab in self.tabs.iter_mut() {
tab.set_size(self.document_area.width, self.document_area.height);
tab.set_size(self.document_area.clone());
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod terminal;

use std::io::Result;

use crate::core::Rectangle;

use crate::{
editor::Editor,
palette::Palette,
Expand All @@ -17,8 +19,9 @@ use crate::{

fn main() -> Result<()> {
let terminal_size = Terminal::get_terminal_size()?;
let editor_area: Rectangle<u16> = Rectangle::<u16>::from_size(terminal_size);

let mut editor = Editor::new(terminal_size);
let mut editor = Editor::new(editor_area);
let tab = editor.create_new_tab();
tab.create_new_buffer();

Expand All @@ -31,7 +34,7 @@ fn main() -> Result<()> {

while let Ok(event) = terminal.read() {
match event {
TerminalEvent::Resize(size) => editor.set_size(size.width, size.height),
TerminalEvent::Resize(size) => editor.set_size(Rectangle::<u16>::from_size(size)),
TerminalEvent::Key(key) => {
if key.ctrl && key.code == String::from("c") {
break;
Expand Down
15 changes: 7 additions & 8 deletions src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,13 @@ impl Palette {

pub fn from(editor: &Editor) -> Self {
let tab = editor.get_active_tab();
let mut palette = Palette::new(editor.size.height, editor.size.width);

let number_column_width = tab.get_active_buffer().lines.len().to_string().len();

palette.cursor.x = tab.get_active_buffer_visible_x(tab.get_active_buffer().cursor.x);
palette.cursor.x += number_column_width as u16 + 2;
palette.cursor.y = tab.get_active_buffer_visible_y(tab.get_active_buffer().cursor.y);
let mut palette = Palette::new(editor.area.height, editor.area.width);

let buffer = tab.get_active_buffer();

palette.cursor.x = buffer.text_area.x + (buffer.cursor.x - buffer.scroll.x) as u16;
palette.cursor.y = buffer.text_area.y + (buffer.cursor.y - buffer.scroll.y) as u16;

match buffer.mode {
BufferMode::Insert => palette.cursor_style = CursorStyle::BlinkingBar,
BufferMode::Command => palette.cursor_style = CursorStyle::BlinkingBar,
Expand All @@ -91,14 +88,16 @@ impl Palette {
None => palette.print(0, 0, &String::from("[No Name]")),
}

let info_area_width = buffer.info_area.width as usize - 2;

for y in 0..buffer.area.height {
let row_index = buffer.scroll.y + y as usize;
match buffer.get_line_visible_text(row_index) {
Some(text) => {
palette.print(
y + 1,
0,
&format!(" {:>2$} {}", row_index + 1, text, number_column_width),
&format!(" {:>2$} {}", row_index + 1, text, info_area_width),
);
}
None => palette.print(y + 1, 0, &format!("~")),
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/explorer/explorer_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs, path::PathBuf};

use crate::{buffer::Buffer, core::Size};
use crate::{buffer::Buffer, core::Rectangle};

fn get_file_list(directory: &String) -> Vec<String> {
let paths = fs::read_dir(directory).unwrap();
Expand Down Expand Up @@ -28,7 +28,7 @@ fn get_file_list(directory: &String) -> Vec<String> {
files
}

pub fn create_explorer_buffer(base_path: String, area: Size<u16>) -> Buffer {
pub fn create_explorer_buffer(base_path: String, area: Rectangle<u16>) -> Buffer {
let files = get_file_list(&base_path);

let mut buffer = Buffer::new(area);
Expand Down
Loading

0 comments on commit 4a2ad1c

Please sign in to comment.