Skip to content

Commit

Permalink
Support generating shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Feb 17, 2024
1 parent 9cbbcb9 commit 3f478b6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add `unindent` property to the label widget, allowing to disable removal of leading spaces (By: nrv)
- Switch to stable rust toolchain (1.76)
- Add `tooltip` widget, which allows setting a custom tooltip (not only text), to a widget (By: Rayzeq)
- Add `eww shell-completions` command, generating completion scripts for different shells

## [0.4.0] (04.09.2022)

Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cached = "0.48.0"
chrono = "0.4.26"
chrono-tz = "0.8.2"
clap = {version = "4.5.1", features = ["derive"] }
clap_complete = "4.5.1"
codespan-reporting = "0.11"
derive_more = "0.99"
extend = "1.2"
Expand Down
1 change: 1 addition & 0 deletions crates/eww/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ anyhow.workspace = true
bincode.workspace = true
chrono.workspace = true
clap = {workspace = true, features = ["derive"] }
clap_complete.workspace = true
codespan-reporting.workspace = true
derive_more.workspace = true
extend.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions crates/eww/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
paths::EwwPaths,
};
use anyhow::{Context, Result};
use clap::CommandFactory as _;
use std::{
io::{Read, Write},
os::unix::net::UnixStream,
Expand All @@ -20,6 +21,9 @@ pub fn handle_client_only_action(paths: &EwwPaths, action: ActionClientOnly) ->
.spawn()?
.wait()?;
}
ActionClientOnly::ShellCompletions { shell } => {
clap_complete::generate(shell, &mut opts::RawOpt::command(), "eww", &mut std::io::stdout());
}
}
Ok(())
}
Expand Down
25 changes: 24 additions & 1 deletion crates/eww/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Opt {
#[derive(Parser, Debug, Serialize, Deserialize, PartialEq)]
#[clap(author = "ElKowar")]
#[clap(version, about)]
struct RawOpt {
pub(super) struct RawOpt {
/// Write out debug logs. (To read the logs, run `eww logs`).
#[arg(long = "debug", global = true)]
log_debug: bool,
Expand Down Expand Up @@ -75,6 +75,13 @@ pub enum ActionClientOnly {
/// Print and watch the eww logs
#[command(name = "logs")]
Logs,

/// Generate a shell completion script
ShellCompletions {
#[arg(short, long)]
#[serde(with = "serde_shell")]
shell: clap_complete::shells::Shell,
},
}

#[derive(Subcommand, Debug, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -303,3 +310,19 @@ where
fn parse_duration(s: &str) -> Result<std::time::Duration, simplexpr::dynval::ConversionError> {
DynVal::from_string(s.to_owned()).as_duration()
}

mod serde_shell {
use std::str::FromStr as _;

use clap_complete::Shell;
use serde::{Deserialize as _, Deserializer, Serialize as _, Serializer};

pub fn serialize<S: Serializer>(shell: &Shell, serializer: S) -> Result<S::Ok, S::Error> {
shell.to_string().serialize(serializer)
}

pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Shell, D::Error> {
let s = String::deserialize(deserializer)?;
Shell::from_str(&s).map_err(serde::de::Error::custom)
}
}

0 comments on commit 3f478b6

Please sign in to comment.