Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gettin started on working on backend #2

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
71 changes: 71 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ansi-to-tui = { version = "^3", optional = true }
alphanumeric-sort = "^1"
chrono = "^0"
colors-transform = "^0"
crossterm = {version = "0.26" }
dirs-next = "^2"
filetime = "^0"
globset = "^0"
Expand All @@ -33,7 +34,7 @@ structopt = "^0"
termion = "^1"
toml = "^0"
trash = { version = "^2", optional = true }
tui = { version = "0.20", default-features = false, features = ["termion"], package = "ratatui" }
tui = { version = "0.20", default-features = false, features = ["termion", "crossterm"], package = "ratatui" }
unicode-segmentation = "^1"
unicode-width = "^0"
users = "^0"
Expand All @@ -50,9 +51,9 @@ features = [
]

[features]
default = [ "devicons", "recycle_bin", "syntax_highlight" ]
devicons = [ "phf" ]
file_mimetype = []
mouse = []
recycle_bin = [ "trash" ]
syntax_highlight = [ "ansi-to-tui" ]
default = [ "devicons", "recycle_bin", "syntax_highlight" ]
16 changes: 9 additions & 7 deletions src/commands/bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ use std::fs::File;
use std::io::Write;
use std::path;

use termion::event::Event;
use tui::layout::Rect;
use tui::widgets::Clear;

use crate::config::{search_directories, BookmarkRaw, BookmarksRaw};
use crate::context::AppContext;
use crate::error::JoshutoResult;
use crate::event::{process_event, AppEvent};
use crate::traits::ToString;
use crate::ui::views::TuiView;
use crate::ui::widgets::TuiMenu;
use crate::ui::AppBackend;
use crate::util::unix;

use crate::{BOOKMARKS_FILE, BOOKMARKS_T, CONFIG_HIERARCHY};
use crate::event::joshuto_event::JoshutoEvent;

use super::change_directory::change_directory;

Expand All @@ -29,7 +28,7 @@ fn find_bookmark_file() -> Option<path::PathBuf> {
None
}

