Skip to content

Commit

Permalink
feat: wezterm cwd compatibility (#439)
Browse files Browse the repository at this point in the history
* add: new cwd utility

* Use cwd::set_current_dir to update terminal with current working directory

---------

Co-authored-by: kennycallado <kennycallado@hotmail.com>
  • Loading branch information
kennycallado and kennycallado authored Oct 9, 2023
1 parent cab8214 commit 7aec4b6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/commands/change_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::commands::reload;
use crate::context::AppContext;
use crate::error::AppResult;
use crate::history::DirectoryHistory;
use crate::util::cwd;

// ChangeDirectory command
pub fn cd(path: &path::Path, context: &mut AppContext) -> std::io::Result<()> {
std::env::set_current_dir(path)?;
cwd::set_current_dir(path)?;
context.tab_context_mut().curr_tab_mut().set_cwd(path);
Ok(())
}
Expand Down Expand Up @@ -51,7 +52,7 @@ pub fn parent_directory(context: &mut AppContext) -> AppResult {
.parent()
.map(|p| p.to_path_buf())
{
std::env::set_current_dir(&parent)?;
cwd::set_current_dir(&parent)?;
context
.tab_context_mut()
.curr_tab_mut()
Expand All @@ -65,7 +66,7 @@ pub fn parent_directory(context: &mut AppContext) -> AppResult {
pub fn previous_directory(context: &mut AppContext) -> AppResult {
if let Some(path) = context.tab_context_ref().curr_tab_ref().previous_dir() {
let path = path.to_path_buf();
std::env::set_current_dir(&path)?;
cwd::set_current_dir(&path)?;
context
.tab_context_mut()
.curr_tab_mut()
Expand Down
4 changes: 2 additions & 2 deletions src/commands/tab_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};
use crate::history::DirectoryHistory;
use crate::tab::{JoshutoTab, TabHomePage};
use crate::util::unix;
use crate::util::{cwd, unix};

use crate::HOME_DIR;

Expand All @@ -16,7 +16,7 @@ use super::quit::{quit_with_action, QuitAction};
fn _tab_switch(new_index: usize, context: &mut AppContext) -> std::io::Result<()> {
context.tab_context_mut().index = new_index;
let cwd = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();
std::env::set_current_dir(cwd.as_path())?;
cwd::set_current_dir(cwd.as_path())?;

let entry_path = match context
.tab_context_ref()
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use config::clean::bookmarks::Bookmarks;
use config::clean::mimetype::AppProgramRegistry;
use config::clean::theme::AppTheme;
use config::TomlConfigFile;
use util::cwd;

use crate::commands::quit::QuitAction;

Expand Down Expand Up @@ -143,7 +144,7 @@ fn run_main(args: Args) -> Result<i32, AppError> {
}

if let Some(path) = args.rest.first() {
if let Err(err) = std::env::set_current_dir(path) {
if let Err(err) = cwd::set_current_dir(path) {
eprintln!("{err}");
process::exit(1);
}
Expand Down
4 changes: 4 additions & 0 deletions src/tab/tab_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::context::UiContext;
use crate::fs::JoshutoDirList;
use crate::history::{DirectoryHistory, JoshutoHistory};
use crate::preview::preview_dir::PreviewDirState;
// use crate::HOSTNAME;

type HistoryMetadata = HashMap<path::PathBuf, PreviewDirState>;

Expand Down Expand Up @@ -54,6 +55,9 @@ impl JoshutoTab {
pub fn set_cwd(&mut self, cwd: &path::Path) {
self._previous_dir = Some(self._cwd.to_path_buf());
self._cwd = cwd.to_path_buf();

// OSC 7: Escape sequence to set the working directory
// print!("\x1b]7;file://{}{}\x1b\\", HOSTNAME.as_str(), cwd.display());
}

pub fn previous_dir(&self) -> Option<&path::Path> {
Expand Down
15 changes: 15 additions & 0 deletions src/util/cwd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::path;

use crate::HOSTNAME;

pub fn set_current_dir(path: &path::Path) -> std::io::Result<()> {
std::env::set_current_dir(path)?;
// OSC 7:
// Escape sequences to advise the terminal of the working directory
print!(
"\x1b]7;file://{}{}\x1b\\",
HOSTNAME.as_str(),
path.display()
);
Ok(())
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cwd;
pub mod format;
pub mod fs;
pub mod keyparse;
Expand Down

0 comments on commit 7aec4b6

Please sign in to comment.