1
0
mirror of https://github.com/bobwen-dev/hunter synced 2025-04-12 00:55:41 +02:00

don't throw crap at screen when preview-process is kill

This commit is contained in:
rabite 2019-02-09 12:49:57 +01:00
parent 95291e9408
commit 528f725145
3 changed files with 24 additions and 9 deletions

View File

@ -1,3 +1,5 @@
#![feature(vec_remove_item)]
extern crate termion; extern crate termion;
extern crate unicode_width; extern crate unicode_width;
#[macro_use] #[macro_use]

View File

@ -10,14 +10,14 @@ use crate::widget::Widget;
lazy_static! { lazy_static! {
static ref PIDS: Arc<Mutex<Vec<i32>>> = { Arc::new(Mutex::new(vec![])) }; static ref PIDS: Arc<Mutex<Vec<u32>>> = { Arc::new(Mutex::new(vec![])) };
static ref CURFILE: Arc<Mutex<Option<File>>> = { Arc::new(Mutex::new(None)) }; static ref CURFILE: Arc<Mutex<Option<File>>> = { Arc::new(Mutex::new(None)) };
} }
fn kill_procs() { fn kill_procs() {
let mut pids = PIDS.lock().unwrap(); let mut pids = PIDS.lock().unwrap();
for pid in &*pids { for pid in &*pids {
unsafe { libc::kill(*pid, 9); } unsafe { libc::kill(*pid as i32, 9); }
} }
pids.clear(); pids.clear();
} }
@ -92,13 +92,17 @@ impl Previewer {
.spawn().unwrap(); .spawn().unwrap();
let pid = process.id(); let pid = process.id();
PIDS.lock().unwrap().push(pid as i32); PIDS.lock().unwrap().push(pid);
if !is_current(&file) { return } if !is_current(&file) { return }
let output = process.wait_with_output().unwrap(); let output = process.wait_with_output();
if output.is_err() { return }
let output = output.unwrap();
let status = output.status.code().unwrap(); let status = output.status.code();
if status.is_none() { return }
let status = status.unwrap();
if status == 0 || status == 5 && is_current(&file) { if status == 0 || status == 5 && is_current(&file) {
let output = std::str::from_utf8(&output.stdout) let output = std::str::from_utf8(&output.stdout)
@ -116,7 +120,7 @@ impl Previewer {
{ {
write!(std::io::stdout(), "{}", redraw).unwrap(); write!(std::io::stdout(), "{}", redraw).unwrap();
} }
PIDS.lock().unwrap().pop(); PIDS.lock().unwrap().remove_item(&pid);
} }
} }
} }

View File

@ -160,12 +160,18 @@ pub fn minibuffer(query: &str) -> Option<String> {
let part = buffer.rsplitn(2, " ").take(1) let part = buffer.rsplitn(2, " ").take(1)
.map(|s| s.to_string()).collect::<String>(); .map(|s| s.to_string()).collect::<String>();
let completions = find_bins(&part);
let completions = find_files(&part);
if !completions.is_empty() { if !completions.is_empty() {
buffer = buffer[..buffer.len() - part.len()].to_string(); buffer = buffer[..buffer.len() - part.len()].to_string();
buffer.push_str(&completions[0]); buffer.push_str(&completions[0]);
pos += &completions[0].len() - part.len(); pos += &completions[0].len() - part.len();
} else {
let completions = find_files(&part);
if !completions.is_empty() {
buffer = buffer[..buffer.len() - part.len()].to_string();
buffer.push_str(&completions[0]);
pos += &completions[0].len() - part.len();
}
} }
} else { } else {
buffer += "$s"; buffer += "$s";
@ -243,7 +249,10 @@ pub fn find_files(comp_name: &str) -> Vec<String> {
cwd.push(comp_name); cwd.push(comp_name);
} }
std::fs::read_dir(cwd.clone()).unwrap().flat_map(|file| { let reader = std::fs::read_dir(cwd.clone());
if reader.is_err() { return vec![]; }
let reader = reader.unwrap();
reader.flat_map(|file| {
let file = file.unwrap(); let file = file.unwrap();
let name = file.file_name().into_string().unwrap(); let name = file.file_name().into_string().unwrap();
if name.starts_with(comp_name) { if name.starts_with(comp_name) {