1use std::io::Write;
3
4use app::parser::Parser;
6
7pub struct FishGen<'a, 'b>
8where
9 'a: 'b,
10{
11 p: &'b Parser<'a, 'b>,
12}
13
14impl<'a, 'b> FishGen<'a, 'b> {
15 pub fn new(p: &'b Parser<'a, 'b>) -> Self { FishGen { p: p } }
16
17 pub fn generate_to<W: Write>(&self, buf: &mut W) {
18 let command = self.p.meta.bin_name.as_ref().unwrap();
19 let mut buffer = String::new();
20 gen_fish_inner(command, self, command, &mut buffer);
21 w!(buf, buffer.as_bytes());
22 }
23}
24
25fn escape_string(string: &str) -> String { string.replace("\\", "\\\\").replace("'", "\\'") }
27
28fn gen_fish_inner(root_command: &str, comp_gen: &FishGen, subcommand: &str, buffer: &mut String) {
29 debugln!("FishGen::gen_fish_inner;");
30 let mut basic_template = format!("complete -c {} -n ", root_command);
44 if root_command == subcommand {
45 basic_template.push_str("\"__fish_use_subcommand\"");
46 } else {
47 basic_template.push_str(format!("\"__fish_seen_subcommand_from {}\"", subcommand).as_str());
48 }
49
50 for option in comp_gen.p.opts() {
51 let mut template = basic_template.clone();
52 if let Some(data) = option.s.short {
53 template.push_str(format!(" -s {}", data).as_str());
54 }
55 if let Some(data) = option.s.long {
56 template.push_str(format!(" -l {}", data).as_str());
57 }
58 if let Some(data) = option.b.help {
59 template.push_str(format!(" -d '{}'", escape_string(data)).as_str());
60 }
61 if let Some(ref data) = option.v.possible_vals {
62 template.push_str(format!(" -r -f -a \"{}\"", data.join(" ")).as_str());
63 }
64 buffer.push_str(template.as_str());
65 buffer.push_str("\n");
66 }
67
68 for flag in comp_gen.p.flags() {
69 let mut template = basic_template.clone();
70 if let Some(data) = flag.s.short {
71 template.push_str(format!(" -s {}", data).as_str());
72 }
73 if let Some(data) = flag.s.long {
74 template.push_str(format!(" -l {}", data).as_str());
75 }
76 if let Some(data) = flag.b.help {
77 template.push_str(format!(" -d '{}'", escape_string(data)).as_str());
78 }
79 buffer.push_str(template.as_str());
80 buffer.push_str("\n");
81 }
82
83 for subcommand in &comp_gen.p.subcommands {
84 let mut template = basic_template.clone();
85 template.push_str(" -f");
86 template.push_str(format!(" -a \"{}\"", &subcommand.p.meta.name).as_str());
87 if let Some(data) = subcommand.p.meta.about {
88 template.push_str(format!(" -d '{}'", escape_string(data)).as_str())
89 }
90 buffer.push_str(template.as_str());
91 buffer.push_str("\n");
92 }
93
94 for subcommand in &comp_gen.p.subcommands {
96 let sub_comp_gen = FishGen::new(&subcommand.p);
97 gen_fish_inner(root_command, &sub_comp_gen, &subcommand.to_string(), buffer);
98 }
99}