-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom_commands + fallback for the older configurations (#446)
* custom_commands + fallback for the older configurations 1. Added custom_commands 2. Implemented custom_search 3. Implemented custom_serach_interactive 4. Added fallback for the command in the keymaps * Docs + missing file * Added two more joshuto scripts --------- Co-authored-by: Tomasz Durda <[email protected]> Co-authored-by: Jeff Zhao <[email protected]>
- Loading branch information
1 parent
ee50d17
commit 960decf
Showing
22 changed files
with
292 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
|
||
CURRENT_PATH="$PWD" | ||
GIT_PATH="$(git rev-parse --show-toplevel)" | ||
|
||
cd $GIT_PATH | ||
GIT_PATH="$PWD" | ||
|
||
IFS=$'\n' FILES=($(git ls-files . --ignored --exclude-standard --others)) | ||
|
||
|
||
cnt=${#FILES[@]} | ||
for ((i=0;i<cnt;i++)); do | ||
FILES[i]=$(realpath --relative-to "$CURRENT_PATH" "${GIT_PATH}/${FILES[i]}") | ||
done | ||
|
||
cd $CURRENT_PATH | ||
|
||
echo "${FILES[*]}" \ | ||
| fzf --ansi --preview 'bat -n $(echo {})' \ | ||
| cut -d ":" -f1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/bash | ||
|
||
CURRENT_PATH="$PWD" | ||
GIT_PATH="$(git rev-parse --show-toplevel)" | ||
|
||
cd $GIT_PATH | ||
GIT_PATH="$PWD" | ||
|
||
IFS=$'\n' FILES=($(git ls-files . --exclude-standard --others)) | ||
|
||
cnt=${#FILES[@]} | ||
for ((i=0;i<cnt;i++)); do | ||
FILES[i]=$(realpath --relative-to "$CURRENT_PATH" "${GIT_PATH}/${FILES[i]}") | ||
done | ||
|
||
cd $CURRENT_PATH | ||
|
||
echo "${FILES[*]}" \ | ||
| fzf --ansi --preview 'bat -n $(echo {})' \ | ||
| cut -d ":" -f1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/bash | ||
|
||
CURRENT_PATH="$PWD" | ||
GIT_PATH="$(git rev-parse --show-toplevel)" | ||
|
||
cd $GIT_PATH | ||
GIT_PATH="$PWD" | ||
|
||
IFS=$'\n' FILES=($(git diff --name-only --diff-filter=U --relative)) | ||
|
||
cnt=${#FILES[@]} | ||
for ((i=0;i<cnt;i++)); do | ||
FILES[i]=$(realpath --relative-to "$CURRENT_PATH" "${GIT_PATH}/${FILES[i]}") | ||
done | ||
|
||
cd $CURRENT_PATH | ||
|
||
echo "${FILES[*]}" \ | ||
| fzf --ansi --preview 'bat -n $(echo {})' \ | ||
| cut -d ":" -f1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
echo "$(git rev-parse --show-toplevel)/.git" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
rg -l "$@" | tail -n 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
rg -n -H --color=never "$@" \ | ||
| fzf --ansi --preview 'bat -n $(echo {} | cut -d ":" -f1) --line-range="$(echo {} | cut -d ":" -f2):"' \ | ||
| cut -d ":" -f1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use super::change_directory::change_directory; | ||
use super::sub_process::current_filenames; | ||
use crate::commands::cursor_move; | ||
use crate::context::AppContext; | ||
use crate::error::{AppError, AppErrorKind, AppResult}; | ||
use crate::ui::AppBackend; | ||
use shell_words::split; | ||
use std::process::{Command, Stdio}; | ||
|
||
pub fn custom_search( | ||
context: &mut AppContext, | ||
backend: &mut AppBackend, | ||
words: &[String], | ||
interactive: bool, | ||
) -> AppResult { | ||
let custom_command = context | ||
.config_ref() | ||
.custom_commands | ||
.as_slice() | ||
.iter() | ||
.find(|x| x.name == words[0]) | ||
.ok_or(AppError::new( | ||
AppErrorKind::InvalidParameters, | ||
"No custom command with given name".into(), | ||
))? | ||
.command | ||
.clone(); | ||
|
||
let current_filenames = current_filenames(context); | ||
|
||
let text = custom_command.replace("%s", ¤t_filenames.join(" ")); | ||
let text = text.replace( | ||
"%text", | ||
&words | ||
.iter() | ||
.skip(1) | ||
.cloned() | ||
.collect::<Vec<String>>() | ||
.join(" "), | ||
); | ||
let mut command_with_args: Vec<String> = split(&text).map_err(|_| { | ||
AppError::new( | ||
AppErrorKind::InvalidParameters, | ||
"Command cannot be splitted".into(), | ||
) | ||
})?; | ||
|
||
let mut cmd = Command::new(command_with_args.remove(0)); | ||
command_with_args.into_iter().for_each(|x| { | ||
cmd.arg(x); | ||
}); | ||
|
||
let cmd_result = if interactive { | ||
backend.terminal_drop(); | ||
let cmd_result = cmd | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.spawn()? | ||
.wait_with_output()?; | ||
backend.terminal_restore()?; | ||
cmd_result | ||
} else { | ||
cmd.output()? | ||
}; | ||
|
||
if cmd_result.status.success() { | ||
let returned_text = std::str::from_utf8(&cmd_result.stdout) | ||
.map_err(|_| { | ||
AppError::new( | ||
AppErrorKind::ParseError, | ||
"Could not get command result as utf8".into(), | ||
) | ||
})? | ||
.trim_end(); | ||
|
||
let path = std::path::Path::new(returned_text); | ||
change_directory( | ||
context, | ||
path.parent().ok_or(AppError::new( | ||
AppErrorKind::ParseError, | ||
"Could not get parent directory".into(), | ||
))?, | ||
)?; | ||
|
||
if let Some(current_dir_items) = context.tab_context_ref().curr_tab_ref().curr_list_ref() { | ||
let position = current_dir_items | ||
.iter() | ||
.enumerate() | ||
.find(|x| x.1.file_name() == path.file_name().unwrap_or_default()) | ||
.map(|x| x.0) | ||
.unwrap_or_default(); | ||
|
||
cursor_move::cursor_move(context, position); | ||
} | ||
|
||
Ok(()) | ||
} else { | ||
let returned_text = std::str::from_utf8(&cmd_result.stderr).map_err(|_| { | ||
AppError::new( | ||
AppErrorKind::ParseError, | ||
"Could not get command result as utf8".into(), | ||
) | ||
})?; | ||
|
||
Err(AppError::new( | ||
AppErrorKind::ParseError, | ||
format!("Command failed: {}", returned_text), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.