diff options
author | Minijackson <minijackson@riseup.net> | 2018-09-04 19:30:02 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2018-09-04 19:30:02 +0200 |
commit | 8b6c0900d6d301eb2791a7300acfdf1cb7fcce8d (patch) | |
tree | 1cd7c9a688b6572cbc9a2d909d3ef30ec24b1615 | |
parent | e2e2f14accc751573a2a52b43bd8c87e1f1b89db (diff) | |
download | set_eq-8b6c0900d6d301eb2791a7300acfdf1cb7fcce8d.tar.gz set_eq-8b6c0900d6d301eb2791a7300acfdf1cb7fcce8d.zip |
Separate CLI + auto-generate completion at build time
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | build.rs | 23 | ||||
-rw-r--r-- | derivation.nix | 7 | ||||
-rw-r--r-- | src/cli.rs | 59 | ||||
-rw-r--r-- | src/main.rs | 60 |
5 files changed, 96 insertions, 57 deletions
@@ -5,6 +5,10 @@ authors = ["Minijackson <minijackson@riseup.net>"] | |||
5 | 5 | ||
6 | [build-dependencies] | 6 | [build-dependencies] |
7 | lalrpop = "0.15.2" | 7 | lalrpop = "0.15.2" |
8 | clap = "*" | ||
9 | clap-verbosity-flag = "0.2" | ||
10 | clap-log-flag = "0.1" | ||
11 | structopt = "0.2" | ||
8 | 12 | ||
9 | [dependencies] | 13 | [dependencies] |
10 | dbus = "0.6.2" | 14 | dbus = "0.6.2" |
@@ -1,5 +1,28 @@ | |||
1 | extern crate lalrpop; | 1 | extern crate lalrpop; |
2 | 2 | ||
3 | #[macro_use] | ||
4 | extern crate clap; | ||
5 | extern crate clap_log_flag; | ||
6 | extern crate clap_verbosity_flag; | ||
7 | #[macro_use] | ||
8 | extern crate structopt; | ||
9 | |||
10 | use structopt::StructOpt; | ||
11 | |||
12 | use clap::Shell; | ||
13 | |||
14 | use std::env; | ||
15 | |||
16 | #[path = "src/cli.rs"] | ||
17 | mod cli; | ||
18 | |||
3 | fn main() { | 19 | fn main() { |
4 | lalrpop::process_root().unwrap(); | 20 | lalrpop::process_root().unwrap(); |
21 | |||
22 | let outdir = env::var_os("OUT_DIR").expect("OUT_DIR environment variable not defined"); | ||
23 | |||
24 | let mut app = cli::Cli::clap(); | ||
25 | app.gen_completions("set_eq", Shell::Bash, &outdir); | ||
26 | app.gen_completions("set_eq", Shell::Fish, &outdir); | ||
27 | app.gen_completions("set_eq", Shell::Zsh, &outdir); | ||
5 | } | 28 | } |
diff --git a/derivation.nix b/derivation.nix index 329ef3b..b895bb8 100644 --- a/derivation.nix +++ b/derivation.nix | |||
@@ -9,6 +9,13 @@ rustPlatform.buildRustPackage rec { | |||
9 | src = ./.; | 9 | src = ./.; |
10 | cargoSha256 = "0lknxqr1pfbj6z981rw5ppkklknryyafl5f552aaw4iqhq94slq4"; | 10 | cargoSha256 = "0lknxqr1pfbj6z981rw5ppkklknryyafl5f552aaw4iqhq94slq4"; |
11 | 11 | ||
12 | preFixup = '' | ||
13 | mkdir -p "$out/share/"{bash-completion/completions,fish/vendor_completions.d,zsh/site-functions} | ||
14 | cp target/release/build/set_eq-*/out/set_eq.bash "$out/share/bash-completion/completions/" | ||
15 | cp target/release/build/set_eq-*/out/set_eq.fish "$out/share/fish/vendor_completions.d/" | ||
16 | cp target/release/build/set_eq-*/out/_set_eq "$out/share/zsh/site-functions/" | ||
17 | ''; | ||
18 | |||
12 | meta = with stdenv.lib; { | 19 | meta = with stdenv.lib; { |
13 | description = "A command-line tool to manipulate PulseAudio's equalizer"; | 20 | description = "A command-line tool to manipulate PulseAudio's equalizer"; |
14 | homepage = https://github.com/minijackson/set_eq; | 21 | homepage = https://github.com/minijackson/set_eq; |
diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..f24b222 --- /dev/null +++ b/src/cli.rs | |||
@@ -0,0 +1,59 @@ | |||
1 | use clap_verbosity_flag; | ||
2 | use clap_log_flag; | ||
3 | use structopt; | ||
4 | |||
5 | #[derive(StructOpt, Debug)] | ||
6 | #[structopt(raw(setting = "structopt::clap::AppSettings::ColoredHelp"))] | ||
7 | /// Hello World! How are you doing? | ||
8 | pub struct Cli { | ||
9 | #[structopt(flatten)] | ||
10 | pub verbose: clap_verbosity_flag::Verbosity, | ||
11 | #[structopt(flatten)] | ||
12 | pub log: clap_log_flag::Log, | ||
13 | #[structopt(short = "s")] | ||
14 | /// Use the given sink. | ||
15 | /// | ||
16 | /// By default it will use the last equalized sink it finds | ||
17 | pub sink: Option<String>, | ||
18 | #[structopt(subcommand)] | ||
19 | pub cmd: Command, | ||
20 | } | ||
21 | |||
22 | #[derive(StructOpt, Debug)] | ||
23 | pub enum Command { | ||
24 | #[structopt(name = "load",)] | ||
25 | /// Load and switch to a given equalizer configuration | ||
26 | Load(LoadCli), | ||
27 | #[structopt(name = "reset")] | ||
28 | /// Switch to a neutral equalizer | ||
29 | Reset(ResetCli), | ||
30 | } | ||
31 | |||
32 | #[derive(StructOpt, Debug)] | ||
33 | pub struct LoadCli { | ||
34 | #[structopt(default_value = "-")] | ||
35 | /// The file from which to load the equalizer configuration | ||
36 | /// | ||
37 | /// If "-" is given, read the configuration from the command-line. | ||
38 | pub file: String, | ||
39 | #[structopt( | ||
40 | short = "f", | ||
41 | raw( | ||
42 | possible_values = "&EqualizerConfFormat::variants()", | ||
43 | case_insensitive = "true" | ||
44 | ), | ||
45 | default_value = "EqualizerAPO" | ||
46 | )] | ||
47 | /// The file format of the equalizer configuration | ||
48 | pub format: EqualizerConfFormat, | ||
49 | } | ||
50 | |||
51 | arg_enum! { | ||
52 | #[derive(Debug)] | ||
53 | pub enum EqualizerConfFormat { | ||
54 | EqualizerAPO | ||
55 | } | ||
56 | } | ||
57 | |||
58 | #[derive(StructOpt, Debug)] | ||
59 | pub struct ResetCli {} | ||
diff --git a/src/main.rs b/src/main.rs index 360117c..85d2443 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -14,75 +14,21 @@ extern crate structopt; | |||
14 | 14 | ||
15 | extern crate lalrpop_util; | 15 | extern crate lalrpop_util; |
16 | 16 | ||
17 | mod cli; | ||
17 | mod dbus_api; | 18 | mod dbus_api; |
18 | mod parsing; | 19 | mod parsing; |
19 | mod utils; | 20 | mod utils; |
20 | 21 | ||
21 | use utils::*; | 22 | use utils::*; |
22 | |||
23 | use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer; | 23 | use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer; |
24 | use cli::*; | ||
25 | |||
24 | use failure::Error; | 26 | use failure::Error; |
25 | use structopt::StructOpt; | 27 | use structopt::StructOpt; |
26 | 28 | ||
27 | use std::fs::File; | 29 | use std::fs::File; |
28 | use std::io; | 30 | use std::io; |
29 | 31 | ||
30 | #[derive(StructOpt, Debug)] | ||
31 | #[structopt(raw(setting = "structopt::clap::AppSettings::ColoredHelp"))] | ||
32 | /// Hello World! How are you doing? | ||
33 | struct Cli { | ||
34 | #[structopt(flatten)] | ||
35 | verbose: clap_verbosity_flag::Verbosity, | ||
36 | #[structopt(flatten)] | ||
37 | log: clap_log_flag::Log, | ||
38 | #[structopt(short = "s")] | ||
39 | /// Use the given sink. | ||
40 | /// | ||
41 | /// By default it will use the last equalized sink it finds | ||
42 | sink: Option<String>, | ||
43 | #[structopt(subcommand)] | ||
44 | cmd: Command, | ||
45 | } | ||
46 | |||
47 | #[derive(StructOpt, Debug)] | ||
48 | enum Command { | ||
49 | #[structopt(name = "load",)] | ||
50 | /// Load and switch to a given equalizer configuration | ||
51 | Load(LoadCli), | ||
52 | #[structopt(name = "reset")] | ||
53 | /// Switch to a neutral equalizer | ||
54 | Reset(ResetCli), | ||
55 | } | ||
56 | |||
57 | #[derive(StructOpt, Debug)] | ||
58 | struct LoadCli { | ||
59 | #[structopt(default_value = "-")] | ||
60 | /// The file from which to load the equalizer configuration | ||
61 | /// | ||
62 | /// If "-" is given, read the configuration from the command-line. | ||
63 | file: String, | ||
64 | #[structopt( | ||
65 | short = "f", | ||
66 | raw( | ||
67 | possible_values = "&EqualizerConfFormat::variants()", | ||
68 | case_insensitive = "true" | ||
69 | ), | ||
70 | default_value = "EqualizerAPO" | ||
71 | )] | ||
72 | /// The file format of the equalizer configuration | ||
73 | format: EqualizerConfFormat, | ||
74 | } | ||
75 | |||
76 | arg_enum! { | ||
77 | #[derive(Debug)] | ||
78 | enum EqualizerConfFormat { | ||
79 | EqualizerAPO | ||
80 | } | ||
81 | } | ||
82 | |||
83 | #[derive(StructOpt, Debug)] | ||
84 | struct ResetCli {} | ||
85 | |||
86 | #[derive(Debug)] | 32 | #[derive(Debug)] |
87 | pub struct Filter { | 33 | pub struct Filter { |
88 | preamp: f64, | 34 | preamp: f64, |