2019-08-02 15:59:10 +02:00
|
|
|
use crate::buffer::Buffer;
|
|
|
|
|
use crate::color::Color;
|
2019-08-06 14:04:34 +02:00
|
|
|
use crate::draw::{Font, ROBOTO_REGULAR};
|
2019-08-02 15:59:10 +02:00
|
|
|
use crate::module::{Input, ModuleImpl};
|
|
|
|
|
|
2019-08-06 14:21:10 +02:00
|
|
|
use std::cell::RefCell;
|
2019-08-02 19:11:39 +02:00
|
|
|
use std::cmp::Ordering;
|
2019-08-04 14:23:49 +02:00
|
|
|
use std::io::Read;
|
2019-08-02 15:59:10 +02:00
|
|
|
|
|
|
|
|
use atty::Stream;
|
|
|
|
|
use chrono::{DateTime, Local};
|
2019-08-04 15:41:56 +02:00
|
|
|
use fuzzy_matcher::skim::{fuzzy_indices, fuzzy_match};
|
2019-08-02 15:59:10 +02:00
|
|
|
use smithay_client_toolkit::keyboard::keysyms;
|
|
|
|
|
|
|
|
|
|
pub struct Launcher {
|
|
|
|
|
options: Vec<String>,
|
|
|
|
|
matches: Vec<String>,
|
|
|
|
|
cur: String,
|
|
|
|
|
offset: usize,
|
2019-08-06 14:04:34 +02:00
|
|
|
font: RefCell<Font>,
|
2019-08-04 14:23:49 +02:00
|
|
|
dirty: bool,
|
2019-08-02 15:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Launcher {
|
|
|
|
|
pub fn new() -> Result<Launcher, ::std::io::Error> {
|
|
|
|
|
if !atty::is(Stream::Stdin) {
|
|
|
|
|
let mut inbuf = String::new();
|
|
|
|
|
std::io::stdin().read_to_string(&mut inbuf).unwrap();
|
2019-08-04 14:23:49 +02:00
|
|
|
let options = inbuf
|
|
|
|
|
.split("\n")
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
|
.collect();
|
2019-08-02 15:59:10 +02:00
|
|
|
|
|
|
|
|
Ok(Launcher {
|
|
|
|
|
options: options,
|
|
|
|
|
matches: vec![],
|
|
|
|
|
cur: "".to_string(),
|
|
|
|
|
offset: 0,
|
2019-08-06 14:04:34 +02:00
|
|
|
font: RefCell::new(Font::new(&ROBOTO_REGULAR, 32.0)),
|
2019-08-02 15:59:10 +02:00
|
|
|
dirty: true,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2019-08-04 14:23:49 +02:00
|
|
|
Err(::std::io::Error::new(
|
|
|
|
|
::std::io::ErrorKind::Other,
|
|
|
|
|
"input is a tty",
|
|
|
|
|
))
|
2019-08-02 15:59:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ModuleImpl for Launcher {
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
buf: &mut Buffer,
|
|
|
|
|
bg: &Color,
|
|
|
|
|
_time: &DateTime<Local>,
|
|
|
|
|
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
|
|
|
|
|
buf.memset(bg);
|
|
|
|
|
|
2019-08-06 16:39:17 +02:00
|
|
|
self.font.borrow_mut().auto_draw_text(
|
2019-08-02 15:59:10 +02:00
|
|
|
&mut buf.subdimensions((0, 0, 128, 32))?,
|
|
|
|
|
bg,
|
2019-08-04 14:23:49 +02:00
|
|
|
&Color::new(0.75, 0.75, 0.75, 1.0),
|
2019-08-02 16:06:51 +02:00
|
|
|
"Run: ",
|
2019-08-02 15:59:10 +02:00
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let mut x_off: i32 = 0;
|
|
|
|
|
let mut width_remaining: i32 = 1280 - 64;
|
2019-08-04 14:39:23 +02:00
|
|
|
if self.matches.len() == 0 && self.cur.len() > 0 {
|
2019-08-06 16:39:17 +02:00
|
|
|
self.font.borrow_mut().auto_draw_text(
|
2019-08-04 15:41:56 +02:00
|
|
|
&mut buf
|
|
|
|
|
.subdimensions((64, 0, width_remaining as u32, 32))
|
|
|
|
|
.unwrap(),
|
2019-08-04 14:39:23 +02:00
|
|
|
bg,
|
|
|
|
|
&Color::new(1.0, 0.5, 0.5, 1.0),
|
|
|
|
|
&self.cur,
|
|
|
|
|
)?;
|
|
|
|
|
} else {
|
|
|
|
|
for (idx, m) in self.matches.iter().enumerate() {
|
|
|
|
|
let mut b =
|
|
|
|
|
match buf.subdimensions((64 + x_off as u32, 0, width_remaining as u32, 32)) {
|
|
|
|
|
Ok(b) => b,
|
|
|
|
|
Err(_) => break,
|
|
|
|
|
};
|
|
|
|
|
let size = if idx == self.offset && self.cur.len() > 0 {
|
2019-08-04 15:41:56 +02:00
|
|
|
let (_, indices) =
|
|
|
|
|
fuzzy_indices(&m.to_lowercase(), &self.cur.to_lowercase()).unwrap();
|
2019-08-04 14:39:23 +02:00
|
|
|
let mut colors = Vec::with_capacity(m.len());
|
|
|
|
|
for pos in 0..m.len() {
|
2019-08-04 15:41:56 +02:00
|
|
|
if indices.contains(&pos) {
|
2019-08-04 14:39:23 +02:00
|
|
|
colors.push(Color::new(1.0, 1.0, 1.0, 1.0));
|
|
|
|
|
} else {
|
|
|
|
|
colors.push(Color::new(0.75, 0.75, 0.75, 1.0));
|
|
|
|
|
}
|
2019-08-03 10:39:48 +02:00
|
|
|
}
|
2019-08-06 14:21:10 +02:00
|
|
|
self.font
|
|
|
|
|
.borrow_mut()
|
2019-08-06 16:39:17 +02:00
|
|
|
.auto_draw_text_individual_colors(&mut b, bg, &colors, &m)?
|
2019-08-04 14:39:23 +02:00
|
|
|
} else {
|
2019-08-06 16:39:17 +02:00
|
|
|
self.font.borrow_mut().auto_draw_text(
|
2019-08-04 14:39:23 +02:00
|
|
|
&mut b,
|
|
|
|
|
bg,
|
|
|
|
|
&Color::new(0.5, 0.5, 0.5, 1.0),
|
|
|
|
|
m,
|
|
|
|
|
)?
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
x_off += (size.0 + 8) as i32;
|
|
|
|
|
width_remaining -= (size.0 + 8) as i32;
|
|
|
|
|
|
|
|
|
|
if width_remaining < 0 {
|
|
|
|
|
break;
|
2019-08-03 10:39:48 +02:00
|
|
|
}
|
2019-08-02 15:59:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(vec![buf.get_signed_bounds()])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
|
|
|
|
|
if self.dirty || force {
|
2019-08-02 19:11:39 +02:00
|
|
|
let mut m = self
|
2019-08-02 15:59:10 +02:00
|
|
|
.options
|
|
|
|
|
.iter()
|
2019-08-04 15:41:56 +02:00
|
|
|
.map(|x| (fuzzy_match(&x.to_lowercase(), &self.cur.to_lowercase()), x))
|
2019-08-02 19:11:39 +02:00
|
|
|
.filter(|(x, _)| x.is_some())
|
|
|
|
|
.map(|(x, y)| (x.unwrap(), y.to_string()))
|
2019-08-04 15:41:56 +02:00
|
|
|
.collect::<Vec<(i64, String)>>();
|
2019-08-02 15:59:10 +02:00
|
|
|
|
2019-08-02 19:11:39 +02:00
|
|
|
m.sort_by(|(x1, y1), (x2, y2)| {
|
2019-08-06 09:51:14 +02:00
|
|
|
if x1 > x2 {
|
2019-08-02 19:11:39 +02:00
|
|
|
Ordering::Less
|
2019-08-06 09:51:14 +02:00
|
|
|
} else if x1 < x2 {
|
2019-08-02 19:11:39 +02:00
|
|
|
Ordering::Greater
|
|
|
|
|
} else if y1.len() < y2.len() {
|
|
|
|
|
Ordering::Less
|
|
|
|
|
} else if y1.len() > y2.len() {
|
|
|
|
|
Ordering::Greater
|
|
|
|
|
} else {
|
|
|
|
|
y1.cmp(y2)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.matches = m.into_iter().map(|(_, x)| x).collect();
|
2019-08-02 15:59:10 +02:00
|
|
|
self.dirty = false;
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn input(&mut self, input: Input) {
|
|
|
|
|
match input {
|
|
|
|
|
Input::Keypress { key, interpreted } => match key {
|
|
|
|
|
keysyms::XKB_KEY_BackSpace => {
|
|
|
|
|
if self.cur.len() > 0 {
|
2019-08-04 14:23:49 +02:00
|
|
|
self.cur = self.cur[..self.cur.len() - 1].to_string();
|
2019-08-02 15:59:10 +02:00
|
|
|
self.offset = 0;
|
|
|
|
|
self.dirty = true
|
|
|
|
|
}
|
2019-08-04 14:23:49 +02:00
|
|
|
}
|
2019-08-02 15:59:10 +02:00
|
|
|
keysyms::XKB_KEY_Return => {
|
|
|
|
|
if self.matches.len() > self.offset {
|
|
|
|
|
println!("{}", self.matches[self.offset]);
|
|
|
|
|
std::process::exit(0);
|
2019-08-04 14:39:23 +02:00
|
|
|
} else {
|
|
|
|
|
println!("{}", self.cur);
|
|
|
|
|
std::process::exit(0);
|
2019-08-02 15:59:10 +02:00
|
|
|
}
|
2019-08-04 14:23:49 +02:00
|
|
|
}
|
2019-08-02 15:59:10 +02:00
|
|
|
keysyms::XKB_KEY_Right => {
|
2019-08-04 14:23:49 +02:00
|
|
|
if self.matches.len() > 0 && self.offset < self.matches.len() - 1 {
|
2019-08-02 15:59:10 +02:00
|
|
|
self.offset += 1;
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
}
|
2019-08-04 14:23:49 +02:00
|
|
|
}
|
2019-08-02 15:59:10 +02:00
|
|
|
keysyms::XKB_KEY_Left => {
|
|
|
|
|
if self.matches.len() > 0 && self.offset > 0 {
|
|
|
|
|
self.offset -= 1;
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
}
|
2019-08-04 14:23:49 +02:00
|
|
|
}
|
2019-08-02 15:59:10 +02:00
|
|
|
_ => match interpreted {
|
|
|
|
|
Some(v) => {
|
|
|
|
|
self.cur += &v;
|
|
|
|
|
self.offset = 0;
|
|
|
|
|
self.dirty = true
|
2019-08-04 14:23:49 +02:00
|
|
|
}
|
2019-08-02 15:59:10 +02:00
|
|
|
None => {}
|
2019-08-04 14:23:49 +02:00
|
|
|
},
|
|
|
|
|
},
|
2019-08-02 15:59:10 +02:00
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|