Skip to content

Commit

Permalink
feat(fig): escape all strings
Browse files Browse the repository at this point in the history
  • Loading branch information
grant0417 committed Aug 21, 2022
1 parent c023db3 commit fedda32
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions clap_complete_fig/src/fig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ impl Generator for Fig {
write!(
&mut buffer,
"const completion: Fig.Spec = {{\n name: \"{}\",\n",
command
escape_string(command)
)
.unwrap();

write!(
&mut buffer,
" description: \"{}\",\n",
cmd.get_about().unwrap_or_default()
escape_string(cmd.get_about().unwrap_or_default())
)
.unwrap();

Expand All @@ -41,9 +41,14 @@ impl Generator for Fig {
}
}

// Escape string inside double quotes
// Escape string inside double quotes and convert whitespace
fn escape_string(string: &str) -> String {
string.replace('\\', "\\\\").replace('\"', "\\\"")
string
.replace('\\', "\\\\")
.replace('\"', "\\\"")
.replace('\t', " ")
.replace('\n', " ")
.replace('\r', "")
}

fn gen_fig_inner(
Expand Down Expand Up @@ -73,7 +78,7 @@ fn gen_fig_inner(
buffer.push_str(
&aliases
.iter()
.map(|name| format!("\"{}\"", name))
.map(|name| format!("\"{}\"", escape_string(name)))
.collect::<Vec<_>>()
.join(", "),
);
Expand All @@ -85,7 +90,7 @@ fn gen_fig_inner(
"{:indent$}{{\n{:indent$} name: \"{}\",\n",
"",
"",
subcommand.get_name(),
escape_string(subcommand.get_name()),
indent = indent + 2
)
.unwrap();
Expand Down Expand Up @@ -157,11 +162,19 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
let mut names = vec![];

if let Some(shorts) = option.get_short_and_visible_aliases() {
names.extend(shorts.iter().map(|short| format!("-{}", short)));
names.extend(
shorts
.iter()
.map(|short| format!("-{}", escape_string(&short.to_string()))),
);
}

if let Some(longs) = option.get_long_and_visible_aliases() {
names.extend(longs.iter().map(|long| format!("--{}", long)));
names.extend(
longs
.iter()
.map(|long| format!("--{}", escape_string(long))),
);
}

if names.len() > 1 {
Expand All @@ -170,7 +183,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
buffer.push_str(
&names
.iter()
.map(|name| format!("\"{}\"", name))
.map(|name| format!("\"{}\"", escape_string(name)))
.collect::<Vec<_>>()
.join(", "),
);
Expand All @@ -181,7 +194,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
&mut buffer,
"{:indent$}name: \"{}\",\n",
"",
names[0],
escape_string(&names[0]),
indent = indent + 4
)
.unwrap();
Expand Down Expand Up @@ -224,7 +237,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
&mut buffer,
"{:indent$}\"{}\",\n",
"",
conflict,
escape_string(&conflict),
indent = indent + 6
)
.unwrap();
Expand Down Expand Up @@ -279,7 +292,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
buffer.push_str(
&flags
.iter()
.map(|name| format!("\"{}\"", name))
.map(|name| format!("\"{}\"", escape_string(name)))
.collect::<Vec<_>>()
.join(", "),
);
Expand All @@ -290,7 +303,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
&mut buffer,
"{:indent$}name: \"{}\",\n",
"",
flags[0],
escape_string(&flags[0]),
indent = indent + 4
)
.unwrap();
Expand Down Expand Up @@ -323,7 +336,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
&mut buffer,
"{:indent$}\"{}\",\n",
"",
conflict,
escape_string(&conflict),
indent = indent + 6
)
.unwrap();
Expand Down Expand Up @@ -362,7 +375,7 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
&mut buffer,
"{{\n{:indent$} name: \"{}\",\n",
"",
arg.get_id(),
escape_string(arg.get_id().as_str()),
indent = indent
)
.unwrap();
Expand Down Expand Up @@ -404,7 +417,7 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
"{:indent$}{{\n{:indent$} name: \"{}\",\n",
"",
"",
value.get_name(),
escape_string(value.get_name()),
indent = indent + 4,
)
.unwrap();
Expand All @@ -424,7 +437,7 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
&mut buffer,
"{:indent$}\"{}\",\n",
"",
value.get_name(),
escape_string(value.get_name()),
indent = indent + 4,
)
.unwrap();
Expand Down Expand Up @@ -476,11 +489,11 @@ fn arg_conflicts(cmd: &Command, arg: &Arg) -> Vec<String> {

for conflict in cmd.get_arg_conflicts_with(arg) {
if let Some(s) = conflict.get_short() {
res.push(format!("-{}", s));
res.push(format!("-{}", escape_string(&s.to_string())));
}

if let Some(l) = conflict.get_long() {
res.push(format!("--{}", l));
res.push(format!("--{}", escape_string(l)));
}
}

Expand Down

0 comments on commit fedda32

Please sign in to comment.