Skip to content

Commit 56ae551

Browse files
committed
fixed edit command
- edit command is supposed to swap current window buffer and it was creating a new window which would make the `ls` command not return the correct output - also deleted more old files
1 parent d68b379 commit 56ae551

16 files changed

+76
-618
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
*.txt
44
*.vim
55
todo.md
6+
error_revi

crates/revi-core/src/buffer.rs

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ impl Buffer {
4242
// self.rope.slice(start..end)
4343
}
4444

45+
pub fn len_lines(&self) -> usize {
46+
self.rope.len_lines()
47+
}
4548
pub fn line_len(&self, line_idx: usize) -> usize {
4649
self.rope.line(line_idx).len_chars()
4750
}

crates/revi-core/src/commands.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use revi_ui::tui::layout::{Pos, Size};
22

33
use crate::context::Context;
44
use crate::mode::Mode;
5-
use crate::{Buffer, Event, MessageBox};
5+
use crate::{panes::MessageBox, Buffer, Event};
66
use std::any::Any;
77
use std::cell::RefCell;
88
use std::fmt;
@@ -16,7 +16,8 @@ pub trait Command: fmt::Debug {
1616
}
1717

1818
macro_rules! build_command {
19-
($name:ident$(($($ty:ty $(,)?)*))?; $caller:expr) => {
19+
($([doc: $doc:expr])? $name:ident$(($($ty:ty $(,)?)*))?; $caller:expr) => {
20+
$(#[doc=$doc])?
2021
#[derive(Debug, PartialEq)]
2122
pub struct $name $(($(pub $ty, )*))?;
2223
impl Command for $name {
@@ -342,29 +343,23 @@ build_command!(
342343
);
343344

344345
build_command!(
346+
[doc: "loads a file from disk if it exsists and if not just create a empty file
347+
and place it in buffer list and put the current file buffer into the current window."]
345348
EditFile(String);
346349
|Self(command): &EditFile, ctx: Context| {
347350
let Some((_, name)) = command.split_once(' ') else {
348351
let msg = "edit command requires a file path";
349352
Message(msg.into(), command.into()).call(ctx);
350353
return;
351354
};
352-
let settings = ctx.settings.borrow();
353-
let line_numbers = settings.line_numbers;
354-
let id = ctx.panes.borrow().len();
355-
*ctx.focused_pane.borrow_mut() = id;
356355
let text = std::fs::read_to_string(name).unwrap_or("\n".into());
357356
let rope = ropey::Rope::from_str(&text);
358-
let buf = Rc::new(RefCell::new(Buffer::new(name, rope)));
359-
let pos = revi_ui::tui::layout::Pos::default();
360-
// HACK: sheppereds hook
361-
// size needs to be dynamicly dispatched
362-
let size = ctx.window_size();
363-
let window = crate::Window::new(pos, size, buf)
364-
.with_status_bar(true)
365-
.with_status_bar(line_numbers);
366-
let mut panes = ctx.panes.borrow_mut();
367-
panes.push(Rc::new(RefCell::new(window)));
357+
let new_buf = Rc::new(RefCell::new(Buffer::new(name, rope)));
358+
let mut buf_list = ctx.buffers.borrow_mut();
359+
buf_list.push(new_buf.clone());
360+
let pane = ctx.focused_pane();
361+
let mut pane = pane.borrow_mut();
362+
pane.set_buffer(new_buf);
368363
}
369364
);
370365

crates/revi-core/src/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::{api::Rhai, Settings};
22
use rhai::FnPtr;
33

4-
use super::{Buffer, CommandBar, Event, Mapper, Mode, Pane};
4+
use super::{
5+
panes::{CommandBar, Pane},
6+
Buffer, Event, Mapper, Mode,
7+
};
58

69
use revi_ui::tui::layout::Size;
710

crates/revi-core/src/lib.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
pub mod api;
22
pub mod buffer;
3-
mod command_bar;
43
pub mod commands;
54
mod context;
65
pub mod event;
76
pub mod map_keys;
8-
mod message_box;
97
pub mod mode;
10-
mod pane;
8+
pub mod panes;
119
mod parse_keys;
1210
mod settings;
13-
mod window;
1411

1512
pub use buffer::Buffer;
16-
pub use command_bar::CommandBar;
1713
pub use context::{Context, ContextBuilder};
1814
pub use event::Event;
1915
pub use map_keys::Mapper;
20-
pub use message_box::MessageBox;
2116
pub use mode::Mode;
22-
pub use pane::Pane;
2317
pub use parse_keys::KeyParser;
2418
pub use settings::Settings;
25-
pub use window::Window;

crates/revi-core/src/mode.rs

-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ pub enum Mode {
66
Command,
77
Insert,
88
}
9-
// impl Mode {
10-
// #[must_use]
11-
// pub fn shape(self) -> revi_ui::CursorShape {
12-
// match self {
13-
// Self::Normal | Self::Command => Block,
14-
// Self::Insert => Line,
15-
// }
16-
// }
17-
// }
189

1910
impl fmt::Display for Mode {
2011
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

crates/revi-core/src/command_bar.rs crates/revi-core/src/panes/command_bar.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use revi_ui::tui::{
33
layout::{Pos, Rect, Size, Stack},
44
text::Text,
55
};
6+
use std::{cell::RefCell, rc::Rc};
67

7-
use crate::{
8-
pane::{BufferBounds, BufferMut, Cursor, CursorMovement, CursorPos, PaneBounds, Scrollable},
9-
Mode, Pane,
8+
use super::{
9+
BufferBounds, BufferMut, Cursor, CursorMovement, CursorPos, Pane, PaneBounds, Scrollable,
1010
};
11+
use crate::{Buffer, Mode};
1112

1213
#[derive(Debug, Clone, Default)]
1314
pub struct CommandBar {
@@ -96,6 +97,9 @@ impl BufferBounds for CommandBar {
9697
}
9798

9899
impl BufferMut for CommandBar {
100+
fn set_buffer(&mut self, _buf: Rc<RefCell<Buffer>>) {
101+
todo!("set buffer for command bar")
102+
}
99103
fn insert_char(&mut self, c: char) {
100104
let idx = self.cursor.pos.x as usize;
101105
if idx < self.content.len() {

crates/revi-core/src/message_box.rs crates/revi-core/src/panes/message_box.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use revi_ui::{
99
Attribute,
1010
};
1111

12-
use crate::{
13-
pane::{BufferBounds, BufferMut, Cursor, CursorMovement, CursorPos, PaneBounds, Scrollable},
14-
Buffer, Mode, Pane,
12+
use super::{
13+
BufferBounds, BufferMut, Cursor, CursorMovement, CursorPos, Pane, PaneBounds, Scrollable,
1514
};
15+
use crate::{Buffer, Mode};
1616

1717
#[derive(Debug)]
1818
pub struct MessageBox {
@@ -171,6 +171,9 @@ impl BufferBounds for MessageBox {
171171
}
172172

173173
impl BufferMut for MessageBox {
174+
fn set_buffer(&mut self, buf: Rc<RefCell<Buffer>>) {
175+
self.buffer = buf;
176+
}
174177
fn insert_char(&mut self, _: char) {}
175178
fn clear_buffer(&mut self) {}
176179
fn get_buffer_contents(&self) -> String {

crates/revi-core/src/panes/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod command_bar;
2+
mod message_box;
3+
mod pane;
4+
mod window;
5+
6+
pub use command_bar::CommandBar;
7+
pub use message_box::MessageBox;
8+
pub use pane::{
9+
BufferBounds, BufferMut, Cursor, CursorMovement, CursorPos, Pane, PaneBounds, Scrollable,
10+
};
11+
pub use window::Window;

crates/revi-core/src/pane.rs crates/revi-core/src/panes/pane.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Debug;
1+
use std::{cell::RefCell, fmt::Debug, rc::Rc};
22

33
use revi_ui::{
44
tui::{
@@ -8,6 +8,7 @@ use revi_ui::{
88
Keys,
99
};
1010

11+
use crate::Buffer;
1112
use crate::Mode;
1213

1314
#[derive(Debug, Clone, Copy, Default)]
@@ -139,6 +140,7 @@ pub trait Scrollable: BufferBounds + CursorPos {
139140
}
140141

141142
pub trait BufferMut {
143+
fn set_buffer(&mut self, buf: Rc<RefCell<Buffer>>);
142144
fn get_buffer_contents(&self) -> String;
143145
fn insert_char(&mut self, c: char);
144146
fn clear_buffer(&mut self);

crates/revi-core/src/window.rs crates/revi-core/src/panes/window.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use revi_ui::{
99
Attribute, Color,
1010
};
1111

12-
use crate::{
13-
pane::{BufferBounds, BufferMut, Cursor, CursorMovement, CursorPos, PaneBounds, Scrollable},
14-
Buffer, Mode, Pane,
12+
use super::{
13+
BufferBounds, BufferMut, Cursor, CursorMovement, CursorPos, Pane, PaneBounds, Scrollable,
1514
};
15+
use crate::{Buffer, Mode};
1616

1717
#[derive(Debug)]
1818
pub struct Window {
@@ -93,9 +93,12 @@ impl Window {
9393
.on_screen(top, bottom)
9494
.iter()
9595
.map(ToString::to_string)
96+
.chain(std::iter::repeat("\n".into()))
97+
.take(height as usize)
9698
.collect::<String>();
9799
Text::new(&contents)
98100
.max_width(width)
101+
.max_height(height)
99102
.with_comment("text file")
100103
}
101104

@@ -132,13 +135,15 @@ impl Window {
132135
let height = height - (self.has_status_bar as u16);
133136
let start = self.cursor.scroll.y;
134137
let end = height + self.cursor.scroll.y;
135-
Text::new(
136-
&(start..=end)
137-
.map(|n| format!(" {} \n", n))
138-
.collect::<String>(),
139-
)
140-
.max_width(4)
141-
.with_comment("numbers")
138+
let content_rows = (self.buffer.borrow().len_lines() - 2) as u16;
139+
let text = &(start..=end.min(content_rows))
140+
.map(|n| format!(" {} \n", n))
141+
.chain(std::iter::repeat("~\n".into()))
142+
.take(end as usize)
143+
.collect::<String>();
144+
Text::new(text)
145+
.max_width(Self::NUMBER_LINE_WIDTH)
146+
.with_comment("numbers")
142147
}
143148
}
144149

@@ -235,6 +240,9 @@ impl BufferBounds for Window {
235240
}
236241

237242
impl BufferMut for Window {
243+
fn set_buffer(&mut self, buf: Rc<RefCell<Buffer>>) {
244+
self.buffer = buf;
245+
}
238246
fn insert_char(&mut self, ch: char) {
239247
let col = (self.cursor.pos.x as usize)
240248
- (self.has_line_numbers as u16 * Self::NUMBER_LINE_WIDTH) as usize;

0 commit comments

Comments
 (0)