Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -9,6 +9,7 @@ atomic_enum = "0.3.0"
cairo-rs = { version = "0.21.2", features = [ "png" ] }
chrono = "0.4.42"
clap = { version = "4.5.53", features = [ "derive" ] }
clap_complete = "4.5.64"
config = "0.15.18"
dirs = "6.0.0"
gdk-pixbuf = "0.21.5"
Expand Down
23 changes: 23 additions & 0 deletions doc/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,26 @@ The following correspond directly to configuration options. See
- `--image-path <PATH>`, path to a background image
- `--image-scale <SCALE MODE>`, sets the image scaling mode

## Shell Completions

nlock supports self-generation of shell completions for common shells.

To generate completions for your current shell, run:

```sh
$ nlock completions
```

This will display a shell completion script for your current shell. The process
of using this script is shell-specific, but typically, it should be written to
a file and loaded on startup.

If shell detection fails, try specifying the shell explicitly, using Zsh as an
example:

```sh
$ nlock completions /usr/bin/zsh
```

If generation continues to fail, your shell is likely unsupported.

41 changes: 38 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2025, Nathan Gill

use std::{path::PathBuf, str::FromStr};
use std::{
path::{Path, PathBuf},
str::FromStr,
};

use clap::{
Arg, ArgMatches, Command, ValueEnum,
Arg, ArgAction, ArgMatches, Command, ValueEnum,
builder::{
EnumValueParser, Styles,
styling::{AnsiColor, Effects},
},
};
use clap_complete::{Shell, aot::generate as generate_completions};

use crate::surface::{BackgroundImageScale, BackgroundType, FontSlant, FontWeight, Rgba};

Expand Down Expand Up @@ -288,6 +292,16 @@ fn build_cli() -> Command {
.about("Customisable, minimalist screen locker for Wayland")
.version(env!("CARGO_PKG_VERSION"))
.styles(styles())
.subcommand(
Command::new("completions")
.about("Generate shell completions for nlock")
.arg(
Arg::new("shell")
.help("Shell to generate completions for")
.value_name("SHELL")
.action(ArgAction::Set),
),
)
.arg(
Arg::new("log_level")
.help("Log level verbosity")
Expand Down Expand Up @@ -453,5 +467,26 @@ pub fn run_cli() -> NLockArgs {
let cli = build_cli();
let args = cli.get_matches();

NLockArgs::load_arg_matches(&args)
if let Some(("completions", args)) = args.subcommand() {
let shell_arg = args.get_one::<String>("shell");

let shell = if let Some(shell_arg) = shell_arg {
Shell::from_shell_path(Path::new(shell_arg))
} else {
Shell::from_env()
};

if let Some(shell) = shell {
let mut cli = build_cli();
generate_completions(shell, &mut cli, "nlock", &mut std::io::stdout());
} else {
println!("Failed to identify shell");
std::process::exit(1);
}

// Don't continue running after generation
std::process::exit(0);
} else {
NLockArgs::load_arg_matches(&args)
}
}