pub fn add_bookmark(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult {
pub fn add_bookmark<T: AppBackend>(context: &mut AppContext, backend: &mut T) -> JoshutoResult {
let cwd = std::env::current_dir()?;

let bookmark_path = match search_directories(BOOKMARKS_FILE, &CONFIG_HIERARCHY) {
Expand Down Expand Up @@ -67,9 +66,9 @@ pub fn add_bookmark(context: &mut AppContext, backend: &mut AppBackend) -> Joshu
Ok(())
}

pub fn change_directory_bookmark(
pub fn change_directory_bookmark<T: AppBackend>(
context: &mut AppContext,
backend: &mut AppBackend,
backend: &mut T,
) -> JoshutoResult {
let key = poll_for_bookmark_key(context, backend);

Expand All @@ -84,7 +83,10 @@ pub fn change_directory_bookmark(
Ok(())
}

fn poll_for_bookmark_key(context: &mut AppContext, backend: &mut AppBackend) -> Option<Event> {
fn poll_for_bookmark_key<T: AppBackend>(
context: &mut AppContext,
backend: &mut T,
) -> Option<JoshutoEvent> {
context.flush_event();

let mut bookmarks: Vec<String> = BOOKMARKS_T
Expand Down Expand Up @@ -130,7 +132,7 @@ fn poll_for_bookmark_key(context: &mut AppContext, backend: &mut AppBackend) ->

if let Ok(event) = context.poll_event() {
match event {
AppEvent::Termion(key) => return Some(key),
AppEvent::Backend(key) => return Some(JoshutoEvent::from(key)),
event => process_event::process_noninteractive(event, context),
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/bulk_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const ENV_EDITOR: &str = "EDITOR";
const FILE_PREFIX: &str = "joshuto-";
const RAND_STR_LEN: usize = 10;

/// TODO: i REALLY dont like this _ prefix. its not idiomatic with the clippy lint
pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult {
let tmp_directory = env::var(ENV_TMP_DIR).unwrap_or_else(|_| "/tmp".to_string());

Expand Down Expand Up @@ -122,7 +123,7 @@ pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult {
Ok(())
}

pub fn bulk_rename(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult {
pub fn bulk_rename<T: AppBackend>(context: &mut AppContext, backend: &mut T) -> JoshutoResult {
context.remove_external_preview();
backend.terminal_drop();
let res = _bulk_rename(context);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::key_command::{AppExecute, Command};
use crate::ui::views::TuiTextField;
use crate::ui::AppBackend;

pub fn read_and_execute(
pub fn read_and_execute<T: AppBackend>(
context: &mut AppContext,
backend: &mut AppBackend,
backend: &mut T,
keymap_t: &AppKeyMapping,
prefix: &str,
suffix: &str,
Expand Down
18 changes: 9 additions & 9 deletions src/commands/cursor_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ pub fn end(context: &mut AppContext) -> JoshutoResult {
Ok(())
}

fn get_page_size(context: &AppContext, backend: &AppBackend) -> Option<usize> {
fn get_page_size<T: AppBackend>(context: &AppContext, backend: &T) -> Option<usize> {
let config = context.config_ref();
let rect = backend.terminal.as_ref().map(|t| t.size())?.ok()?;
let rect = backend.terminal().as_ref().map(|t| t.size())?.ok()?;

let rect_height = rect.height as usize;
if config.display_options_ref().show_borders() {
Expand All @@ -138,9 +138,9 @@ fn get_page_size(context: &AppContext, backend: &AppBackend) -> Option<usize> {
}
}

pub fn page_up(
pub fn page_up<T: AppBackend>(
context: &mut AppContext,
backend: &mut AppBackend,
backend: &mut T,
proportion: f64,
) -> JoshutoResult {
let page_size = get_page_size(context, backend).unwrap_or(10) as f64 * proportion;
Expand All @@ -158,9 +158,9 @@ pub fn page_up(
Ok(())
}

pub fn page_down(
pub fn page_down<T: AppBackend>(
context: &mut AppContext,
backend: &mut AppBackend,
backend: &mut T,
proportion: f64,
) -> JoshutoResult {
let page_size = get_page_size(context, backend).unwrap_or(10) as f64 * proportion;
Expand All @@ -178,7 +178,7 @@ pub fn page_down(
Ok(())
}

pub fn page_home(context: &mut AppContext, _: &mut AppBackend) -> JoshutoResult {
pub fn page_home<T: AppBackend>(context: &mut AppContext, _: &mut T) -> JoshutoResult {
let new_index = context
.tab_context_ref()
.curr_tab_ref()
Expand All @@ -190,7 +190,7 @@ pub fn page_home(context: &mut AppContext, _: &mut AppBackend) -> JoshutoResult
Ok(())
}

pub fn page_middle(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult {
pub fn page_middle<T: AppBackend>(context: &mut AppContext, backend: &mut T) -> JoshutoResult {
let movement = get_page_size(context, backend).unwrap_or(10) / 2;

let new_index = context
Expand All @@ -204,7 +204,7 @@ pub fn page_middle(context: &mut AppContext, backend: &mut AppBackend) -> Joshut
Ok(())
}

pub fn page_end(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult {
pub fn page_end<T: AppBackend>(context: &mut AppContext, backend: &mut T) -> JoshutoResult {
let movement = get_page_size(context, backend).unwrap_or(10) - 1;

let new_index = context
Expand Down
14 changes: 7 additions & 7 deletions src/commands/delete_files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path;
use std::sync::mpsc;

use termion::event::Key;
use crate::event::joshuto_event::JoshutoKey;

use crate::context::AppContext;
use crate::error::JoshutoResult;
Expand All @@ -10,9 +10,9 @@ use crate::io::{FileOperation, FileOperationOptions, IoWorkerThread};
use crate::ui::widgets::TuiPrompt;
use crate::ui::AppBackend;

fn delete_files(
fn delete_files<T: AppBackend>(
context: &mut AppContext,
backend: &mut AppBackend,
backend: &mut T,
background: bool,
permanently: bool,
) -> std::io::Result<()> {
Expand All @@ -37,15 +37,15 @@ fn delete_files(
};

match ch {
Key::Char('Y') | Key::Char('y') | Key::Char('\n') => {
JoshutoKey::Char('Y') | JoshutoKey::Char('y') | JoshutoKey::Char('\n') => {
let confirm_delete = if paths_len > 1 {
// prompt user again for deleting multiple files
let ch = {
let prompt_str = "Are you sure? (y/N)";
let mut prompt = TuiPrompt::new(prompt_str);
prompt.get_key(backend, context)
};
ch == Key::Char('y')
ch == JoshutoKey::Char('y')
} else {
true
};
Expand Down Expand Up @@ -77,9 +77,9 @@ fn delete_files(
}
}

pub fn delete_selected_files(
pub fn delete_selected_files<T: AppBackend>(
context: &mut AppContext,
backend: &mut AppBackend,
backend: &mut T,
background: bool,
permanently: bool,
) -> JoshutoResult {
Expand Down
14 changes: 7 additions & 7 deletions src/commands/numbered_command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use termion::event::{Event, Key};
use crate::event::joshuto_event::{JoshutoEvent, JoshutoKey};

use crate::commands::cursor_move;
use crate::config::AppKeyMapping;
Expand All @@ -10,9 +10,9 @@ use crate::key_command::{CommandKeybind, NumberedExecute};
use crate::ui::views::TuiView;
use crate::ui::AppBackend;

pub fn numbered_command(
pub fn numbered_command<T: AppBackend>(
context: &mut AppContext,
backend: &mut AppBackend,
backend: &mut T,
keymap: &AppKeyMapping,
first_char: char,
) -> JoshutoResult {
Expand Down Expand Up @@ -41,14 +41,14 @@ pub fn numbered_command(
};

match event {
AppEvent::Termion(event) => {
AppEvent::Backend(event) => {
match event {
Event::Key(Key::Esc) => return Ok(()),
Event::Key(Key::Char('g')) => {
JoshutoEvent::Key(JoshutoKey::Esc) => return Ok(()),
JoshutoEvent::Key(JoshutoKey::Char('g')) => {
cursor_move::cursor_move(context, num_prefix - 1);
return Ok(());
}
Event::Key(Key::Char(c)) if c.is_numeric() => {
JoshutoEvent::Key(JoshutoKey::Char(c)) if c.is_numeric() => {
prefix.push(c);
}
key => match keymap.default_view.get(&key) {
Expand Down
Loading