Move CLI types to submodule
Browse files at this commite68641c0a773c4c79591545cfabb549c69ece45dParent: 2a892ef5 changed files +66 -57
- src/cli.rs +49 -0
- src/ipc/client.rs +1 -1
- src/lib.rs +1 -6
- src/main.rs +6 -50
- src/utils.rs +9 -0
src/cli.rsadded+49 -0
| @@ -0,0 +1,49 @@ | |||
| 1 | +use std::ffi::OsString; | ||
| 2 | +use std::path::PathBuf; | ||
| 3 | + | ||
| 4 | +use clap::{Parser, Subcommand}; | ||
| 5 | + | ||
| 6 | +use crate::utils::version; | ||
| 7 | + | ||
| 8 | +#[derive(Parser)] | ||
| 9 | +#[command(author, version = version(), about, long_about = None)] | ||
| 10 | +#[command(args_conflicts_with_subcommands = true)] | ||
| 11 | +#[command(subcommand_value_name = "SUBCOMMAND")] | ||
| 12 | +#[command(subcommand_help_heading = "Subcommands")] | ||
| 13 | +pub struct Cli { | ||
| 14 | + /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). | ||
| 15 | + #[arg(short, long)] | ||
| 16 | + pub config: Option<PathBuf>, | ||
| 17 | + /// Command to run upon compositor startup. | ||
| 18 | + #[arg(last = true)] | ||
| 19 | + pub command: Vec<OsString>, | ||
| 20 | + | ||
| 21 | + #[command(subcommand)] | ||
| 22 | + pub subcommand: Option<Sub>, | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +#[derive(Subcommand)] | ||
| 26 | +pub enum Sub { | ||
| 27 | + /// Validate the config file. | ||
| 28 | + Validate { | ||
| 29 | + /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). | ||
| 30 | + #[arg(short, long)] | ||
| 31 | + config: Option<PathBuf>, | ||
| 32 | + }, | ||
| 33 | + /// Communicate with the running niri instance. | ||
| 34 | + Msg { | ||
| 35 | + #[command(subcommand)] | ||
| 36 | + msg: Msg, | ||
| 37 | + /// Format output as JSON. | ||
| 38 | + #[arg(short, long)] | ||
| 39 | + json: bool, | ||
| 40 | + }, | ||
| 41 | + /// Cause a panic to check if the backtraces are good. | ||
| 42 | + Panic, | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +#[derive(Subcommand)] | ||
| 46 | +pub enum Msg { | ||
| 47 | + /// List connected outputs. | ||
| 48 | + Outputs, | ||
| 49 | +} | ||
src/ipc/client.rsmodified+1 -1
| @@ -6,7 +6,7 @@ use std::os::unix::net::UnixStream; | |||
| 6 | use anyhow::{bail, Context}; | 6 | use anyhow::{bail, Context}; |
| 7 | use niri_ipc::{Mode, Output, Request, Response}; | 7 | use niri_ipc::{Mode, Output, Request, Response}; |
| 8 | 8 | ||
| 9 | -use crate::Msg; | 9 | +use crate::cli::Msg; |
| 10 | 10 | ||
| 11 | pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | 11 | pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { |
| 12 | let socket_path = env::var_os(niri_ipc::SOCKET_PATH_ENV).with_context(|| { | 12 | let socket_path = env::var_os(niri_ipc::SOCKET_PATH_ENV).with_context(|| { |
src/lib.rsmodified+1 -6
| @@ -3,6 +3,7 @@ extern crate tracing; | |||
| 3 | 3 | ||
| 4 | pub mod animation; | 4 | pub mod animation; |
| 5 | pub mod backend; | 5 | pub mod backend; |
| 6 | +pub mod cli; | ||
| 6 | pub mod config_error_notification; | 7 | pub mod config_error_notification; |
| 7 | pub mod cursor; | 8 | pub mod cursor; |
| 8 | #[cfg(feature = "dbus")] | 9 | #[cfg(feature = "dbus")] |
| @@ -28,9 +29,3 @@ pub mod pw_utils; | |||
| 28 | 29 | ||
| 29 | #[cfg(not(feature = "xdp-gnome-screencast"))] | 30 | #[cfg(not(feature = "xdp-gnome-screencast"))] |
| 30 | pub use dummy_pw_utils as pw_utils; | 31 | pub use dummy_pw_utils as pw_utils; |
| 31 | - | ||
| 32 | -#[derive(clap::Subcommand)] | ||
| 33 | -pub enum Msg { | ||
| 34 | - /// List connected outputs. | ||
| 35 | - Outputs, | ||
| 36 | -} | ||
src/main.rsmodified+6 -50
| @@ -1,23 +1,24 @@ | |||
| 1 | #[macro_use] | 1 | #[macro_use] |
| 2 | extern crate tracing; | 2 | extern crate tracing; |
| 3 | 3 | ||
| 4 | -use std::ffi::OsString; | ||
| 5 | use std::fs::{self, File}; | 4 | use std::fs::{self, File}; |
| 6 | use std::io::{self, Write}; | 5 | use std::io::{self, Write}; |
| 7 | use std::path::PathBuf; | 6 | use std::path::PathBuf; |
| 8 | use std::process::Command; | 7 | use std::process::Command; |
| 9 | use std::{env, mem}; | 8 | use std::{env, mem}; |
| 10 | 9 | ||
| 11 | -use clap::{Parser, Subcommand}; | 10 | +use clap::Parser; |
| 12 | use directories::ProjectDirs; | 11 | use directories::ProjectDirs; |
| 13 | -use git_version::git_version; | 12 | +use niri::animation; |
| 13 | +use niri::cli::{Cli, Sub}; | ||
| 14 | #[cfg(feature = "dbus")] | 14 | #[cfg(feature = "dbus")] |
| 15 | use niri::dbus; | 15 | use niri::dbus; |
| 16 | use niri::ipc::client::handle_msg; | 16 | use niri::ipc::client::handle_msg; |
| 17 | use niri::niri::State; | 17 | use niri::niri::State; |
| 18 | -use niri::utils::{cause_panic, spawn, REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE}; | 18 | +use niri::utils::{ |
| 19 | + cause_panic, spawn, version, REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE, | ||
| 20 | +}; | ||
| 19 | use niri::watcher::Watcher; | 21 | use niri::watcher::Watcher; |
| 20 | -use niri::{animation, Msg}; | ||
| 21 | use niri_config::Config; | 22 | use niri_config::Config; |
| 22 | use portable_atomic::Ordering; | 23 | use portable_atomic::Ordering; |
| 23 | use sd_notify::NotifyState; | 24 | use sd_notify::NotifyState; |
| @@ -25,43 +26,6 @@ use smithay::reexports::calloop::{self, EventLoop}; | |||
| 25 | use smithay::reexports::wayland_server::Display; | 26 | use smithay::reexports::wayland_server::Display; |
| 26 | use tracing_subscriber::EnvFilter; | 27 | use tracing_subscriber::EnvFilter; |
| 27 | 28 | ||
| 28 | -#[derive(Parser)] | ||
| 29 | -#[command(author, version = version(), about, long_about = None)] | ||
| 30 | -#[command(args_conflicts_with_subcommands = true)] | ||
| 31 | -#[command(subcommand_value_name = "SUBCOMMAND")] | ||
| 32 | -#[command(subcommand_help_heading = "Subcommands")] | ||
| 33 | -struct Cli { | ||
| 34 | - /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). | ||
| 35 | - #[arg(short, long)] | ||
| 36 | - config: Option<PathBuf>, | ||
| 37 | - /// Command to run upon compositor startup. | ||
| 38 | - #[arg(last = true)] | ||
| 39 | - command: Vec<OsString>, | ||
| 40 | - | ||
| 41 | - #[command(subcommand)] | ||
| 42 | - subcommand: Option<Sub>, | ||
| 43 | -} | ||
| 44 | - | ||
| 45 | -#[derive(Subcommand)] | ||
| 46 | -enum Sub { | ||
| 47 | - /// Validate the config file. | ||
| 48 | - Validate { | ||
| 49 | - /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). | ||
| 50 | - #[arg(short, long)] | ||
| 51 | - config: Option<PathBuf>, | ||
| 52 | - }, | ||
| 53 | - /// Communicate with the running niri instance. | ||
| 54 | - Msg { | ||
| 55 | - #[command(subcommand)] | ||
| 56 | - msg: Msg, | ||
| 57 | - /// Format output as JSON. | ||
| 58 | - #[arg(short, long)] | ||
| 59 | - json: bool, | ||
| 60 | - }, | ||
| 61 | - /// Cause a panic to check if the backtraces are good. | ||
| 62 | - Panic, | ||
| 63 | -} | ||
| 64 | - | ||
| 65 | fn main() -> Result<(), Box<dyn std::error::Error>> { | 29 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 66 | // Set backtrace defaults if not set. | 30 | // Set backtrace defaults if not set. |
| 67 | if env::var_os("RUST_BACKTRACE").is_none() { | 31 | if env::var_os("RUST_BACKTRACE").is_none() { |
| @@ -270,14 +234,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 270 | Ok(()) | 234 | Ok(()) |
| 271 | } | 235 | } |
| 272 | 236 | ||
| 273 | -fn version() -> String { | ||
| 274 | - format!( | ||
| 275 | - "{} ({})", | ||
| 276 | - env!("CARGO_PKG_VERSION"), | ||
| 277 | - git_version!(fallback = "unknown commit"), | ||
| 278 | - ) | ||
| 279 | -} | ||
| 280 | - | ||
| 281 | fn import_env_to_systemd() { | 237 | fn import_env_to_systemd() { |
| 282 | let rv = Command::new("/bin/sh") | 238 | let rv = Command::new("/bin/sh") |
| 283 | .args([ | 239 | .args([ |
src/utils.rsmodified+9 -0
| @@ -11,6 +11,7 @@ use std::time::Duration; | |||
| 11 | 11 | ||
| 12 | use anyhow::{ensure, Context}; | 12 | use anyhow::{ensure, Context}; |
| 13 | use directories::UserDirs; | 13 | use directories::UserDirs; |
| 14 | +use git_version::git_version; | ||
| 14 | use niri_config::Config; | 15 | use niri_config::Config; |
| 15 | use smithay::output::Output; | 16 | use smithay::output::Output; |
| 16 | use smithay::reexports::rustix::time::{clock_gettime, ClockId}; | 17 | use smithay::reexports::rustix::time::{clock_gettime, ClockId}; |
| @@ -20,6 +21,14 @@ pub fn clone2<T: Clone, U: Clone>(t: (&T, &U)) -> (T, U) { | |||
| 20 | (t.0.clone(), t.1.clone()) | 21 | (t.0.clone(), t.1.clone()) |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 24 | +pub fn version() -> String { | ||
| 25 | + format!( | ||
| 26 | + "{} ({})", | ||
| 27 | + env!("CARGO_PKG_VERSION"), | ||
| 28 | + git_version!(fallback = "unknown commit"), | ||
| 29 | + ) | ||
| 30 | +} | ||
| 31 | + | ||
| 23 | pub fn get_monotonic_time() -> Duration { | 32 | pub fn get_monotonic_time() -> Duration { |
| 24 | let ts = clock_gettime(ClockId::Monotonic); | 33 | let ts = clock_gettime(ClockId::Monotonic); |
| 25 | Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32) | 34 | Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32) |