Skip to content

Commit

Permalink
Add a file explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
cestef committed Mar 18, 2024
1 parent e439307 commit f5c90cc
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/papier/.config/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[keybindings.normal]
"ctrl-l" = { action = "Custom", payload = "NextBuffer" }
"ctrl-h" = { action = "Custom", payload = "PreviousBuffer" }
"ctrl-e" = { action = "Custom", payload = "ToggleExplorer" }
2 changes: 2 additions & 0 deletions crates/papier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ toml = "0.8.10"
tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
ratatui-explorer = "0.1.1"
tui-term = "0.1.8"
50 changes: 47 additions & 3 deletions crates/papier/src/components/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use crate::{
PapierAction,
};
use color_eyre::eyre::{eyre, Result};
use crossterm::event::{KeyCode, KeyEvent};
use config::File;
use crossterm::event::{Event, KeyCode, KeyEvent};
use edtui::{
actions::Execute, state::command::Command, view::EditorMessage, EditorMode, EditorState, EditorTheme, EditorView,
Index2, Input, Lines, StatusLine,
};
use log::debug;
use ratatui::{prelude::*, style::palette::tailwind::PURPLE, widgets::*};
use ratatui_explorer::{FileExplorer, Input as ExplorerInput, Theme as FileTheme};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
Expand Down Expand Up @@ -81,6 +83,12 @@ pub struct Buffer {
state: EditorState,
input: Input<PapierAction>,
message: Option<String>,
explorer: FileExplorer,
explorer_state: FileExplorerState,
}

struct FileExplorerState {
open: bool,
}

impl Buffer {
Expand Down Expand Up @@ -170,6 +178,8 @@ Don't hesitate to open issues or submit pull requests to contribute!
input,
message: None,
name: name.or_else(|| path.map(|p| p.file_name().unwrap().to_string_lossy().to_string())),
explorer: FileExplorer::with_theme(FileTheme::default().add_default_title())?,
explorer_state: FileExplorerState { open: false },
})
}

Expand Down Expand Up @@ -225,6 +235,26 @@ impl Component for Editor {
let current_buffer = self.current_buffer().unwrap();
let input = &mut current_buffer.input;
let state = &mut current_buffer.state;
let explorer = &mut current_buffer.explorer;
let explorer_state = &mut current_buffer.explorer_state;

if explorer_state.open {
if key.code == KeyCode::Esc {
explorer_state.open = false;
return Ok(None);
}
explorer.handle(&Event::Key(key))?;

if !explorer.current().is_dir() && (key.code == KeyCode::Enter || key.code == KeyCode::Char('l')) {
let path = explorer.current().path().to_path_buf();
let buffer = Buffer::new(Some(path), self.config.keybindings.clone(), None, None)?;
self.buffers.push(buffer);
self.current_buffer = Some(self.buffers.len() - 1);
return Ok(None);
}
return Ok(None);
}

let maybe_custom = input.on_key(key, state);

if let Some(custom) = maybe_custom {
Expand Down Expand Up @@ -278,19 +308,33 @@ impl Component for Editor {
self.current_buffer = Some(self.buffers.len() - 1);
},
PapierAction::QuitAll => return Ok(Some(Action::Quit)),
PapierAction::ToggleExplorer => {
current_buffer.explorer_state.open = !current_buffer.explorer_state.open;
},
}
};

Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
let [top, bottom] = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(area);

let buffer_index = self.current_buffer.unwrap();
let buffer_count = self.buffers.len();
let current_buffer = self.current_buffer().unwrap();
let state = &mut current_buffer.state;

let area = area.inner(&Margin { horizontal: 1, vertical: 1 });
let [explorer, editor] = Layout::horizontal([
Constraint::Length(if current_buffer.explorer_state.open { 20 } else { 0 }),
Constraint::Min(0),
])
.areas(area);
let [top, bottom] = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(editor);

if current_buffer.explorer_state.open {
f.render_widget(&current_buffer.explorer.widget(), explorer);
}

let theme = EditorTheme::default()
.base(EditorTheme::default().base.bold().bg(Color::Reset))
.selection_style(Style::default().bg(Color::LightMagenta).fg(Color::Reset))
Expand Down
1 change: 1 addition & 0 deletions crates/papier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum PapierAction {
PreviousBuffer,
NextBuffer,
Open(String),
ToggleExplorer,
}

impl Execute for PapierAction {
Expand Down

0 comments on commit f5c90cc

Please sign in to comment.