Skip to content

Commit 49d7b0c

Browse files
committed
feat(watcher()): Add the -w argument to a Bashdoc call to watch for changes on INPUT
Bashdoc output will be updated on change
1 parent b8f5433 commit 49d7b0c

File tree

5 files changed

+107
-32
lines changed

5 files changed

+107
-32
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ serde = "1.0.80"
1414
serde_json = "1.0.33"
1515
toml = "0.4.8"
1616
nom = "4.1.1"
17-
handlebars = "1.1.0"
17+
handlebars = "1.1.0"
18+
notify = "4.0.6"

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Creates a "javadoc" like structure for bash. See github repo github.com/dustinkn
6060
formatting.
6161
6262
USAGE:
63-
bashdoc [FLAGS] [OPTIONS] <INPUT>
63+
bashdoc [FLAGS] [OPTIONS] <INPUT> [SUBCOMMAND]
6464
6565
FLAGS:
6666
-c, --color toggles color
@@ -69,10 +69,13 @@ FLAGS:
6969
-V, --version Prints version information
7070
7171
OPTIONS:
72-
-h, --html <html> output html documentation
73-
-j, --json <FILE> print result as JSON
74-
-o, --override <override> override delimiters for this call.
72+
-h, --html <html> output html documentation
73+
-j, --json <FILE> print result as JSON
7574
7675
ARGS:
7776
<INPUT> Sets the input file to use
77+
78+
SUBCOMMANDS:
79+
help Prints this message or the help of the given subcommand(s)
80+
override override the delimiters
7881
```

cli.yml

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
about: 'Creates a "javadoc" like structure for bash. See github repo github.com/dustinknopoff/bashdoc for information on formatting.'
2+
author: "Dustin Knopoff <dustinknopoff@gmail.com>"
3+
name: bashdoc
4+
version: "1.0"
25
args:
36
- color:
47
help: "toggles color"
@@ -18,16 +21,46 @@ args:
1821
short: j
1922
takes_value: true
2023
value_name: FILE
21-
- override:
22-
help: override delimiters for this call.
23-
short: o
24-
long: override
25-
takes_value: true
2624
- html:
2725
help: output html documentation
2826
short: h
2927
long: html
3028
takes_value: true
31-
author: "Dustin Knopoff <dustinknopoff@gmail.com>"
32-
name: bashdoc
33-
version: "1.0"
29+
- watch:
30+
help: continuously update on change
31+
short: w
32+
long: watch
33+
subcommands:
34+
- override:
35+
about: override the delimiters
36+
args:
37+
- start:
38+
short: s
39+
long: start
40+
help: delimiter for start
41+
takes_value: true
42+
- end:
43+
short: e
44+
long: end
45+
help: delimiter for start
46+
takes_value: true
47+
- descriptor:
48+
short: d
49+
long: desc
50+
help: delimiter for descriptors
51+
takes_value: true
52+
- params:
53+
short: p
54+
long: param
55+
help: delimiter for parameters
56+
takes_value: true
57+
- returns:
58+
short: r
59+
long: ret
60+
help: delimiter for return values
61+
takes_value: true
62+
- comment:
63+
short: c
64+
long: comment
65+
help: delimiter for comments
66+
takes_value: true

src/docs.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clap::ArgMatches;
12
use colored::*;
23
use dirs::home_dir;
34
use glob::glob;
@@ -115,9 +116,9 @@ fn parse_doc<'a>(input: &'a str, delims: Delimiters) -> IResult<&'a str, Doc> {
115116
>> (Doc {
116117
short_description: short.to_string(),
117118
long_description: long.unwrap_or("").to_string(),
118-
descriptors: desc.unwrap_or(Vec::new()),
119-
params: par.unwrap_or(Vec::new()),
120-
returns: ret.unwrap_or(Vec::new()),
119+
descriptors: desc.unwrap_or_default(),
120+
params: par.unwrap_or_default(),
121+
returns: ret.unwrap_or_default(),
121122
})
122123
)
123124
}
@@ -364,20 +365,26 @@ impl<'a> Default for Delimiters<'a> {
364365

365366
impl<'a> Delimiters<'a> {
366367
/// Override default delimiters with passed in values
367-
pub fn override_delims(overrides: String) -> Self {
368+
pub fn override_delims(overrides: &'a ArgMatches<'a>) -> Self {
368369
let mut result: Delimiters = Delimiters::default();
369-
let splitted: Vec<_> = Box::leak(overrides.into_boxed_str())
370-
.split_whitespace()
371-
.collect();
372-
if splitted.len() != 6 {
373-
panic!("Please enter the proper number of delimiters");
370+
if overrides.is_present("start") {
371+
result.start = overrides.value_of("start").unwrap();
372+
}
373+
if overrides.is_present("end") {
374+
result.end = overrides.value_of("end").unwrap();
375+
}
376+
if overrides.is_present("descriptor") {
377+
result.opt = overrides.value_of("descriptor").unwrap();
378+
}
379+
if overrides.is_present("params") {
380+
result.params = overrides.value_of("params").unwrap();
381+
}
382+
if overrides.is_present("returns") {
383+
result.ret = overrides.value_of("returns").unwrap();
384+
}
385+
if overrides.is_present("comment") {
386+
result.comm = overrides.value_of("comment").unwrap();
374387
}
375-
result.start = &splitted[0];
376-
result.end = &splitted[1];
377-
result.params = &splitted[2];
378-
result.ret = &splitted[3];
379-
result.opt = &splitted[4];
380-
result.comm = &splitted[5];
381388
result
382389
}
383390

src/main.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,26 @@
4545
//!
4646
mod docs;
4747
use crate::docs::*;
48-
use clap::{load_yaml, App};
48+
use clap::{load_yaml, App, ArgMatches};
49+
use dirs::home_dir;
50+
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
51+
use std::sync::mpsc::channel;
52+
use std::time::Duration;
4953

5054
fn main() {
5155
let yaml = load_yaml!("../cli.yml");
5256
let matches = App::from_yaml(yaml).get_matches();
53-
let delims = if matches.is_present("override") {
54-
Delimiters::override_delims(matches.value_of("override").unwrap().to_string())
57+
if matches.is_present("watch") {
58+
watcher(&matches);
5559
} else {
56-
Delimiters::get_delims()
60+
generate(&matches);
61+
}
62+
}
63+
64+
fn generate<'a>(matches: &'a ArgMatches<'a>) {
65+
let delims = match matches.subcommand() {
66+
("override", Some(sub_m)) => Delimiters::override_delims(sub_m),
67+
_ => Delimiters::get_delims(),
5768
};
5869
let all_em = if matches.is_present("directory") {
5970
start(
@@ -82,3 +93,23 @@ fn main() {
8293
}
8394
}
8495
}
96+
97+
fn watcher<'a>(matches: &'a ArgMatches<'a>) {
98+
generate(matches);
99+
let (tx, rx) = channel();
100+
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2)).unwrap();
101+
let path = matches
102+
.value_of("INPUT")
103+
.unwrap()
104+
.replace("~", home_dir().unwrap().to_str().unwrap());
105+
watcher.watch(&path, RecursiveMode::Recursive).unwrap();
106+
println!("Watching for changes in {}...", path);
107+
loop {
108+
match rx.recv() {
109+
Ok(_) => {
110+
generate(&matches);
111+
}
112+
Err(e) => println!("watch error: {:?}", e),
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)