This commit is contained in:
Kenny Levinsen
2019-08-04 14:23:49 +02:00
parent 5cc4a6e740
commit beb6cb2f08
9 changed files with 181 additions and 131 deletions
+25 -25
View File
@@ -3,8 +3,8 @@ use crate::color::Color;
use crate::draw::{draw_text, draw_text_individual_colors, ROBOTO_REGULAR};
use crate::module::{Input, ModuleImpl};
use std::io::Read;
use std::cmp::Ordering;
use std::io::Read;
use atty::Stream;
@@ -17,7 +17,7 @@ pub struct Launcher {
matches: Vec<String>,
cur: String,
offset: usize,
dirty: bool
dirty: bool,
}
impl Launcher {
@@ -25,7 +25,11 @@ impl Launcher {
if !atty::is(Stream::Stdin) {
let mut inbuf = String::new();
std::io::stdin().read_to_string(&mut inbuf).unwrap();
let options = inbuf.split("\n").into_iter().map(|s| s.to_string()).collect();
let options = inbuf
.split("\n")
.into_iter()
.map(|s| s.to_string())
.collect();
Ok(Launcher {
options: options,
@@ -35,7 +39,10 @@ impl Launcher {
dirty: true,
})
} else {
Err(::std::io::Error::new(::std::io::ErrorKind::Other, "input is a tty"))
Err(::std::io::Error::new(
::std::io::ErrorKind::Other,
"input is a tty",
))
}
}
}
@@ -53,16 +60,16 @@ impl ModuleImpl for Launcher {
ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 128, 32))?,
bg,
&Color::new(0.75, 0.75, 0.75, 1.0),
&Color::new(0.75, 0.75, 0.75, 1.0),
32.0,
"Run: ",
)?;
let mut x_off: i32 = 0;
let mut width_remaining: i32 = 1280 - 64;
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)) {
let mut b = match buf.subdimensions((64 + x_off as u32, 0, width_remaining as u32, 32))
{
Ok(b) => b,
Err(_) => break,
};
@@ -71,19 +78,13 @@ impl ModuleImpl for Launcher {
let off = m.to_lowercase().find(&self.cur.to_lowercase()).unwrap();
let mut colors = Vec::with_capacity(m.len());
for pos in 0..m.len() {
if pos >= off && pos < off + l {
if pos >= off && pos < off + l {
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));
}
}
draw_text_individual_colors(
ROBOTO_REGULAR,
&mut b,
bg,
&colors,
32.0,
&m)?
draw_text_individual_colors(ROBOTO_REGULAR, &mut b, bg, &colors, 32.0, &m)?
} else {
draw_text(
ROBOTO_REGULAR,
@@ -93,7 +94,6 @@ impl ModuleImpl for Launcher {
32.0,
m,
)?
};
x_off += (size.0 + 8) as i32;
@@ -144,38 +144,38 @@ impl ModuleImpl for Launcher {
Input::Keypress { key, interpreted } => match key {
keysyms::XKB_KEY_BackSpace => {
if self.cur.len() > 0 {
self.cur = self.cur[..self.cur.len()-1].to_string();
self.cur = self.cur[..self.cur.len() - 1].to_string();
self.offset = 0;
self.dirty = true
}
},
}
keysyms::XKB_KEY_Return => {
if self.matches.len() > self.offset {
println!("{}", self.matches[self.offset]);
std::process::exit(0);
}
},
}
keysyms::XKB_KEY_Right => {
if self.matches.len() > 0 && self.offset < self.matches.len() - 1{
if self.matches.len() > 0 && self.offset < self.matches.len() - 1 {
self.offset += 1;
self.dirty = true;
}
},
}
keysyms::XKB_KEY_Left => {
if self.matches.len() > 0 && self.offset > 0 {
self.offset -= 1;
self.dirty = true;
}
},
}
_ => match interpreted {
Some(v) => {
self.cur += &v;
self.offset = 0;
self.dirty = true
},
}
None => {}
}
}
},
},
_ => {}
}
}