launcher: Add calculator feature
This commit is contained in:
+50
-38
@@ -6,8 +6,6 @@ use crate::module::{Input, ModuleImpl};
|
||||
use std::cell::RefCell;
|
||||
use std::cmp::Ordering;
|
||||
use std::io::Read;
|
||||
use std::process::Command;
|
||||
use std::str;
|
||||
|
||||
use atty::Stream;
|
||||
use chrono::{DateTime, Local};
|
||||
@@ -17,7 +15,8 @@ use smithay_client_toolkit::keyboard::keysyms;
|
||||
pub struct Launcher {
|
||||
options: Vec<String>,
|
||||
matches: Vec<String>,
|
||||
cur: String,
|
||||
input: String,
|
||||
result: Option<String>,
|
||||
offset: usize,
|
||||
font: RefCell<Font>,
|
||||
dirty: bool,
|
||||
@@ -27,7 +26,7 @@ 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();
|
||||
std::io::stdin().read_to_string(&mut inbuf)?;
|
||||
let options = inbuf
|
||||
.split("\n")
|
||||
.into_iter()
|
||||
@@ -38,7 +37,8 @@ impl Launcher {
|
||||
Ok(Launcher {
|
||||
options: options,
|
||||
matches: vec![],
|
||||
cur: "".to_string(),
|
||||
input: "".to_string(),
|
||||
result: None,
|
||||
offset: 0,
|
||||
font: RefCell::new(Font::new(&ROBOTO_REGULAR, 32.0)),
|
||||
dirty: true,
|
||||
@@ -47,7 +47,8 @@ impl Launcher {
|
||||
Ok(Launcher {
|
||||
options: vec![],
|
||||
matches: vec![],
|
||||
cur: "".to_string(),
|
||||
input: "".to_string(),
|
||||
result: None,
|
||||
offset: 0,
|
||||
font: RefCell::new(Font::new(&ROBOTO_REGULAR, 32.0)),
|
||||
dirty: true,
|
||||
@@ -62,13 +63,13 @@ impl Launcher {
|
||||
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
|
||||
buf.memset(bg);
|
||||
|
||||
let mut x_off = if self.cur.len() > 0 {
|
||||
let mut x_off = if self.input.len() > 0 {
|
||||
let c = if self.matches.len() == 0 { Color::new(1.0, 0.5, 0.5, 1.0) } else { Color::new(1.0, 1.0, 1.0, 1.0) };
|
||||
let dim = self.font.borrow_mut().auto_draw_text(
|
||||
&mut buf.subdimensions((0, 0, 1232, 32))?,
|
||||
bg,
|
||||
&c,
|
||||
&self.cur,
|
||||
&self.input,
|
||||
)?;
|
||||
|
||||
dim.0 + 8
|
||||
@@ -84,9 +85,9 @@ impl Launcher {
|
||||
Ok(b) => b,
|
||||
Err(_) => break,
|
||||
};
|
||||
let size = if idx == self.offset && self.cur.len() > 0 {
|
||||
let size = if idx == self.offset && self.input.len() > 0 {
|
||||
let (_, indices) =
|
||||
fuzzy_indices(&m.to_lowercase(), &self.cur.to_lowercase()).unwrap();
|
||||
fuzzy_indices(&m.to_lowercase(), &self.input.to_lowercase()).unwrap();
|
||||
let mut colors = Vec::with_capacity(m.len());
|
||||
for pos in 0..m.len() {
|
||||
if indices.contains(&pos) {
|
||||
@@ -125,12 +126,12 @@ impl Launcher {
|
||||
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
|
||||
buf.memset(bg);
|
||||
|
||||
let x_off = if self.cur.len() > 0 {
|
||||
let x_off = if self.input.len() > 0 {
|
||||
let dim = self.font.borrow_mut().auto_draw_text(
|
||||
&mut buf.subdimensions((0, 0, 1232, 32))?,
|
||||
bg,
|
||||
&Color::new(1.0, 1.0, 1.0, 1.0),
|
||||
&self.cur,
|
||||
&self.input,
|
||||
)?;
|
||||
|
||||
dim.0 + 16
|
||||
@@ -138,21 +139,13 @@ impl Launcher {
|
||||
0
|
||||
};
|
||||
|
||||
if let Ok(output) = Command::new("ivy")
|
||||
.arg("-e")
|
||||
.arg(&self.cur.chars().skip(1).collect::<String>())
|
||||
.output() {
|
||||
if let Ok(stdout) = str::from_utf8(&output.stdout) {
|
||||
let stdout = stdout.trim();
|
||||
if stdout.len() > 0 {
|
||||
self.font.borrow_mut().auto_draw_text(
|
||||
&mut buf.subdimensions((x_off, 0, 1232-x_off, 32))?,
|
||||
bg,
|
||||
&Color::new(0.75, 0.75, 0.75, 1.0),
|
||||
&format!(" = {:}", stdout),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
if let Some(result) = &self.result {
|
||||
self.font.borrow_mut().auto_draw_text(
|
||||
&mut buf.subdimensions((x_off, 0, 1232-x_off, 32))?,
|
||||
bg,
|
||||
&Color::new(0.75, 0.75, 0.75, 1.0),
|
||||
&format!(" = {:}", result),
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(vec![buf.get_signed_bounds()])
|
||||
@@ -169,13 +162,23 @@ impl Launcher {
|
||||
&mut buf.subdimensions((0, 0, 1232, 32))?,
|
||||
bg,
|
||||
&Color::new(1.0, 1.0, 1.0, 1.0),
|
||||
&self.cur,
|
||||
&self.input,
|
||||
)?;
|
||||
|
||||
Ok(vec![buf.get_signed_bounds()])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ivy")]
|
||||
fn calc(s: &str) -> Result<String, String> {
|
||||
libivy::eval(s)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ivy"))]
|
||||
fn calc(_s: &str) -> Result<String, String> {
|
||||
Err("ivy not enabled".to_string())
|
||||
}
|
||||
|
||||
impl ModuleImpl for Launcher {
|
||||
fn draw(
|
||||
&self,
|
||||
@@ -185,7 +188,7 @@ impl ModuleImpl for Launcher {
|
||||
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
|
||||
buf.memset(bg);
|
||||
|
||||
match self.cur.chars().next() {
|
||||
match self.input.chars().next() {
|
||||
Some('=') => self.draw_calc(buf, bg),
|
||||
Some('!') => self.draw_shell(buf, bg),
|
||||
_ => self.draw_launcher(buf, bg),
|
||||
@@ -196,14 +199,21 @@ impl ModuleImpl for Launcher {
|
||||
|
||||
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
|
||||
if self.dirty || force {
|
||||
match self.cur.chars().next() {
|
||||
Some('=') => (),
|
||||
match self.input.chars().next() {
|
||||
Some('=') => {
|
||||
if self.input.len() > 1 {
|
||||
match calc(&self.input.chars().skip(1).collect::<String>()) {
|
||||
Ok(v) => self.result = Some(v),
|
||||
Err(_) => self.result = None,
|
||||
}
|
||||
}
|
||||
},
|
||||
Some('!') => (),
|
||||
_ => {
|
||||
let mut m = self
|
||||
.options
|
||||
.iter()
|
||||
.map(|x| (fuzzy_match(&x.to_lowercase(), &self.cur.to_lowercase()), x))
|
||||
.map(|x| (fuzzy_match(&x.to_lowercase(), &self.input.to_lowercase()), x))
|
||||
.filter(|(x, _)| x.is_some())
|
||||
.map(|(x, y)| (x.unwrap(), y.to_string()))
|
||||
.collect::<Vec<(i64, String)>>();
|
||||
@@ -237,17 +247,18 @@ impl ModuleImpl for Launcher {
|
||||
match input {
|
||||
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();
|
||||
if self.input.len() > 0 {
|
||||
self.input = self.input[..self.input.len() - 1].to_string();
|
||||
self.offset = 0;
|
||||
self.result = None;
|
||||
self.dirty = true
|
||||
}
|
||||
}
|
||||
keysyms::XKB_KEY_Return => {
|
||||
match self.cur.chars().next() {
|
||||
match self.input.chars().next() {
|
||||
Some('=') => (),
|
||||
Some('!') => {
|
||||
println!("{}", self.cur.chars().skip(1).collect::<String>());
|
||||
println!("{}", self.input.chars().skip(1).collect::<String>());
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => if self.matches.len() > self.offset {
|
||||
@@ -270,9 +281,10 @@ impl ModuleImpl for Launcher {
|
||||
}
|
||||
_ => match interpreted {
|
||||
Some(v) => {
|
||||
self.cur += &v;
|
||||
self.input += &v;
|
||||
self.offset = 0;
|
||||
self.dirty = true
|
||||
self.result = None;
|
||||
self.dirty = true;
|
||||
}
|
||||
None => {}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user