Skip to content

Commit ed0550e

Browse files
authored
Merge pull request #36 from PicoJr/dev
v1.5.0
2 parents 577d3e5 + ae8d242 commit ed0550e

File tree

7 files changed

+80
-4
lines changed

7 files changed

+80
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.5.0](https://crates.io/crates/rtw/1.5.0) Jun 21, 2020
9+
10+
* add `completion <shell>` command.
11+
12+
`rtw completion <shell>` generates completion file for `<shell>`
13+
814
## [1.4.1](https://crates.io/crates/rtw/1.4.1) Jun 19, 2020
915

1016
* Fix timeline crash when activity spans over several days (#33)

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rtw"
3-
version = "1.4.1"
3+
version = "1.5.0"
44
authors = ["PicoJr <picojr_dev@gmx.com>"]
55
edition = "2018"
66
repository = "https://github.com/PicoJr/rtw"
@@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
99
readme = "README.md"
1010
keywords = ["time", "tracker", "cli", "tool"]
1111
categories = ["command-line-utilities"]
12-
include = ["src/**/*", "/LICENSE", "/README.md", "/CHANGELOG.md", "/commands.md", "/img/*"]
12+
include = ["src/**/*", "/LICENSE", "/README.md", "/CHANGELOG.md", "/commands.md", "/shell-completion.md", "/img/*"]
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ cargo build --release
5555

5656
Download the corresponding archive from the [Release page](https://github.com/picojr/rtw/releases).
5757

58+
### Shell Completion (Bash, Zsh, Fish, Powershell, Elvish)
59+
60+
Please see [shell completion](shell-completion.md).
61+
5862
## Changelog
5963

6064
Please see the [CHANGELOG](CHANGELOG.md) for a release history.

release.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ rustfmt --check src/**/*.rs &&
66
cargo test &&
77
cargo build --release &&
88
mkdir -p ${RELEASE_DIR} &&
9-
cp -r CHANGELOG.md commands.md example/ img/ README.md LICENSE target/release/rtw ${RELEASE_DIR}
9+
cp -r ./CHANGELOG.md ./commands.md ./shell-completion.md example/ img/ ./README.md ./LICENSE target/release/rtw ${RELEASE_DIR}

shell-completion.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# RTW Shell Completion
2+
3+
supported shells: bash, zsh, fish, powershell, elvish
4+
5+
Write completion file for `<shell>` to stdout:
6+
7+
```
8+
rtw completion <shell>
9+
```
10+
11+
## oh-my-zsh
12+
13+
```
14+
.oh-my-zsh/custom/plugins/rtw
15+
├── _rtw
16+
└── rtw.plugin.zsh
17+
```
18+
19+
```
20+
mkdir -p ~/.oh-my-zsh/custom/plugins/rtw
21+
rtw completion zsh > ~/.oh-my-zsh/custom/plugins/rtw/_rtw
22+
echo "#rtw completion plugin" > ~/.oh-my-zsh/custom/plugins/rtw/rtw.plugin.zsh
23+
```
24+
25+
Add `rtw` to `plugins` in `.zshrc`:
26+
27+
```
28+
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
29+
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
30+
# Example format: plugins=(rails git textmate ruby lighthouse)
31+
# Add wisely, as too many plugins slow down shell startup.
32+
plugins=(git rtw)
33+
```

src/cli_helper.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn split_time_range(tokens: &[String], clock: &dyn Clock) -> anyhow::Result<(Tim
7171
}
7272

7373
pub fn get_app() -> App<'static, 'static> {
74-
App::new("RTW")
74+
App::new(crate_name!())
7575
.version(crate_version!())
7676
.author("PicoJr")
7777
.about("rust time tracking CLI")
@@ -224,6 +224,16 @@ pub fn get_app() -> App<'static, 'static> {
224224
.arg(Arg::with_name("id").required(true).help("activity id")),
225225
)
226226
.subcommand(SubCommand::with_name("cancel").about("cancel current activity"))
227+
.subcommand(
228+
SubCommand::with_name("completion")
229+
.about("generate completion file")
230+
.arg(
231+
Arg::with_name("shell")
232+
.possible_values(&["bash", "zsh", "fish", "powershell", "elvish"])
233+
.takes_value(true)
234+
.required(true),
235+
),
236+
)
227237
}
228238

229239
pub fn parse_start_args(start_m: &ArgMatches, clock: &dyn Clock) -> anyhow::Result<(Time, Tags)> {
@@ -327,6 +337,19 @@ pub fn parse_delete_args(delete_m: &ArgMatches) -> anyhow::Result<ActivityId> {
327337
}
328338
}
329339

340+
pub fn parse_completion_args(completion_m: &ArgMatches) -> anyhow::Result<clap::Shell> {
341+
let shell_maybe = completion_m.value_of("shell");
342+
match shell_maybe {
343+
Some("bash") => Ok(clap::Shell::Bash),
344+
Some("zsh") => Ok(clap::Shell::Zsh),
345+
Some("fish") => Ok(clap::Shell::Fish),
346+
Some("powershell") => Ok(clap::Shell::PowerShell),
347+
Some("elvish") => Ok(clap::Shell::Elvish),
348+
None => Err(anyhow::anyhow!("missing shell")), // should never happen thanks to clap check
349+
_ => Err(anyhow::anyhow!("invalid shell")), // should never happen thanks to clap check
350+
}
351+
}
352+
330353
#[cfg(test)]
331354
mod tests {
332355
use crate::chrono_clock::ChronoClock;

src/rtw_cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub enum RTWAction {
2929
Delete(ActivityId),
3030
DisplayCurrent,
3131
Timeline((DateTimeW, DateTimeW)),
32+
Completion(clap::Shell),
3233
}
3334

3435
pub enum RTWMutation {
@@ -93,6 +94,10 @@ where
9394
cli_helper::parse_summary_args(sub_m, clock)?;
9495
Ok(RTWAction::DumpICal((range_start, range_end)))
9596
}
97+
("completion", Some(sub_m)) => {
98+
let shell = cli_helper::parse_completion_args(sub_m)?;
99+
Ok(RTWAction::Completion(shell))
100+
}
96101
// default case: display current activity
97102
_ => Ok(RTWAction::DisplayCurrent),
98103
}
@@ -262,6 +267,11 @@ where
262267
println!("{}", calendar);
263268
Ok(RTWMutation::Pure)
264269
}
270+
RTWAction::Completion(shell) => {
271+
let mut app = cli_helper::get_app();
272+
app.gen_completions_to(crate_name!(), shell, &mut std::io::stdout());
273+
Ok(RTWMutation::Pure)
274+
}
265275
}
266276
}
267277

0 commit comments

Comments
 (0)