diff --git a/examples/methods.ion b/examples/methods.ion index 4b1545de49ee324d8bb00c99fe0f70f2cf9720ea..333379a91beed79fbf897c8dafa81d9a3cebfd45 100644 --- a/examples/methods.ion +++ b/examples/methods.ion @@ -25,4 +25,5 @@ echo $starts_with("one two", two) echo $ends_with("one two", one) echo $ends_with("one two", two) echo $contains("one two three", two) -echo $contains("one two three", four) \ No newline at end of file +echo $contains("one two three", four) +echo $repeat("one ", 5) \ No newline at end of file diff --git a/examples/methods.out b/examples/methods.out index 7a385707b03fc1e895c186a8ff5f9c7adcfa4bbc..b53f57f35a67ac9fc2b283015ceffe20398ae775 100644 --- a/examples/methods.out +++ b/examples/methods.out @@ -56,3 +56,4 @@ e 1 1 0 +one one one one one diff --git a/src/parser/shell_expand/words.rs b/src/parser/shell_expand/words.rs index 89bc9d006b8e9cbf9ce461f629ff7a15242848ae..645a75297bbd39ce8e7fa57ba2b3d7ec3935d53a 100644 --- a/src/parser/shell_expand/words.rs +++ b/src/parser/shell_expand/words.rs @@ -493,6 +493,22 @@ impl<'a> StringMethod<'a> { "parent" => path_eval!(parent), "to_lowercase" => string_case!(to_lowercase), "to_uppercase" => string_case!(to_uppercase), + "repeat" => { + let pattern = expand_string(pattern, expand, false).join(" "); + match pattern.parse::<usize>() { + Ok(repeat) => { + if let Some(value) = expand.vars.get_var(variable) { + output.push_str(&value.repeat(repeat)); + } else if is_expression(variable) { + let value = expand_string(variable, &expand, false).join(" "); + output.push_str(&value.repeat(repeat)); + } + }, + Err(_) => { + eprintln!("ion: value supplied to $repeat() is not a valid number"); + } + } + } "replace" => { let pattern = ArgumentSplitter::new(pattern) .map(|x| expand_string(x, expand, false).join(" "))