monitoring windows and workspaces are functional
This commit is contained in:
parent
9667aae9e2
commit
461d59ec0e
7 changed files with 105 additions and 95 deletions
|
@ -9,7 +9,7 @@ pub struct WindowIcon {
|
||||||
pub icon: String,
|
pub icon: String,
|
||||||
pub substring: String
|
pub substring: String
|
||||||
}
|
}
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub title_length: usize,
|
pub title_length: usize,
|
||||||
pub window_icons: Vec<WindowIcon>
|
pub window_icons: Vec<WindowIcon>
|
||||||
|
@ -20,7 +20,7 @@ pub fn parse_config() -> Config {
|
||||||
let config_path = xdg_dirs
|
let config_path = xdg_dirs
|
||||||
.place_config_file("config.toml")
|
.place_config_file("config.toml")
|
||||||
.expect("cannot create configuration directory");
|
.expect("cannot create configuration directory");
|
||||||
println!("{}",config_path.exists());
|
// println!("{}",config_path.exists());
|
||||||
if !config_path.exists() {
|
if !config_path.exists() {
|
||||||
let _ = File::create(&config_path);
|
let _ = File::create(&config_path);
|
||||||
// let config_file = File::create(&config_path);
|
// let config_file = File::create(&config_path);
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
use std::{fmt::Write};
|
|
||||||
use swayipc::WindowEvent;
|
|
||||||
use crate::config::WindowIcon;
|
|
||||||
|
|
||||||
pub fn get_window_title(title_length: usize, window_event: WindowEvent, window_icons: Vec<WindowIcon>) -> String {
|
|
||||||
let mut window_title = window_event.container.name.unwrap().trim_end().to_string();
|
|
||||||
for pair in window_icons {
|
|
||||||
if window_title.contains(&pair.substring) {
|
|
||||||
window_title = pair.icon + " " + &window_title.replace(&pair.substring, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
if window_title.len().gt(&title_length) {
|
|
||||||
window_title.truncate(title_length);
|
|
||||||
let _ = window_title.write_char('…');
|
|
||||||
}
|
|
||||||
window_title
|
|
||||||
}
|
|
|
@ -1,10 +0,0 @@
|
||||||
use swayipc::WorkspaceEvent;
|
|
||||||
|
|
||||||
pub fn get_workspace_name(workspace_event: WorkspaceEvent) -> String {
|
|
||||||
workspace_event.current.unwrap().name.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_workspace_list(workspace_event: WorkspaceEvent) -> String {
|
|
||||||
println!("{:?}",workspace_event.change);
|
|
||||||
"blah".to_string()
|
|
||||||
}
|
|
12
src/lib/results.rs
Normal file
12
src/lib/results.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
pub struct WindowInfo {
|
||||||
|
pub title: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
pub struct WorkspaceInfo {
|
||||||
|
pub num: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub is_focused: bool,
|
||||||
|
pub position: char
|
||||||
|
}
|
24
src/lib/windows.rs
Normal file
24
src/lib/windows.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
use std::{fmt::Write};
|
||||||
|
use serde_json::json;
|
||||||
|
use swayipc::Node;
|
||||||
|
use crate::{config::Config, Cli};
|
||||||
|
|
||||||
|
//#[path = "../results.rs"]
|
||||||
|
mod results;
|
||||||
|
use results::WindowInfo;
|
||||||
|
|
||||||
|
pub fn print_window_title(window_node: Node,cli: &Cli,config: &Config) {
|
||||||
|
let mut window_title_display: String = window_node.name.unwrap();
|
||||||
|
for pair in &config.window_icons {
|
||||||
|
if window_title_display.contains(&pair.substring) {
|
||||||
|
window_title_display = pair.icon.clone() + " " + &window_title_display.replace(&pair.substring, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !cli.no_truncate_title.unwrap() && window_title_display.len().gt(&config.title_length) {
|
||||||
|
window_title_display.truncate(config.title_length);
|
||||||
|
let _ = window_title_display.write_char('…');
|
||||||
|
}
|
||||||
|
let window_info = WindowInfo { title: window_title_display };
|
||||||
|
let window_output = json!(window_info);
|
||||||
|
println!("{}",window_output)
|
||||||
|
}
|
24
src/lib/workspaces.rs
Normal file
24
src/lib/workspaces.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
use serde_json::json;
|
||||||
|
use swayipc::Workspace;
|
||||||
|
|
||||||
|
//#[path = "../results.rs"]
|
||||||
|
mod results;
|
||||||
|
use results::WorkspaceInfo;
|
||||||
|
|
||||||
|
pub fn print_workspace_array(workspaces: Vec<Workspace>) {
|
||||||
|
let current_ws = workspaces.iter().find(|&x| x.focused).unwrap().num;
|
||||||
|
let mut workspaces_info: Vec<WorkspaceInfo> = vec![];
|
||||||
|
for workspace in workspaces {
|
||||||
|
let mut ws_position = 'c';
|
||||||
|
if current_ws.lt(&workspace.num) {
|
||||||
|
ws_position = 'r';
|
||||||
|
} else if current_ws.gt(&workspace.num){
|
||||||
|
ws_position = 'l';
|
||||||
|
}
|
||||||
|
let workspace_info = WorkspaceInfo { num: workspace.num, name: workspace.name, is_focused: workspace.focused, position: ws_position };
|
||||||
|
workspaces_info.push(workspace_info);
|
||||||
|
}
|
||||||
|
workspaces_info.sort_by(|a, b| a.num.cmp(&b.num));
|
||||||
|
let workspace_output = json!(workspaces_info);
|
||||||
|
println!("{}",workspace_output)
|
||||||
|
}
|
104
src/main.rs
104
src/main.rs
|
@ -1,15 +1,16 @@
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
use clap::{Parser,Subcommand};
|
use clap::{Parser,Subcommand,ArgAction};
|
||||||
use swayipc::{Connection, Event, EventType, Fallible};
|
use swayipc::{Connection, Event, EventType, Fallible};
|
||||||
|
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
#[path = "lib/monitor/windows.rs"]
|
#[path = "lib/windows.rs"]
|
||||||
mod windows;
|
mod windows;
|
||||||
use windows::get_window_title;
|
use windows::print_window_title;
|
||||||
#[path = "lib/monitor/workspaces.rs"]
|
#[path = "lib/workspaces.rs"]
|
||||||
mod workspaces;
|
mod workspaces;
|
||||||
use workspaces::get_workspace_name;
|
use workspaces::print_workspace_array;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
mod monitor;
|
mod monitor;
|
||||||
|
@ -17,17 +18,21 @@ mod monitor;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Cli {
|
pub struct Cli {
|
||||||
/// Optional name to operate on
|
/// Disable truncation of window titles
|
||||||
#[arg(short, long)]
|
#[arg(short = 'T', long = "no-truncate-title", action = ArgAction::SetTrue)]
|
||||||
name: Option<String>,
|
no_truncate_title: Option<bool>,
|
||||||
|
|
||||||
|
/// Enables monitoring for supported event types
|
||||||
|
#[arg(short = 'm', long = "monitor", action = ArgAction::SetTrue)]
|
||||||
|
monitor: Option<bool>,
|
||||||
|
|
||||||
// /// Sets a custom config file
|
// /// Sets a custom config file
|
||||||
// #[arg(short, long, value_name = "FILE")]
|
// #[arg(short, long, value_name = "FILE")]
|
||||||
// config: Option<PathBuf>,
|
// config: Option<PathBuf>,
|
||||||
|
|
||||||
/// Turn debugging information on
|
/// Turn debugging information on
|
||||||
#[arg(short, long, action = clap::ArgAction::Count)]
|
#[arg(short, long, action = ArgAction::Count)]
|
||||||
debug: u8,
|
debug: u8,
|
||||||
|
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
|
@ -37,22 +42,24 @@ struct Cli {
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
/// Prints the Currently Active Window Title
|
/// Prints the Currently Active Window Title
|
||||||
WindowTitle,
|
Windows,
|
||||||
//
|
// Prints the Currently Active Workspace layout
|
||||||
|
Workspaces,
|
||||||
|
// Launch Program with Current Profile's Configuration
|
||||||
Launch,
|
Launch,
|
||||||
Lock,
|
Lock,
|
||||||
Rename,
|
Rename,
|
||||||
Profile,
|
Profile,
|
||||||
Shortcuts {
|
Shortcuts {
|
||||||
#[arg(short, long, action = clap::ArgAction::Count)]
|
#[arg(short, long, action = ArgAction::SetTrue)]
|
||||||
global: u8,
|
global: Option<bool>,
|
||||||
},
|
},
|
||||||
Monitor {
|
// Monitor {
|
||||||
/// monitor a sway activity type
|
// /// monitor a sway activity type
|
||||||
// #[arg(short, long)]
|
// #[arg(short, long)]
|
||||||
#[command(subcommand)]
|
// #[command(subcommand)]
|
||||||
monitor_type: MonitorTypes,
|
// monitor_type: MonitorTypes,
|
||||||
},
|
// },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
|
@ -68,29 +75,21 @@ enum MonitorTypes {
|
||||||
fn main() -> Fallible<()> {
|
fn main() -> Fallible<()> {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
let config = config::parse_config();
|
let config = config::parse_config();
|
||||||
// std::process::exit(0);
|
if let Some(no_truncate_title) = cli.no_truncate_title {
|
||||||
if let Some(name) = cli.name.as_deref() {
|
println!("Value for no_truncate_title: {no_truncate_title}");
|
||||||
println!("Value for name: {name}");
|
|
||||||
}
|
}
|
||||||
|
let mut sway_connection = Connection::new()?;
|
||||||
match &cli.command {
|
match &cli.command {
|
||||||
Commands::WindowTitle => {
|
Commands::Windows => {
|
||||||
println!("Print window title");
|
print_window_title(sway_connection.get_tree().iter().find(|&x | x.focused).unwrap().clone(), &cli, &config);
|
||||||
let sway_connection = Connection::new()?;
|
if cli.monitor.unwrap() {
|
||||||
sway_connection.get_tree().unwrap();
|
let _ = monitor_events(EventType::Window, cli, config);
|
||||||
// println!("{:?}", sway_connection.get_tree().map(op))
|
|
||||||
}
|
}
|
||||||
Commands::Monitor { monitor_type } => {
|
|
||||||
match monitor_type {
|
|
||||||
MonitorTypes::Workspaces => {
|
|
||||||
println!("monitoring workspace changes!")
|
|
||||||
}
|
|
||||||
MonitorTypes::Windows => {
|
|
||||||
println!("monitoring window changes!");
|
|
||||||
monitor_events(EventType::Window, config);
|
|
||||||
}
|
|
||||||
MonitorTypes::Profile => {
|
|
||||||
println!("monitoring profile changes!")
|
|
||||||
}
|
}
|
||||||
|
Commands::Workspaces => {
|
||||||
|
print_workspace_array(sway_connection.get_workspaces().unwrap());
|
||||||
|
if cli.monitor.unwrap() {
|
||||||
|
let _ = monitor_events(EventType::Workspace, cli, config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Commands::Launch => todo!(),
|
Commands::Launch => todo!(),
|
||||||
|
@ -102,7 +101,7 @@ fn main() -> Fallible<()> {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn monitor_events(event_type: EventType, config: Config) -> Fallible<()> {
|
pub fn monitor_events(event_type: EventType, cli: Cli, config: Config) -> Fallible<()> {
|
||||||
/* let subs = [
|
/* let subs = [
|
||||||
// Valid EventTypes: Workspace, Output, Input, Tick, Shutdown, Mode, Window, BarStateUpdate, BarConfigUpdate, Binding
|
// Valid EventTypes: Workspace, Output, Input, Tick, Shutdown, Mode, Window, BarStateUpdate, BarConfigUpdate, Binding
|
||||||
//EventType::Workspace,
|
//EventType::Workspace,
|
||||||
|
@ -111,35 +110,14 @@ pub fn monitor_events(event_type: EventType, config: Config) -> Fallible<()> {
|
||||||
event_type
|
event_type
|
||||||
]; */
|
]; */
|
||||||
let sway_connection = Connection::new()?;
|
let sway_connection = Connection::new()?;
|
||||||
|
|
||||||
// for event in Connection::new()?.subscribe(subs)? {
|
|
||||||
for event in sway_connection.subscribe([event_type])? {
|
for event in sway_connection.subscribe([event_type])? {
|
||||||
let e = event?;
|
let e = event?;
|
||||||
// println!("{:?}\n", e);
|
|
||||||
match e {
|
match e {
|
||||||
Event::Window(w) => {
|
Event::Window(w) => {
|
||||||
// println!("{}",config.window_icons[0].icon);
|
print_window_title(w.container, &cli, &config);
|
||||||
println!("{}", get_window_title(config.title_length, *w, config.window_icons.clone()));
|
},
|
||||||
// println!("{:?}",w.container.nodes);
|
|
||||||
// println!("{:?}",Connection::get_workspaces(&mut Connection::new().unwrap()).unwrap());
|
|
||||||
|
|
||||||
},
|
|
||||||
Event::Workspace(w) => {
|
Event::Workspace(w) => {
|
||||||
// println!("{}", get_workspace_name(*w));
|
print_workspace_array(self::Connection::get_workspaces(&mut self::Connection::new().unwrap()).unwrap());
|
||||||
// println!("{:?}",w.change);
|
|
||||||
let mut sway_con = self::Connection::new()?;
|
|
||||||
for workspace in Connection::get_workspaces(&mut sway_con)? {
|
|
||||||
// println!("{}",workspace.name);
|
|
||||||
let current_workspace_num = w.current.as_ref().unwrap().num.unwrap();
|
|
||||||
if workspace.num.lt(¤t_workspace_num) {
|
|
||||||
println!("left: {} {}",workspace.num % 10, workspace.name)
|
|
||||||
} else if workspace.focused {
|
|
||||||
println!("focused: {}",workspace.num % 10)
|
|
||||||
} else if workspace.num.gt(¤t_workspace_num) {
|
|
||||||
println!("right: {} {}",workspace.num % 10, workspace.name)
|
|
||||||
}
|
|
||||||
// println!("{}",workspace.focused);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
Event::Tick(w) => {
|
Event::Tick(w) => {
|
||||||
println!("{}",w.payload);
|
println!("{}",w.payload);
|
||||||
|
|
Loading…
Add table
Reference in a new issue