-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor icon painter and cmd (#386)
* Refactor icon painter in light_command * Refactor exec subcommand * Use IconPainter * Introduce ContentFiltering * Add --content-filtering * Update CHANGELOG.md * Allow :Clap files +name-only ~ * Add --icon-painter * Fix deprecated --enable-icon in sync_grep * Improve icon for truncated text in dyn filter * Imporve icon highlight for truncated grep line * . * .
- Loading branch information
1 parent
6f35788
commit 3913e1e
Showing
19 changed files
with
310 additions
and
239 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,52 +1,74 @@ | ||
use crate::light_command::{set_current_dir, LightCommand}; | ||
use anyhow::Result; | ||
use icon::IconPainter; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
use structopt::StructOpt; | ||
|
||
use anyhow::Result; | ||
/// Execute the shell command | ||
#[derive(StructOpt, Debug, Clone)] | ||
pub struct Exec { | ||
/// Specify the system command to run. | ||
#[structopt(index = 1, short, long)] | ||
cmd: String, | ||
|
||
use crate::light_command::{set_current_dir, LightCommand}; | ||
/// Specify the output file path when the output of command exceeds the threshold. | ||
#[structopt(long = "output")] | ||
output: Option<String>, | ||
|
||
/// Specify the threshold for writing the output of command to a tempfile. | ||
#[structopt(long = "output-threshold", default_value = "100000")] | ||
output_threshold: usize, | ||
|
||
/// Specify the working directory of CMD | ||
#[structopt(long = "cmd-dir", parse(from_os_str))] | ||
cmd_dir: Option<PathBuf>, | ||
} | ||
|
||
impl Exec { | ||
// This can work with the piped command, e.g., git ls-files | uniq. | ||
fn prepare_exec_cmd(&self) -> Command { | ||
let mut cmd = if cfg!(target_os = "windows") { | ||
let mut cmd = Command::new("cmd"); | ||
cmd.args(&["/C", &self.cmd]); | ||
cmd | ||
} else { | ||
let mut cmd = Command::new("bash"); | ||
cmd.arg("-c").arg(&self.cmd); | ||
cmd | ||
}; | ||
|
||
set_current_dir(&mut cmd, self.cmd_dir.clone()); | ||
|
||
// This can work with the piped command, e.g., git ls-files | uniq. | ||
fn prepare_exec_cmd(cmd_str: &str, cmd_dir: Option<PathBuf>) -> Command { | ||
let mut cmd = if cfg!(target_os = "windows") { | ||
let mut cmd = Command::new("cmd"); | ||
cmd.args(&["/C", cmd_str]); | ||
cmd | ||
} else { | ||
let mut cmd = Command::new("bash"); | ||
cmd.arg("-c").arg(cmd_str); | ||
cmd | ||
}; | ||
} | ||
|
||
set_current_dir(&mut cmd, cmd_dir); | ||
pub fn run( | ||
&self, | ||
number: Option<usize>, | ||
icon_painter: Option<IconPainter>, | ||
no_cache: bool, | ||
) -> Result<()> { | ||
let mut exec_cmd = self.prepare_exec_cmd(); | ||
|
||
cmd | ||
} | ||
let mut light_cmd = LightCommand::new( | ||
&mut exec_cmd, | ||
number, | ||
self.output.clone(), | ||
icon_painter, | ||
self.output_threshold, | ||
); | ||
|
||
pub fn run( | ||
cmd: String, | ||
output: Option<String>, | ||
output_threshold: usize, | ||
cmd_dir: Option<PathBuf>, | ||
number: Option<usize>, | ||
enable_icon: bool, | ||
no_cache: bool, | ||
) -> Result<()> { | ||
let mut exec_cmd = prepare_exec_cmd(&cmd, cmd_dir.clone()); | ||
|
||
let mut light_cmd = LightCommand::new( | ||
&mut exec_cmd, | ||
number, | ||
output, | ||
enable_icon, | ||
false, | ||
output_threshold, | ||
); | ||
|
||
let args = cmd.split_whitespace().map(Into::into).collect::<Vec<_>>(); | ||
|
||
if !no_cache && cmd_dir.is_some() { | ||
light_cmd.try_cache_or_execute(&args, cmd_dir.unwrap()) | ||
} else { | ||
light_cmd.execute(&args) | ||
let args = self | ||
.cmd | ||
.split_whitespace() | ||
.map(Into::into) | ||
.collect::<Vec<_>>(); | ||
|
||
if !no_cache && self.cmd_dir.is_some() { | ||
light_cmd.try_cache_or_execute(&args, self.cmd_dir.clone().unwrap()) | ||
} else { | ||
light_cmd.execute(&args) | ||
} | ||
} | ||
} |
Oops, something went wrong.