-
-
Save rust-play/e3ae3f729bb741238c83dbdb23096dff to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use dictcc::dictcc; | |
use futures_lite::{AsyncWrite, AsyncWriteExt, StreamExt}; | |
use pop_launcher::{ | |
async_stdin, async_stdout, json_input_stream, PluginResponse, PluginSearchResult, Request, | |
}; | |
use smol::Unblock; | |
pub async fn send<W: AsyncWrite + Unpin>(tx: &mut W, response: PluginResponse) { | |
// convert response to json then add "\n" and convert them to bytes and write them to stdout (? not sure) | |
if let Ok(mut bytes) = serde_json::to_string(&response) { | |
bytes.push('\n'); | |
let _ = tx.write_all(bytes.as_bytes()).await; | |
} | |
} | |
#[tokio::main] | |
pub async fn main() { | |
let mut requests = json_input_stream(async_stdin()); | |
let mut app = App::default(); | |
while let Some(result) = requests.next().await { | |
match result { | |
Ok(request) => match request { | |
Request::Activate(id) => app.activate(id).await, | |
Request::Search(query) => app.search(query).await, | |
Request::Exit => break, | |
_ => (), | |
}, | |
Err(why) => eprintln!("{}", why), | |
} | |
} | |
} | |
struct App { | |
definitions: Vec<String>, | |
out: Unblock<io::Stdout>, | |
} | |
impl Default for App { | |
fn default() -> Self { | |
Self { | |
definitions: vec![], | |
out: async_stdout(), | |
} | |
} | |
} | |
impl App { | |
// When we move over the options of the launcher, it gets the id of the | |
// option and then the id is used to get definition | |
pub async fn activate(&mut self, id: u32) { | |
let mut definition = String::new(); | |
if let Some(def) = self.definitions.get(id as usize) { | |
eprintln!("Got definition: {}", &def); | |
definition = def.clone(); | |
} | |
send(&mut self.out, PluginResponse::Fill(definition)).await | |
} | |
pub async fn search(&mut self, query: String) { | |
self.definitions.clear(); | |
let query_vec = query | |
.split(' ') | |
.map(|x| x.to_string()) | |
.collect::<Vec<String>>(); | |
let mut translate_from = String::new(); | |
let mut translate_to = String::new(); | |
let mut word = String::new(); | |
query.split(' ').enumerate().for_each(|(index, x)| { | |
if x == "-f" || x == "--from" { | |
translate_from = query_vec[index + 1].clone(); | |
} else if x == "-t" || x == "--to" { | |
translate_to = query_vec[index + 1].clone(); | |
} else { | |
word = query_vec[index].clone(); | |
} | |
}); | |
let definitions = dictcc::translate(&translate_from, &translate_to, &word); | |
if let Err(err) = definitions { | |
eprintln!("Something went wrong...\nError: {}", err,) | |
} else { | |
let definitions = definitions.unwrap(); | |
self.definitions = definitions.clone(); | |
for (index, definition) in definitions.into_iter().enumerate() { | |
send( | |
&mut self.out, | |
PluginResponse::Append(PluginSearchResult { | |
id: index as u32, | |
name: definition, | |
description: format!("{} -> {}", translate_from, translate_to), | |
..Default::default() | |
}), | |
) | |
.await; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment