diff --git a/Cargo.lock b/Cargo.lock index 09ddf1e..1ce4cfd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -257,7 +257,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -276,6 +276,29 @@ dependencies = [ "usage-lib", ] +[[package]] +name = "cli-table" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b53f9241f288a7b12c56565f04aaeaeeab6b8923d42d99255d4ca428b4d97f89" +dependencies = [ + "cli-table-derive", + "csv", + "termcolor", + "unicode-width", +] + +[[package]] +name = "cli-table-derive" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e83a93253aaae7c74eb7428ce4faa6e219ba94886908048888701819f82fb94" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "colorchoice" version = "1.0.3" @@ -345,6 +368,27 @@ dependencies = [ "typenum", ] +[[package]] +name = "csv" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" +dependencies = [ + "memchr", +] + [[package]] name = "darwin-libproc" version = "0.1.2" @@ -373,7 +417,7 @@ checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -467,7 +511,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -903,7 +947,7 @@ checksum = "23c9b935fbe1d6cbd1dac857b54a688145e2d93f48db36010514d0f612d0ad67" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -1147,7 +1191,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -1212,6 +1256,7 @@ dependencies = [ "chrono", "clap", "clap_usage", + "cli-table", "console", "dirs", "duct", @@ -1443,7 +1488,7 @@ dependencies = [ "errno 0.3.10", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -1490,7 +1535,7 @@ checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -1608,7 +1653,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn", + "syn 2.0.90", ] [[package]] @@ -1632,6 +1677,17 @@ version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.90" @@ -1679,6 +1735,15 @@ dependencies = [ "unic-segment", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "terminal_size" version = "0.4.1" @@ -1725,7 +1790,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -1736,7 +1801,7 @@ checksum = "d65750cab40f4ff1929fb1ba509e9914eb756131cef4210da8d5d700d26f6312" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -1765,7 +1830,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -1981,7 +2046,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn", + "syn 2.0.90", "wasm-bindgen-shared", ] @@ -2003,7 +2068,7 @@ checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2042,7 +2107,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] @@ -2090,7 +2155,7 @@ checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -2101,7 +2166,7 @@ checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -2305,5 +2370,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] diff --git a/Cargo.toml b/Cargo.toml index db69557..c16ba95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ path = "src/main.rs" chrono = "0.4" clap = { version = "4", features = ["derive"] } clap_usage = "1" +cli-table = "0.4.9" console = "0.15" dirs = "5" duct = "0.13" diff --git a/docs/cli/commands.json b/docs/cli/commands.json index 1485fdc..a36208e 100644 --- a/docs/cli/commands.json +++ b/docs/cli/commands.json @@ -205,10 +205,23 @@ "full_cmd": [ "list" ], - "usage": "list", + "usage": "list [--hide-header]", "subcommands": {}, "args": [], - "flags": [], + "flags": [ + { + "name": "hide-header", + "usage": "--hide-header", + "help": "Show header", + "help_first_line": "Show header", + "short": [], + "long": [ + "hide-header" + ], + "hide": false, + "global": false + } + ], "mounts": [], "hide": false, "help": "List all daemons", diff --git a/docs/cli/index.md b/docs/cli/index.md index 60911f5..58dd307 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -14,7 +14,7 @@ - [`pitchfork completion `](/cli/completion.md) - [`pitchfork disable`](/cli/disable.md) - [`pitchfork enable`](/cli/enable.md) -- [`pitchfork list`](/cli/list.md) +- [`pitchfork list [--hide-header]`](/cli/list.md) - [`pitchfork logs [-n ] [-t --tail] [NAME]...`](/cli/logs.md) - [`pitchfork run [-f --force] [CMD]...`](/cli/run.md) - [`pitchfork start [NAME]...`](/cli/start.md) diff --git a/docs/cli/list.md b/docs/cli/list.md index 35b0d93..0017c68 100644 --- a/docs/cli/list.md +++ b/docs/cli/list.md @@ -1,6 +1,12 @@ # `pitchfork list` -- **Usage**: `pitchfork list` +- **Usage**: `pitchfork list [--hide-header]` - **Aliases**: `ls` List all daemons + +## Flags + +### `--hide-header` + +Show header diff --git a/pitchfork.usage.kdl b/pitchfork.usage.kdl index 6d46aed..e7dec15 100644 --- a/pitchfork.usage.kdl +++ b/pitchfork.usage.kdl @@ -38,6 +38,7 @@ cmd "enable" help="Allow a daemon to start" { } cmd "list" help="List all daemons" { alias "ls" + flag "--hide-header" help="Show header" } cmd "logs" help="Displays logs for daemon(s)" { alias "l" diff --git a/src/cli/list.rs b/src/cli/list.rs index cf6ba9d..5b031e1 100644 --- a/src/cli/list.rs +++ b/src/cli/list.rs @@ -1,12 +1,39 @@ -use crate::Result; +use crate::state_file::StateFile; +use crate::{env, Result}; +use cli_table::format::Separator; +use cli_table::{ + format::{Border, Justify}, + print_stdout, Cell, Table, +}; /// List all daemons #[derive(Debug, clap::Args)] #[clap(visible_alias = "ls", verbatim_doc_comment)] -pub struct List {} +pub struct List { + /// Show header + #[clap(long)] + hide_header: bool, +} impl List { pub async fn run(&self) -> Result<()> { + let sf = StateFile::read(&*env::PITCHFORK_STATE_FILE)?; + let mut table = vec![]; + for (name, daemon) in sf.daemons.iter() { + table.push(vec![ + name.cell(), + daemon.pid.cell(), + daemon.status.style().cell().justify(Justify::Right), + ]); + } + let mut table = table + .table() + .separator(Separator::builder().build()) + .border(Border::builder().build()); + if !self.hide_header || !console::user_attended() { + table = table.title(vec!["Name".cell(), "PID".cell(), "Status".cell()]) + } + assert!(print_stdout(table).is_ok()); Ok(()) } } diff --git a/src/state_file.rs b/src/state_file.rs index 2e34fc0..ede7b86 100644 --- a/src/state_file.rs +++ b/src/state_file.rs @@ -18,7 +18,8 @@ pub struct StateFileDaemon { pub status: DaemonStatus, } -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display)] +#[strum(serialize_all = "snake_case")] #[serde(rename_all = "snake_case")] pub enum DaemonStatus { Failed(String), @@ -26,6 +27,17 @@ pub enum DaemonStatus { Running, } +impl DaemonStatus { + pub fn style(&self) -> String { + let s = self.to_string(); + match self { + DaemonStatus::Failed(_) => console::style(s).red().to_string(), + DaemonStatus::Waiting => console::style(s).yellow().to_string(), + DaemonStatus::Running => console::style(s).green().to_string(), + } + } +} + impl StateFile { pub fn new(path: PathBuf) -> Self { Self {