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:
parent
242d818f88
commit
1debbf25a4
@ -14,6 +14,7 @@ members = ["libs/deob"]
|
||||
|
||||
[dependencies]
|
||||
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_yaml = "0.9"
|
||||
quick-xml = { version = "0.37", features = ["escape-html", "serialize"], default-features = false }
|
||||
|
@ -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
|
||||
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
|
||||
|
||||
Die folgenden Unterbefehle sind verfügbar
|
||||
|
8
build.rs
8
build.rs
@ -18,16 +18,16 @@
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
use std::fs;
|
||||
use std::io::Error;
|
||||
|
||||
use clap::CommandFactory;
|
||||
use clap_complete::generate_to;
|
||||
use clap_complete::Shell::Bash;
|
||||
use std::fs;
|
||||
use std::io::Error;
|
||||
|
||||
include!("src/cli.rs");
|
||||
|
||||
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());
|
||||
|
||||
|
52
docs/completions.md
Normal file
52
docs/completions.md
Normal 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.
|
14
src/cli.rs
14
src/cli.rs
@ -18,12 +18,8 @@
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
use clap::{Command, CommandFactory, Parser, Subcommand};
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn build_cli() -> Command {
|
||||
Cli::command()
|
||||
}
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap_complete::Shell;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about)]
|
||||
@ -35,6 +31,12 @@ pub struct Cli {
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum SubCommand {
|
||||
#[command(
|
||||
name = "completion",
|
||||
about = "Erzeuge und gebe Command-Completion aus",
|
||||
hide = true
|
||||
)]
|
||||
Completion { shell: Shell },
|
||||
#[command(
|
||||
name = "sha256sum",
|
||||
about = "Berechne SHA256 Prüfsumme für die angegebene Datei"
|
||||
|
@ -18,6 +18,18 @@
|
||||
* 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::fs;
|
||||
use std::fs::OpenOptions;
|
||||
@ -25,18 +37,6 @@ use std::io::Write;
|
||||
use std::ops::Add;
|
||||
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> {
|
||||
OpenOptions::new()
|
||||
.read(false)
|
||||
@ -52,6 +52,15 @@ fn write_outputfile(filename: String, content: &String) -> Result<(), FileError>
|
||||
|
||||
pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
|
||||
match command {
|
||||
SubCommand::Completion { shell } => {
|
||||
let command = &mut Cli::command();
|
||||
generate(
|
||||
shell,
|
||||
command,
|
||||
command.get_name().to_string(),
|
||||
&mut std::io::stdout(),
|
||||
);
|
||||
}
|
||||
SubCommand::List {
|
||||
inputfile,
|
||||
sorted,
|
||||
|
Loading…
x
Reference in New Issue
Block a user