Fix paste automatically

This commit is contained in:
Ferran Basora 2019-02-26 23:48:31 +00:00
parent b1200599bf
commit 4f34101c23

View File

@ -85,115 +85,121 @@ fn main() {
let lines = output.split("\n").collect::<Vec<&str>>(); let lines = output.split("\n").collect::<Vec<&str>>();
let mut state = state::State::new(lines, alphabet); let mut state = state::State::new(lines, alphabet);
let mut paste = false;
let mut rustbox = match RustBox::init(Default::default()) { {
Result::Ok(v) => v, let mut rustbox = match RustBox::init(Default::default()) {
Result::Err(e) => panic!("{}", e), Result::Ok(v) => v,
}; Result::Err(e) => panic!("{}", e),
};
rustbox.set_output_mode(OutputMode::EightBit); rustbox.set_output_mode(OutputMode::EightBit);
for (index, line) in state.lines.iter().enumerate() { for (index, line) in state.lines.iter().enumerate() {
let clean = line.trim_right_matches(|c: char| c.is_whitespace()); let clean = line.trim_right_matches(|c: char| c.is_whitespace());
if clean.len() > 0 { if clean.len() > 0 {
let formatted = format!("{}\n", line).to_string(); let formatted = format!("{}\n", line).to_string();
rustbox.print(0, index, rustbox::RB_NORMAL, Color::White, Color::Black, formatted.as_str()); rustbox.print(0, index, rustbox::RB_NORMAL, Color::White, Color::Black, formatted.as_str());
}
}
let mut typed_hint: String = "".to_owned();
let matches = state.matches(reverse, unique);
let longest_hint = matches.iter().filter(|&m| m.hint.clone().is_some()).last().unwrap().hint.clone().expect("Unknown hint").len();
loop {
let mut selected = matches.last();
match matches.iter().enumerate().find(|&h| h.0 == state.skip) {
Some(hm) => {
selected = Some(hm.1);
}
_ => {}
}
for mat in matches.iter() {
let selected_color = if selected == Some(mat) {
select_foreground_color
} else {
foreground_color
};
// TODO: Find long utf sequences and extract it from mat.x
// let re = regex::bytes::Regex::new(r"127").unwrap();
// let line = lines[mat.y as usize];
// let extra = re
// .find_iter(line.as_bytes())
// .fold(0, |sum, item| sum + item.as_bytes().len());
let extra = 0;
let offset = (mat.x as usize) - extra;
rustbox.print(offset, mat.y as usize, rustbox::RB_NORMAL, selected_color, background_color, mat.text);
if let Some(ref hint) = mat.hint {
let extra_position = if position == "left" { 0 } else { mat.text.len() - mat.hint.clone().unwrap().len() };
rustbox.print(offset + extra_position, mat.y as usize, rustbox::RB_BOLD, hint_foreground_color, hint_background_color, hint.as_str());
} }
} }
rustbox.present(); let mut typed_hint: String = "".to_owned();
let matches = state.matches(reverse, unique);
let longest_hint = matches.iter().filter(|&m| m.hint.clone().is_some()).last().unwrap().hint.clone().expect("Unknown hint").len();
match rustbox.poll_event(false) { loop {
Ok(rustbox::Event::KeyEvent(key)) => { let mut selected = matches.last();
match key {
Key::Esc => { break; }
Key::Enter => {
let mut choosen = matches.first().unwrap();
match matches.iter().enumerate().find(|&h| h.0 == state.skip) { match matches.iter().enumerate().find(|&h| h.0 == state.skip) {
Some(hm) => { Some(hm) => {
choosen = hm.1; selected = Some(hm.1);
} }
_ => {} _ => {}
} }
exec_command(format!("tmux set-buffer {}", choosen.text)); for mat in matches.iter() {
let selected_color = if selected == Some(mat) {
select_foreground_color
} else {
foreground_color
};
break; // TODO: Find long utf sequences and extract it from mat.x
} // let re = regex::bytes::Regex::new(r"127").unwrap();
Key::Up => { state.prev(); } // let line = lines[mat.y as usize];
Key::Down => { state.next(); } // let extra = re
Key::Left => { state.prev(); } // .find_iter(line.as_bytes())
Key::Right => { state.next(); } // .fold(0, |sum, item| sum + item.as_bytes().len());
Key::Char(ch) => {
let key = ch.to_string();
let lower_key = key.to_lowercase();
typed_hint.push_str(lower_key.as_str());
match matches.iter().find(|mat| mat.hint == Some(typed_hint.clone())) {
Some(mat) => {
exec_command(format!("tmux set-buffer {}", mat.text));
if key == key.to_uppercase() { let extra = 0;
// FIXME
exec_command(format!("tmux paste-buffer"));
}
break; let offset = (mat.x as usize) - extra;
},
None => { rustbox.print(offset, mat.y as usize, rustbox::RB_NORMAL, selected_color, background_color, mat.text);
if typed_hint.len() > longest_hint {
break; if let Some(ref hint) = mat.hint {
} let extra_position = if position == "left" { 0 } else { mat.text.len() - mat.hint.clone().unwrap().len() };
}
} rustbox.print(offset + extra_position, mat.y as usize, rustbox::RB_BOLD, hint_foreground_color, hint_background_color, hint.as_str());
}
_ => {}
} }
} }
Err(e) => panic!("{}", e),
_ => { } rustbox.present();
match rustbox.poll_event(false) {
Ok(rustbox::Event::KeyEvent(key)) => {
match key {
Key::Esc => { break; }
Key::Enter => {
let mut choosen = matches.first().unwrap();
match matches.iter().enumerate().find(|&h| h.0 == state.skip) {
Some(hm) => {
choosen = hm.1;
}
_ => {}
}
exec_command(format!("tmux set-buffer {}", choosen.text));
break;
}
Key::Up => { state.prev(); }
Key::Down => { state.next(); }
Key::Left => { state.prev(); }
Key::Right => { state.next(); }
Key::Char(ch) => {
let key = ch.to_string();
let lower_key = key.to_lowercase();
typed_hint.push_str(lower_key.as_str());
match matches.iter().find(|mat| mat.hint == Some(typed_hint.clone())) {
Some(mat) => {
exec_command(format!("tmux set-buffer {}", mat.text));
if key == key.to_uppercase() {
paste = true;
}
break;
},
None => {
if typed_hint.len() > longest_hint {
break;
}
}
}
}
_ => {}
}
}
Err(e) => panic!("{}", e),
_ => { }
}
} }
} }
if paste {
exec_command(format!("tmux paste-buffer"));
}
} }