Skip to content

Commit

Permalink
Refactor icon painter and cmd (#386)
Browse files Browse the repository at this point in the history
* 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
liuchengxu authored Apr 12, 2020
1 parent 6f35788 commit 3913e1e
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 239 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ CHANGELOG

## [unreleased]

### Added

- Add `--content-filtering` in maple. You can use `:Clap files +name-only ~` to filter the file name instead of full file path, but you can only use it when clap is using the cached tempfile inside vim.

### Improved

- icon highlight for truncated grep text.

### Changed

- `grep2` will not match the file path by default. ([#385](https://github.com/liuchengxu/vim-clap/pull/385))

### Fixed

- `ITEMS_TO_SHOW` is fixed at the moment, only 30 rows can be shown correctly for dyn filter. https://github.com/liuchengxu/vim-clap/pull/385#issuecomment-611601076
- Fix wrong icon for dyn filter when the text is truncated.

## [0.11] 2020-04-09

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

19 changes: 14 additions & 5 deletions autoload/clap/filter/async/dyn.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,34 @@ endfunction

function! clap#filter#async#dyn#from_tempfile(tempfile) abort
let s:last_query = g:clap.input.get()

if g:clap_enable_icon && index(['files', 'git_files'], g:clap.provider.id) > -1
let enable_icon_opt = '--enable-icon'
let enable_icon_opt = '--icon-painter=File'
else
let enable_icon_opt = ''
endif
let filter_cmd = printf('%s --number %d --winwidth %d filter "%s" --input "%s"',

if g:clap.provider.id ==# 'files' && has_key(g:clap.context, 'name-only')
let content_filtering = '--content-filtering=FileNameOnly'
else
let content_filtering = ''
endif

let filter_cmd = printf('%s --number %d --winwidth %d filter "%s" --input "%s" %s',
\ enable_icon_opt,
\ s:DYN_ITEMS_TO_SHOW,
\ winwidth(g:clap.display.winid),
\ g:clap.input.get(),
\ a:tempfile
\ a:tempfile,
\ content_filtering,
\ )
call clap#job#stdio#start_service(function('s:handle_message'), clap#maple#build_cmd(filter_cmd))
endfunction

function! clap#filter#async#dyn#start_grep() abort
let s:last_query = g:clap.input.get()
let grep_cmd = printf('%s --number %d --winwidth %d grep "" "%s" --cmd-dir "%s"',
\ g:clap_enable_icon ? '--enable-icon' : '',
\ g:clap_enable_icon ? '--icon-painter=Grep' : '',
\ s:DYN_ITEMS_TO_SHOW,
\ winwidth(g:clap.display.winid),
\ g:clap.input.get(),
Expand All @@ -58,7 +67,7 @@ endfunction
function! clap#filter#async#dyn#grep_from_cache(tempfile) abort
let s:last_query = g:clap.input.get()
let grep_cmd = printf('%s %s --number %d --winwidth %d grep "" "%s" --input "%s"',
\ g:clap_enable_icon ? '--enable-icon' : '',
\ g:clap_enable_icon ? '--icon-painter=Grep' : '',
\ has_key(g:clap.context, 'no-cache') ? '--no-cache' : '',
\ s:DYN_ITEMS_TO_SHOW,
\ winwidth(g:clap.display.winid),
Expand Down
2 changes: 1 addition & 1 deletion autoload/clap/job/stdio.vim
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function! clap#job#stdio#start_dyn_filter_service(MessageHandler, cmd) abort
let s:MessageHandler = a:MessageHandler

let filter_cmd = printf('%s --number 100 --winwidth %d filter "%s" --cmd "%s" --cmd-dir "%s"',
\ g:clap_enable_icon ? '--enable-icon' : '',
\ g:clap_enable_icon ? '--icon-painter=File' : '',
\ winwidth(g:clap.display.winid),
\ g:clap.input.get(),
\ a:cmd,
Expand Down
10 changes: 5 additions & 5 deletions autoload/clap/maple.vim
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ let s:can_enable_icon = ['files', 'git_files']
function! clap#maple#get_enable_icon_opt() abort
if g:clap_enable_icon
\ && index(s:can_enable_icon, g:clap.provider.id) > -1
return '--enable-icon'
return '--icon-painter=File'
else
return ''
endif
Expand Down Expand Up @@ -210,7 +210,7 @@ function! clap#maple#sync_filter_subcommand(query) abort
let global_opt = '--number '.g:clap.display.preload_capacity.' --winwidth '.winwidth(g:clap.display.winid)

if g:clap.provider.id ==# 'files' && g:clap_enable_icon
let global_opt .= ' --enable-icon'
let global_opt .= ' --icon-painter=File'
endif

let cmd = printf('%s %s filter "%s" --sync', s:maple_bin, global_opt, a:query)
Expand All @@ -222,7 +222,7 @@ function! clap#maple#ripgrep_forerunner_subcommand() abort
" let global_opt = '--number '.g:clap.display.preload_capacity
" TODO: add max_output
if g:clap_enable_icon
let global_opt = '--enable-icon'
let global_opt = '--icon-painter=Grep'
else
let global_opt = ''
endif
Expand All @@ -243,7 +243,7 @@ endfunction
function! clap#maple#run_exec(cmd) abort
let global_opt = '--number '.g:clap.display.preload_capacity
if g:clap.provider.id ==# 'files' && g:clap_enable_icon
let global_opt .= ' --enable-icon'
let global_opt .= ' --icon-painter=File'
endif

let cmd_dir = clap#rooter#working_dir()
Expand All @@ -257,7 +257,7 @@ endfunction
function! clap#maple#run_sync_grep(cmd, query, enable_icon, glob) abort
let global_opt = '--number '.g:clap.display.preload_capacity
if a:enable_icon
let global_opt .= ' --enable-icon'
let global_opt .= ' --icon-painter=Grep'
endif

let cmd_dir = clap#rooter#working_dir()
Expand Down
1 change: 1 addition & 0 deletions crates/icon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ icon is a tool for drawing an icon according to the path name for vim-clap.
[dependencies]
regex = "1"
lazy_static = "1.4.0"
structopt = "0.3"
38 changes: 26 additions & 12 deletions crates/icon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ mod constants;

pub use constants::{bsearch_icon_table, EXACTMATCH_ICON_TABLE, EXTENSION_ICON_TABLE};

use std::path::Path;

use lazy_static::lazy_static;
use regex::Regex;
use std::path::Path;
use structopt::clap::arg_enum;

pub const DEFAULT_ICON: char = '';
pub const FOLDER_ICON: char = '';
Expand Down Expand Up @@ -64,23 +64,29 @@ pub fn prepend_filer_icon(path: &Path, line: &str) -> String {
format!("{} {}", icon_for_filer(path), line)
}

/// Prepend an icon to the output line of ripgrep.
pub fn prepend_grep_icon(line: &str) -> String {
#[inline]
fn grep_icon_for(line: &str) -> Icon {
lazy_static! {
static ref RE: Regex = Regex::new(r"^(.*):\d+:\d+:").unwrap();
}
let icon = RE
.captures(line)
RE.captures(line)
.and_then(|cap| cap.get(1))
.map(|m| icon_for(m.as_str()))
.unwrap_or(DEFAULT_ICON);
format!("{} {}", icon, line)
.unwrap_or(DEFAULT_ICON)
}

/// Prepend an icon to the output line of ripgrep.
pub fn prepend_grep_icon(line: &str) -> String {
format!("{} {}", grep_icon_for(line), line)
}

/// Prepend an icon for various kind of output line.
pub enum IconPainter {
File,
Grep,
arg_enum! {
/// Prepend an icon for various kind of output line.
#[derive(Clone, Debug)]
pub enum IconPainter {
File,
Grep,
}
}

impl IconPainter {
Expand All @@ -91,4 +97,12 @@ impl IconPainter {
Self::Grep => prepend_grep_icon(raw_str),
}
}

/// Returns appropriate icon for the given text.
pub fn get_icon(&self, text: &str) -> Icon {
match *self {
Self::File => icon_for(text),
Self::Grep => grep_icon_for(text),
}
}
}
6 changes: 3 additions & 3 deletions crates/maple_cli/src/cmd/blines.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cmd::filter::ContentFiltering;
use anyhow::Result;
use fuzzy_filter::Source;
use std::path::PathBuf;
Expand Down Expand Up @@ -28,10 +29,9 @@ impl Blines {
),
None,
number,
false,
winwidth,
false,
false,
None,
ContentFiltering::Full,
)
}
}
104 changes: 63 additions & 41 deletions crates/maple_cli/src/cmd/exec.rs
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)
}
}
}
Loading

0 comments on commit 3913e1e

Please sign in to comment.