Skip to content

Commit

Permalink
add quit_to_cwd command
Browse files Browse the repository at this point in the history
 - this command lets users exit to the current directory more
 easily and more ergonomically
  • Loading branch information
kamiyaa committed Aug 18, 2021
1 parent 45e7a9f commit f8851c7
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/commands/delete_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn delete_selected_files(
context: &mut AppContext,
backend: &mut TuiBackend,
) -> std::io::Result<()> {
let res = delete_files(context, backend)?;
let _ = delete_files(context, backend)?;

let options = context.config_ref().display_options_ref().clone();
let curr_path = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();
Expand Down
5 changes: 5 additions & 0 deletions src/commands/key_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub enum KeyCommand {
ParentDirectory,

Quit,
QuitToCurrentDirectory,
ForceQuit,
ReloadDirList,
RenameFile(path::PathBuf),
Expand Down Expand Up @@ -108,6 +109,7 @@ impl KeyCommand {
Self::ParentDirectory => "cd ..",

Self::Quit => "quit",
Self::QuitToCurrentDirectory => "quit_to_cwd",
Self::ForceQuit => "force_quit",
Self::ReloadDirList => "reload_dirlist",
Self::RenameFile(_) => "rename",
Expand Down Expand Up @@ -260,6 +262,7 @@ impl std::str::FromStr for KeyCommand {
Ok(Self::PasteFiles(options))
}
"quit" => Ok(Self::Quit),
"quit_to_cwd" => Ok(Self::QuitToCurrentDirectory),
"reload_dirlist" => Ok(Self::ReloadDirList),
"rename" => match arg {
"" => Err(JoshutoError::new(
Expand Down Expand Up @@ -398,7 +401,9 @@ impl AppExecute for KeyCommand {
Self::ParentDirectory => parent_directory::parent_directory(context),

Self::Quit => quit::quit(context),
Self::QuitToCurrentDirectory => quit::quit_to_current_directory(context),
Self::ForceQuit => quit::force_quit(context),

Self::ReloadDirList => reload::reload_dirlist(context),
Self::RenameFile(p) => rename_file::rename_file(context, p.as_path()),
Self::RenameFileAppend => rename_file::rename_file_append(context, backend),
Expand Down
11 changes: 8 additions & 3 deletions src/commands/quit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;

use crate::context::AppContext;
use crate::context::{AppContext, QuitType};
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};

pub fn quit(context: &mut AppContext) -> JoshutoResult<()> {
Expand All @@ -11,12 +11,17 @@ pub fn quit(context: &mut AppContext) -> JoshutoResult<()> {
String::from("operations running in background, use force_quit to quit"),
))
} else {
context.exit = true;
context.quit = QuitType::Normal;
Ok(())
}
}

pub fn force_quit(context: &mut AppContext) -> JoshutoResult<()> {
context.exit = true;
context.quit = QuitType::Force;
Ok(())
}

pub fn quit_to_current_directory(context: &mut AppContext) -> JoshutoResult<()> {
context.quit = QuitType::ToCurrentDirectory;
Ok(())
}
12 changes: 10 additions & 2 deletions src/context/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ use crate::context::{LocalStateContext, PreviewContext, TabContext, WorkerContex
use crate::event::{AppEvent, Events};
use crate::util::search::SearchPattern;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum QuitType {
DoNot,
Normal,
Force,
ToCurrentDirectory,
}

pub struct AppContext {
pub exit: bool,
pub quit: QuitType,
// event loop querying
pub events: Events,
// app config
Expand All @@ -31,7 +39,7 @@ impl AppContext {
let events = Events::new();
let event_tx = events.event_tx.clone();
Self {
exit: false,
quit: QuitType::DoNot,
events,
tab_context: TabContext::new(),
local_state: None,
Expand Down
2 changes: 1 addition & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod preview_context;
mod tab_context;
mod worker_context;

pub use self::app_context::AppContext;
pub use self::app_context::{AppContext, QuitType};
pub use self::local_state::LocalStateContext;
pub use self::preview_context::PreviewContext;
pub use self::tab_context::TabContext;
Expand Down
2 changes: 1 addition & 1 deletion src/fs/dirlist.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::slice::{Iter, IterMut};
use std::{fs, path};

use crate::fs::{FileType, JoshutoDirEntry, JoshutoMetadata};
use crate::fs::{JoshutoDirEntry, JoshutoMetadata};
use crate::util::display::DisplayOption;

#[derive(Clone, Debug)]
Expand Down
34 changes: 21 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use structopt::StructOpt;
use crate::config::{
AppConfig, AppKeyMapping, AppMimetypeRegistry, AppTheme, ConfigStructure, JoshutoPreview,
};
use crate::context::AppContext;
use crate::context::{AppContext, QuitType};
use crate::error::JoshutoError;
use crate::run::run;

Expand Down Expand Up @@ -98,24 +98,32 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
let config = AppConfig::get_config(CONFIG_FILE);
let keymap = AppKeyMapping::get_config(KEYMAP_FILE);

let mut context = AppContext::new(config);
{
let mut context = AppContext::new(config);
let mut backend: ui::TuiBackend = ui::TuiBackend::new()?;
run(&mut backend, &mut context, keymap)?;
}

if let Some(p) = args.last_dir {
let curr_path = std::env::current_dir()?;
let mut file = File::create(p)?;
file.write_all(
curr_path
.into_os_string()
.as_os_str()
.to_string_lossy()
.as_bytes(),
)?;
file.write_all("\n".as_bytes())?;
match context.quit {
QuitType::ToCurrentDirectory => {
if let Some(p) = args.last_dir {
let curr_path = std::env::current_dir()?;
let mut file = File::create(p)?;
file.write_all(
curr_path
.into_os_string()
.as_os_str()
.to_string_lossy()
.as_bytes(),
)?;
file.write_all("\n".as_bytes())?;
}
},
QuitType::Force => {},
_ => {},

}

Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use termion::event::Event;

use crate::commands::{AppExecute, CommandKeybind, KeyCommand};
use crate::config::AppKeyMapping;
use crate::context::AppContext;
use crate::context::{AppContext, QuitType};
use crate::event::AppEvent;
use crate::preview::preview_default;
use crate::tab::JoshutoTab;
Expand All @@ -26,7 +26,7 @@ pub fn run(
preview_default::load_preview(context, backend);
}

while !context.exit {
while context.quit == QuitType::DoNot {
backend.render(TuiView::new(&context));

if !context.worker_context_ref().is_busy() && !context.worker_context_ref().is_empty() {
Expand Down
4 changes: 4 additions & 0 deletions src/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl JoshutoTab {
self._cwd = cwd.to_path_buf();
}

pub fn history_ref(&self) -> &JoshutoHistory {
&self.history
}

pub fn history_mut(&mut self) -> &mut JoshutoHistory {
&mut self.history
}
Expand Down
3 changes: 2 additions & 1 deletion src/ui/views/tui_folder_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<'a> Widget for TuiFolderView<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let preview_context = self.context.preview_context_ref();
let curr_tab = self.context.tab_context_ref().curr_tab_ref();
let history = curr_tab.history_ref();

let curr_list = curr_tab.curr_list_ref();
let parent_list = curr_tab.parent_list_ref();
Expand Down Expand Up @@ -127,7 +128,7 @@ impl<'a> Widget for TuiFolderView<'a> {

// render current view
if let Some(list) = curr_list.as_ref() {
TuiDirListDetailed::new(&list).render(layout_rect[1], buf);
TuiDirListDetailed::new(&list, history).render(layout_rect[1], buf);
let rect = Rect {
x: 0,
y: area.height - 1,
Expand Down
6 changes: 4 additions & 2 deletions src/ui/widgets/tui_dirlist_detailed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use tui::style::{Color, Modifier, Style};
use tui::widgets::Widget;

use crate::fs::{FileType, JoshutoDirEntry, JoshutoDirList, LinkType};
use crate::history::JoshutoHistory;
use crate::util::format;
use crate::util::string::UnicodeTruncate;
use crate::util::style;
Expand All @@ -15,11 +16,12 @@ const ELLIPSIS: &str = "…";

pub struct TuiDirListDetailed<'a> {
dirlist: &'a JoshutoDirList,
history: &'a JoshutoHistory,
}

impl<'a> TuiDirListDetailed<'a> {
pub fn new(dirlist: &'a JoshutoDirList) -> Self {
Self { dirlist }
pub fn new(dirlist: &'a JoshutoDirList, history: &'a JoshutoHistory) -> Self {
Self { dirlist, history }
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/util/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ pub fn process_mouse(event: MouseEvent, context: &mut AppContext, backend: &mut
let f_size = backend.terminal.as_ref().unwrap().size().unwrap();

let constraints: &[Constraint; 3] = &context.config_ref().display_options_ref().default_layout;
let vertical_margin = if context.config_ref().display_options_ref().show_borders() {
2
} else {
1
};

let layout_rect = Layout::default()
.direction(Direction::Horizontal)
.vertical_margin(
if context.config_ref().display_options_ref().show_borders() {
2
} else {
1
},
)
.vertical_margin(vertical_margin)
.constraints(constraints.as_ref())
.split(f_size);

Expand Down

0 comments on commit f8851c7

Please sign in to comment.