sway-launchpad

A small rust script to switch sway workspaces using a Launchpad S
git clone git://archive.git.mtrnord.blog/MTRNord/sway-launchpad.git
Log | Files | Refs | LICENSE

main.rs (1763B)


      1 use color_eyre::Result;
      2 use futures_util::StreamExt;
      3 use std::os::unix::fs::PermissionsExt;
      4 use std::path::Path;
      5 use std::{env, fs};
      6 use tokio::io::{AsyncBufReadExt, BufStream};
      7 use tokio::net::UnixListener;
      8 use tracing::*;
      9 use users::get_current_uid;
     10 
     11 #[tokio::main]
     12 async fn main() -> Result<()> {
     13     // Enable the logging crates
     14     color_eyre::install()?;
     15     if let Err(_) = env::var("RUST_LOG") {
     16         env::set_var("RUST_LOG", "INFO");
     17     }
     18     tracing_subscriber::fmt::init();
     19     let user_id = get_current_uid();
     20     let path_string = format!("/run/user/{}/lsc_example_plugin.sock", user_id);
     21     let socket_path = Path::new(&path_string);
     22     if socket_path.exists() {
     23         fs::remove_file(socket_path)?;
     24     }
     25 
     26     // First we create a unix socket. This is required to be a) The same as the crate name and b) to be in /run
     27     let mut listener = UnixListener::bind(socket_path).unwrap();
     28 
     29     let mut perms = fs::metadata(socket_path)?.permissions();
     30     perms.set_mode(0o666);
     31     fs::set_permissions(socket_path, perms)?;
     32     // This listens for new connections
     33     while let Some(stream) = listener.next().await {
     34         match stream {
     35             Ok(stream) => {
     36                 tokio::spawn(async move {
     37                     println!("new client!");
     38                     let buffer = BufStream::new(stream);
     39                     let mut lines = buffer.lines();
     40                     while let Ok(line) = lines.next_line().await {
     41                         if let Some(line) = line {
     42                             println!("{}", line);
     43                         }
     44                     }
     45                 });
     46             }
     47             Err(e) => {
     48                 error!("Unix socket connection failed: {}", e);
     49             }
     50         }
     51     }
     52     Ok(())
     53 }