Skip to content

Commit 4a2ad1c

Browse files
committed
fix(info_column): calculate info column width
1 parent f38f87e commit 4a2ad1c

File tree

13 files changed

+136
-83
lines changed

13 files changed

+136
-83
lines changed

src/buffer.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ pub mod text;
1010

1111
use std::collections::HashMap;
1212

13-
use crate::core::{editable_text::EditableText, Point, Size};
13+
use crate::core::Rectangle;
14+
use crate::core::{editable_text::EditableText, Point};
1415
use crate::{
1516
buffer::{
1617
maps::{
@@ -28,7 +29,9 @@ pub type ActionMap = HashMap<&'static str, fn(&mut Editor)>;
2829
pub struct Buffer {
2930
pub file_name: Option<String>,
3031
pub mode: BufferMode,
31-
pub area: Size<u16>,
32+
pub area: Rectangle<u16>,
33+
pub info_area: Rectangle<u16>,
34+
pub text_area: Rectangle<u16>,
3235
pub scroll: Point<usize>,
3336
pub cursor: Point<usize>,
3437
pub lines: Vec<String>,
@@ -41,11 +44,13 @@ pub struct Buffer {
4144
}
4245

4346
impl Buffer {
44-
pub fn new(area: Size<u16>) -> Buffer {
45-
Buffer {
47+
pub fn new(area: Rectangle<u16>) -> Buffer {
48+
let mut buffer = Buffer {
4649
file_name: None,
4750
mode: BufferMode::Normal,
48-
area,
51+
area: area.clone(),
52+
info_area: Rectangle::zero(),
53+
text_area: Rectangle::zero(),
4954
scroll: Point { x: 0, y: 0 },
5055
cursor: Point { x: 0, y: 0 },
5156
lines: vec![String::new()],
@@ -60,6 +65,17 @@ impl Buffer {
6065
actions_insert: get_default_insert_maps(),
6166
actions_normal: get_default_normal_maps(),
6267
actions_visual: get_default_visual_maps(),
63-
}
68+
};
69+
70+
buffer.set_size(area);
71+
buffer
72+
}
73+
74+
pub fn set_size(&mut self, area: Rectangle<u16>) {
75+
self.info_area = area.clone();
76+
self.text_area = area.clone();
77+
self.info_area.width = 2 + self.lines.len().to_string().len() as u16;
78+
self.text_area.x = self.info_area.x + self.info_area.width;
79+
self.text_area.width = area.width - self.info_area.width;
6480
}
6581
}

src/buffer/actions/find_next_word_position.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ pub fn find_next_word_position(buffer: &Buffer) -> Point<usize> {
6262
mod tests {
6363
use crate::{
6464
buffer::{actions::find_next_word_position::find_next_word_position, Buffer},
65-
core::Size,
65+
core::Rectangle,
6666
};
6767

6868
#[test]
6969
fn test() {
70-
let mut buffer = Buffer::new(Size {
71-
width: 10,
72-
height: 10,
73-
});
70+
let mut area = Rectangle::<u16>::zero();
71+
area.width = 10;
72+
area.height = 10;
73+
let mut buffer = Buffer::new(area);
7474

7575
let mut position = find_next_word_position(&buffer);
7676

src/buffer/maps.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use crate::{
4-
buffer::actions::find_next_word_position::find_next_word_position, core::Size,
4+
buffer::actions::find_next_word_position::find_next_word_position,
55
plugins::explorer::explorer_buffer::create_explorer_buffer,
66
};
77

@@ -111,13 +111,7 @@ pub fn get_default_command_maps() -> ActionMap {
111111
let command = editor.get_active_buffer().command.text.trim();
112112
if command == "e" {
113113
let tab = editor.get_active_tab();
114-
let buffer = create_explorer_buffer(
115-
String::from("."),
116-
Size {
117-
width: tab.size.width,
118-
height: tab.size.height,
119-
},
120-
);
114+
let buffer = create_explorer_buffer(String::from("."), tab.area.clone());
121115
let tab = editor.get_active_tab_mut();
122116
tab.buffers.push(buffer);
123117
tab.active_buffer = tab.buffers.len() - 1;

src/buffer/movement.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ impl Buffer {
99

1010
if self.cursor.x < self.scroll.x {
1111
self.scroll.x = self.cursor.x
12-
} else if self.scroll.x + (self.area.width as usize) <= self.cursor.x {
13-
self.scroll.x = self.cursor.x - (self.area.width as usize) + 1;
12+
} else if self.scroll.x + (self.text_area.width as usize) <= self.cursor.x {
13+
self.scroll.x = self.cursor.x - (self.text_area.width as usize) + 1;
1414
}
1515

1616
if self.cursor.y < self.scroll.y {
1717
self.scroll.y = self.cursor.y;
18-
} else if self.scroll.y + (self.area.height as usize) <= self.cursor.y {
19-
self.scroll.y = self.cursor.y - (self.area.height as usize) + 1;
18+
} else if self.scroll.y + (self.text_area.height as usize) <= self.cursor.y {
19+
self.scroll.y = self.cursor.y - (self.text_area.height as usize) + 1;
2020
}
2121
}
2222

@@ -59,13 +59,13 @@ impl Buffer {
5959

6060
#[cfg(test)]
6161
pub mod tests {
62-
use crate::{buffer::Buffer, core::Size};
62+
use crate::{buffer::Buffer, core::Rectangle};
6363

6464
fn create_buffer() -> Buffer {
65-
Buffer::new(Size {
66-
width: 5,
67-
height: 5,
68-
})
65+
let mut area = Rectangle::<u16>::zero();
66+
area.width = 8;
67+
area.height = 5;
68+
Buffer::new(area)
6969
}
7070

7171
#[test]

src/buffer/operations.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl Buffer {
2727
} else {
2828
line.remove(column);
2929
self.move_cursor(row, column);
30+
self.set_size(self.area.clone());
3031
}
3132
}
3233

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

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

6365
self.move_cursor(row + 1, 0);
66+
self.set_size(self.area.clone());
6467
}
6568

6669
pub fn join_lines(&mut self, row1: usize, row2: usize) {
6770
let line2 = self.get_line(row2).clone();
6871
let line1 = self.get_line_mut(row1);
6972
line1.push_str(&line2);
7073
self.lines.remove(row2);
74+
self.set_size(self.area.clone());
7175
}
7276
}
7377

src/buffer/paint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use crate::buffer::Buffer;
22

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

88
pub fn row_to_visible_y(&self, row: usize) -> u16 {
9-
(row - self.scroll.y) as u16
9+
self.text_area.height + (row - self.scroll.y) as u16
1010
}
1111
}

src/buffer/text.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ impl Buffer {
2424
if line.len() <= start_index {
2525
return Some(String::new());
2626
}
27-
let end_index = min(
28-
line.len(),
29-
self.scroll.x + (self.area.width as usize),
30-
);
27+
let end_index = min(line.len(), self.scroll.x + (self.text_area.width as usize));
3128

3229
Some(line[start_index..end_index].to_string())
3330
}

src/core.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,44 @@ pub struct Size<T> {
1212
pub width: T,
1313
pub height: T,
1414
}
15+
16+
#[derive(Clone, Debug, Eq, PartialEq)]
17+
pub struct Rectangle<T> {
18+
pub x: T,
19+
pub y: T,
20+
pub width: T,
21+
pub height: T,
22+
}
23+
24+
impl<T> Rectangle<T> {
25+
pub fn from(x: T, y: T, width: T, height: T) -> Self {
26+
Self {
27+
x,
28+
y,
29+
width,
30+
height,
31+
}
32+
}
33+
}
34+
35+
impl<T: From<u16>> Rectangle<T> {
36+
pub fn zero() -> Self {
37+
Self {
38+
x: 0.into(),
39+
y: 0.into(),
40+
width: 0.into(),
41+
height: 0.into(),
42+
}
43+
}
44+
}
45+
46+
impl<T: From<u16>> Rectangle<T> {
47+
pub fn from_size(size: Size<T>) -> Self {
48+
Self {
49+
x: 0.into(),
50+
y: 0.into(),
51+
width: size.width,
52+
height: size.height,
53+
}
54+
}
55+
}

src/editor.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
use crate::{
22
buffer::{mode::BufferMode, Buffer},
3-
core::{key::Key, Size},
3+
core::{key::Key, Rectangle},
44
tab::Tab,
55
};
66

77
pub struct Editor {
8-
pub size: Size<u16>,
8+
pub area: Rectangle<u16>,
9+
pub tabs_area: Rectangle<u16>,
10+
pub document_area: Rectangle<u16>,
11+
pub status_area: Rectangle<u16>,
912
pub tabs: Vec<Tab>,
1013
pub active_tab: usize,
11-
pub document_area: Size<u16>,
1214
}
1315

1416
impl Editor {
15-
pub fn new(size: Size<u16>) -> Self {
16-
Self {
17-
size: size.clone(),
17+
pub fn new(area: Rectangle<u16>) -> Self {
18+
let mut editor = Self {
19+
area: area.clone(),
20+
tabs_area: Rectangle::zero(),
21+
document_area: Rectangle::zero(),
22+
status_area: Rectangle::zero(),
1823
tabs: vec![],
1924
active_tab: 0,
20-
document_area: Size {
21-
width: size.width,
22-
height: size.height - 2,
23-
},
24-
}
25+
};
26+
27+
editor.set_size(area);
28+
editor
2529
}
2630

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

51-
pub fn set_size(&mut self, width: u16, height: u16) {
52-
self.size.width = width;
53-
self.size.height = height;
54-
self.document_area.width = width;
55-
self.document_area.height = height - 2;
55+
pub fn set_size(&mut self, area: Rectangle<u16>) {
56+
self.area = area.clone();
57+
self.tabs_area = area.clone();
58+
self.tabs_area.height = 1;
59+
self.document_area = area.clone();
60+
self.document_area.y += self.tabs_area.height;
61+
self.document_area.height -= 2;
62+
self.status_area = area.clone();
63+
self.status_area.y = self.document_area.y + self.document_area.height;
64+
self.status_area.height = 1;
5665

5766
for tab in self.tabs.iter_mut() {
58-
tab.set_size(self.document_area.width, self.document_area.height);
67+
tab.set_size(self.document_area.clone());
5968
}
6069
}
6170

src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub mod terminal;
99

1010
use std::io::Result;
1111

12+
use crate::core::Rectangle;
13+
1214
use crate::{
1315
editor::Editor,
1416
palette::Palette,
@@ -17,8 +19,9 @@ use crate::{
1719

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

21-
let mut editor = Editor::new(terminal_size);
24+
let mut editor = Editor::new(editor_area);
2225
let tab = editor.create_new_tab();
2326
tab.create_new_buffer();
2427

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

3235
while let Ok(event) = terminal.read() {
3336
match event {
34-
TerminalEvent::Resize(size) => editor.set_size(size.width, size.height),
37+
TerminalEvent::Resize(size) => editor.set_size(Rectangle::<u16>::from_size(size)),
3538
TerminalEvent::Key(key) => {
3639
if key.ctrl && key.code == String::from("c") {
3740
break;

src/palette.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,13 @@ impl Palette {
7070

7171
pub fn from(editor: &Editor) -> Self {
7272
let tab = editor.get_active_tab();
73-
let mut palette = Palette::new(editor.size.height, editor.size.width);
74-
75-
let number_column_width = tab.get_active_buffer().lines.len().to_string().len();
76-
77-
palette.cursor.x = tab.get_active_buffer_visible_x(tab.get_active_buffer().cursor.x);
78-
palette.cursor.x += number_column_width as u16 + 2;
79-
palette.cursor.y = tab.get_active_buffer_visible_y(tab.get_active_buffer().cursor.y);
73+
let mut palette = Palette::new(editor.area.height, editor.area.width);
8074

8175
let buffer = tab.get_active_buffer();
8276

77+
palette.cursor.x = buffer.text_area.x + (buffer.cursor.x - buffer.scroll.x) as u16;
78+
palette.cursor.y = buffer.text_area.y + (buffer.cursor.y - buffer.scroll.y) as u16;
79+
8380
match buffer.mode {
8481
BufferMode::Insert => palette.cursor_style = CursorStyle::BlinkingBar,
8582
BufferMode::Command => palette.cursor_style = CursorStyle::BlinkingBar,
@@ -91,14 +88,16 @@ impl Palette {
9188
None => palette.print(0, 0, &String::from("[No Name]")),
9289
}
9390

91+
let info_area_width = buffer.info_area.width as usize - 2;
92+
9493
for y in 0..buffer.area.height {
9594
let row_index = buffer.scroll.y + y as usize;
9695
match buffer.get_line_visible_text(row_index) {
9796
Some(text) => {
9897
palette.print(
9998
y + 1,
10099
0,
101-
&format!(" {:>2$} {}", row_index + 1, text, number_column_width),
100+
&format!(" {:>2$} {}", row_index + 1, text, info_area_width),
102101
);
103102
}
104103
None => palette.print(y + 1, 0, &format!("~")),

src/plugins/explorer/explorer_buffer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{fs, path::PathBuf};
22

3-
use crate::{buffer::Buffer, core::Size};
3+
use crate::{buffer::Buffer, core::Rectangle};
44

55
fn get_file_list(directory: &String) -> Vec<String> {
66
let paths = fs::read_dir(directory).unwrap();
@@ -28,7 +28,7 @@ fn get_file_list(directory: &String) -> Vec<String> {
2828
files
2929
}
3030

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

3434
let mut buffer = Buffer::new(area);

0 commit comments

Comments
 (0)