1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-04-19 11:46:50 +00:00

feat: add command to generate completion (#33)

This commit is contained in:
Paul-Christian Volkmer 2024-11-06 18:54:36 +01:00 committed by GitHub
parent 242d818f88
commit 1debbf25a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 93 additions and 22 deletions

View File

@ -14,6 +14,7 @@ members = ["libs/deob"]
[dependencies] [dependencies]
clap = { version = "4.5", features = ["std", "help", "usage", "derive", "error-context"], default-features = false } clap = { version = "4.5", features = ["std", "help", "usage", "derive", "error-context"], default-features = false }
clap_complete = "4.5"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9" serde_yaml = "0.9"
quick-xml = { version = "0.37", features = ["escape-html", "serialize"], default-features = false } quick-xml = { version = "0.37", features = ["escape-html", "serialize"], default-features = false }

View File

@ -14,6 +14,13 @@ sowie die Anzeige anhand eines Profils angepasst.
Wird in einer OSC-Datei eine noch nicht bekannte Eigenschaft erkannt, wird die weitere Bearbeitung abgebrochen, um keine Wird in einer OSC-Datei eine noch nicht bekannte Eigenschaft erkannt, wird die weitere Bearbeitung abgebrochen, um keine
unvollständigen Ausgabedateien zu erzeugen. unvollständigen Ausgabedateien zu erzeugen.
### Command Completion
Die Anwendung kann selbständig Scripte zur automatischen Ergänzung der Unterbefehle erstellen,
sollte dies nicht im Installationspaket enthalten sein.
Weitere Informationen hier: [Command Completion](docs/completions.md)
### Beispiele ### Beispiele
Die folgenden Unterbefehle sind verfügbar Die folgenden Unterbefehle sind verfügbar

View File

@ -18,16 +18,16 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
use std::fs; use clap::CommandFactory;
use std::io::Error;
use clap_complete::generate_to; use clap_complete::generate_to;
use clap_complete::Shell::Bash; use clap_complete::Shell::Bash;
use std::fs;
use std::io::Error;
include!("src/cli.rs"); include!("src/cli.rs");
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let mut cmd = build_cli(); let mut cmd = Cli::command();
let package_name = std::env::var("CARGO_CRATE_NAME").unwrap_or("osc-variant".to_string()); let package_name = std::env::var("CARGO_CRATE_NAME").unwrap_or("osc-variant".to_string());

52
docs/completions.md Normal file
View File

@ -0,0 +1,52 @@
## Command Completion
Die Anwendung kann selbständig Scripte zur automatischen Ergänzung der Unterbefehle erstellen,
sollte dies nicht im Installationspaket enthalten sein.
### Bash
Erzeugen des Completion-Files mit:
```bash
mkdir -p ~/.local/share/bash-completion/completions
osc-variant completion bash > ~/.local/share/bash-completion/completions/osc-variant
```
Nach einem Neustart des Terminals sollte nun die Completion verfügbar sein.
Alternativ kann im aktuellen Terminal auch folgendes angewendet werden:
```bash
source <(osc-variant completion bash)
```
### Zsh
Erzeugen des Completions-Files mit:
```sh
mkdir -p ~/.osc-variant/completions
osc-variant completion zsh > ~/.osc-variant/completions/_osc-variant
```
Hinzufügen zur Umgebungsvariable `FPATH` zur Konfiguration mit:
```sh
cat <<"EOT" >> ~/.zshrc
FPATH="$HOME/.osc-variant/completions:$FPATH"
autoload -Uz compinit
compinit
EOT
```
Nach einem Neustart des Terminals sollte nun die Completion verfügbar sein.
### Fish
Erzeugen des Completions-Files mit:
```sh
mkdir -p ~/.config/fish/completions
osc-variant completion fish > ~/.config/fish/completions/osc-variant.fish
```
Die Completion sollte sofort verfügbar sein.

View File

@ -18,12 +18,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
use clap::{Command, CommandFactory, Parser, Subcommand}; use clap::{Parser, Subcommand};
use clap_complete::Shell;
#[allow(dead_code)]
fn build_cli() -> Command {
Cli::command()
}
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about)] #[command(author, version, about)]
@ -35,6 +31,12 @@ pub struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum SubCommand { pub enum SubCommand {
#[command(
name = "completion",
about = "Erzeuge und gebe Command-Completion aus",
hide = true
)]
Completion { shell: Shell },
#[command( #[command(
name = "sha256sum", name = "sha256sum",
about = "Berechne SHA256 Prüfsumme für die angegebene Datei" about = "Berechne SHA256 Prüfsumme für die angegebene Datei"

View File

@ -18,6 +18,18 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
use crate::checks::{check_file, print_checks, CheckNotice};
use crate::cli::{Cli, SubCommand};
use crate::file_io::{FileError, FileReader, InputFile};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile;
use clap::CommandFactory;
use clap_complete::generate;
use console::style;
use dialoguer::Confirm;
use quick_xml::se::Serializer;
use serde::Serialize;
use sha256::digest;
use std::error::Error; use std::error::Error;
use std::fs; use std::fs;
use std::fs::OpenOptions; use std::fs::OpenOptions;
@ -25,18 +37,6 @@ use std::io::Write;
use std::ops::Add; use std::ops::Add;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use console::style;
use dialoguer::Confirm;
use quick_xml::se::Serializer;
use serde::Serialize;
use sha256::digest;
use crate::checks::{check_file, print_checks, CheckNotice};
use crate::cli::SubCommand;
use crate::file_io::{FileError, FileReader, InputFile};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile;
fn write_outputfile(filename: String, content: &String) -> Result<(), FileError> { fn write_outputfile(filename: String, content: &String) -> Result<(), FileError> {
OpenOptions::new() OpenOptions::new()
.read(false) .read(false)
@ -52,6 +52,15 @@ fn write_outputfile(filename: String, content: &String) -> Result<(), FileError>
pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> { pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
match command { match command {
SubCommand::Completion { shell } => {
let command = &mut Cli::command();
generate(
shell,
command,
command.get_name().to_string(),
&mut std::io::stdout(),
);
}
SubCommand::List { SubCommand::List {
inputfile, inputfile,
sorted, sorted,