aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Molodetskikh <yalterz@gmail.com>2024-02-10 08:40:13 +0400
committerIvan Molodetskikh <yalterz@gmail.com>2024-02-10 08:40:13 +0400
commite68641c0a773c4c79591545cfabb549c69ece45d (patch)
tree518e519584834b0ea8e1b429b9b1c2a2d0d1f93a
parent2a892ef5116999d17c7e87e4f915219d45bf1219 (diff)
downloadniri-e68641c0a773c4c79591545cfabb549c69ece45d.tar.gz
niri-e68641c0a773c4c79591545cfabb549c69ece45d.tar.bz2
niri-e68641c0a773c4c79591545cfabb549c69ece45d.zip
Move CLI types to submodule
-rw-r--r--src/cli.rs49
-rw-r--r--src/ipc/client.rs2
-rw-r--r--src/lib.rs7
-rw-r--r--src/main.rs56
-rw-r--r--src/utils.rs9
5 files changed, 66 insertions, 57 deletions
diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644
index 00000000..8dd8927d
--- /dev/null
+++ b/src/cli.rs
@@ -0,0 +1,49 @@
+use std::ffi::OsString;
+use std::path::PathBuf;
+
+use clap::{Parser, Subcommand};
+
+use crate::utils::version;
+
+#[derive(Parser)]
+#[command(author, version = version(), about, long_about = None)]
+#[command(args_conflicts_with_subcommands = true)]
+#[command(subcommand_value_name = "SUBCOMMAND")]
+#[command(subcommand_help_heading = "Subcommands")]
+pub struct Cli {
+ /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
+ #[arg(short, long)]
+ pub config: Option<PathBuf>,
+ /// Command to run upon compositor startup.
+ #[arg(last = true)]
+ pub command: Vec<OsString>,
+
+ #[command(subcommand)]
+ pub subcommand: Option<Sub>,
+}
+
+#[derive(Subcommand)]
+pub enum Sub {
+ /// Validate the config file.
+ Validate {
+ /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
+ #[arg(short, long)]
+ config: Option<PathBuf>,
+ },
+ /// Communicate with the running niri instance.
+ Msg {
+ #[command(subcommand)]
+ msg: Msg,
+ /// Format output as JSON.
+ #[arg(short, long)]
+ json: bool,
+ },
+ /// Cause a panic to check if the backtraces are good.
+ Panic,
+}
+
+#[derive(Subcommand)]
+pub enum Msg {
+ /// List connected outputs.
+ Outputs,
+}
diff --git a/src/ipc/client.rs b/src/ipc/client.rs
index c09ef84e..7754187a 100644
--- a/src/ipc/client.rs
+++ b/src/ipc/client.rs
@@ -6,7 +6,7 @@ use std::os::unix::net::UnixStream;
use anyhow::{bail, Context};
use niri_ipc::{Mode, Output, Request, Response};
-use crate::Msg;
+use crate::cli::Msg;
pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
let socket_path = env::var_os(niri_ipc::SOCKET_PATH_ENV).with_context(|| {
diff --git a/src/lib.rs b/src/lib.rs
index 6a459378..b332dc5e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,6 +3,7 @@ extern crate tracing;
pub mod animation;
pub mod backend;
+pub mod cli;
pub mod config_error_notification;
pub mod cursor;
#[cfg(feature = "dbus")]
@@ -28,9 +29,3 @@ pub mod pw_utils;
#[cfg(not(feature = "xdp-gnome-screencast"))]
pub use dummy_pw_utils as pw_utils;
-
-#[derive(clap::Subcommand)]
-pub enum Msg {
- /// List connected outputs.
- Outputs,
-}
diff --git a/src/main.rs b/src/main.rs
index c7a7b475..ed9ac6d4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,23 +1,24 @@
#[macro_use]
extern crate tracing;
-use std::ffi::OsString;
use std::fs::{self, File};
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::Command;
use std::{env, mem};
-use clap::{Parser, Subcommand};
+use clap::Parser;
use directories::ProjectDirs;
-use git_version::git_version;
+use niri::animation;
+use niri::cli::{Cli, Sub};
#[cfg(feature = "dbus")]
use niri::dbus;
use niri::ipc::client::handle_msg;
use niri::niri::State;
-use niri::utils::{cause_panic, spawn, REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE};
+use niri::utils::{
+ cause_panic, spawn, version, REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE,
+};
use niri::watcher::Watcher;
-use niri::{animation, Msg};
use niri_config::Config;
use portable_atomic::Ordering;
use sd_notify::NotifyState;
@@ -25,43 +26,6 @@ use smithay::reexports::calloop::{self, EventLoop};
use smithay::reexports::wayland_server::Display;
use tracing_subscriber::EnvFilter;
-#[derive(Parser)]
-#[command(author, version = version(), about, long_about = None)]
-#[command(args_conflicts_with_subcommands = true)]
-#[command(subcommand_value_name = "SUBCOMMAND")]
-#[command(subcommand_help_heading = "Subcommands")]
-struct Cli {
- /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
- #[arg(short, long)]
- config: Option<PathBuf>,
- /// Command to run upon compositor startup.
- #[arg(last = true)]
- command: Vec<OsString>,
-
- #[command(subcommand)]
- subcommand: Option<Sub>,
-}
-
-#[derive(Subcommand)]
-enum Sub {
- /// Validate the config file.
- Validate {
- /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
- #[arg(short, long)]
- config: Option<PathBuf>,
- },
- /// Communicate with the running niri instance.
- Msg {
- #[command(subcommand)]
- msg: Msg,
- /// Format output as JSON.
- #[arg(short, long)]
- json: bool,
- },
- /// Cause a panic to check if the backtraces are good.
- Panic,
-}
-
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set backtrace defaults if not set.
if env::var_os("RUST_BACKTRACE").is_none() {
@@ -270,14 +234,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
-fn version() -> String {
- format!(
- "{} ({})",
- env!("CARGO_PKG_VERSION"),
- git_version!(fallback = "unknown commit"),
- )
-}
-
fn import_env_to_systemd() {
let rv = Command::new("/bin/sh")
.args([
diff --git a/src/utils.rs b/src/utils.rs
index 9288df49..b215c3fd 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -11,6 +11,7 @@ use std::time::Duration;
use anyhow::{ensure, Context};
use directories::UserDirs;
+use git_version::git_version;
use niri_config::Config;
use smithay::output::Output;
use smithay::reexports::rustix::time::{clock_gettime, ClockId};
@@ -20,6 +21,14 @@ pub fn clone2<T: Clone, U: Clone>(t: (&T, &U)) -> (T, U) {
(t.0.clone(), t.1.clone())
}
+pub fn version() -> String {
+ format!(
+ "{} ({})",
+ env!("CARGO_PKG_VERSION"),
+ git_version!(fallback = "unknown commit"),
+ )
+}
+
pub fn get_monotonic_time() -> Duration {
let ts = clock_gettime(ClockId::Monotonic);
Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32)