1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use nu_engine::CallExt;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
ast::Call, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct SeqChar;
impl Command for SeqChar {
fn name(&self) -> &str {
"seq char"
}
fn usage(&self) -> &str {
"Print sequence of chars"
}
fn signature(&self) -> Signature {
Signature::build("seq char")
.rest("rest", SyntaxShape::String, "sequence chars")
.named(
"separator",
SyntaxShape::String,
"separator character (defaults to \\n)",
Some('s'),
)
.named(
"terminator",
SyntaxShape::String,
"terminator character (defaults to \\n)",
Some('t'),
)
.category(Category::Generators)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "sequence a to e with newline separator",
example: "seq char a e",
result: Some(Value::List {
vals: vec![
Value::test_string('a'),
Value::test_string('b'),
Value::test_string('c'),
Value::test_string('d'),
Value::test_string('e'),
],
span: Span::test_data(),
}),
},
Example {
description: "sequence a to e with pipe separator separator",
example: "seq char -s '|' a e",
result: Some(Value::test_string("a|b|c|d|e")),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
seq_char(engine_state, stack, call)
}
}
fn is_single_character(ch: &str) -> bool {
ch.is_ascii() && ch.len() == 1 && ch.chars().all(char::is_alphabetic)
}
fn seq_char(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let separator: Option<Spanned<String>> = call.get_flag(engine_state, stack, "separator")?;
let terminator: Option<Spanned<String>> = call.get_flag(engine_state, stack, "terminator")?;
let rest_inputs: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
let (start_ch, end_ch) = if rest_inputs.len() != 2
|| !is_single_character(&rest_inputs[0].item)
|| !is_single_character(&rest_inputs[1].item)
{
return Err(ShellError::GenericError(
"seq char required two character parameters".into(),
"needs parameter".into(),
Some(call.head),
None,
Vec::new(),
));
} else {
(
rest_inputs[0]
.item
.chars()
.next()
.expect("seq char input must contains 2 inputs"),
rest_inputs[1]
.item
.chars()
.next()
.expect("seq char input must contains 2 inputs"),
)
};
let sep: String = match separator {
Some(s) => {
if s.item == r"\t" {
'\t'.to_string()
} else if s.item == r"\n" {
'\n'.to_string()
} else if s.item == r"\r" {
'\r'.to_string()
} else {
let vec_s: Vec<char> = s.item.chars().collect();
if vec_s.is_empty() {
return Err(ShellError::GenericError(
"Expected a single separator char from --separator".into(),
"requires a single character string input".into(),
Some(s.span),
None,
Vec::new(),
));
};
vec_s.iter().collect()
}
}
_ => '\n'.to_string(),
};
let terminator: String = match terminator {
Some(t) => {
if t.item == r"\t" {
'\t'.to_string()
} else if t.item == r"\n" {
'\n'.to_string()
} else if t.item == r"\r" {
'\r'.to_string()
} else {
let vec_t: Vec<char> = t.item.chars().collect();
if vec_t.is_empty() {
return Err(ShellError::GenericError(
"Expected a single terminator char from --terminator".into(),
"requires a single character string input".into(),
Some(t.span),
None,
Vec::new(),
));
};
vec_t.iter().collect()
}
}
_ => '\n'.to_string(),
};
let span = call.head;
run_seq_char(start_ch, end_ch, sep, terminator, span)
}
fn run_seq_char(
start_ch: char,
end_ch: char,
sep: String,
terminator: String,
span: Span,
) -> Result<PipelineData, ShellError> {
let mut result_vec = vec![];
for current_ch in start_ch as u8..end_ch as u8 + 1 {
result_vec.push((current_ch as char).to_string())
}
let return_list = (sep == "\n" || sep == "\r") && (terminator == "\n" || terminator == "\r");
if return_list {
let result = result_vec
.into_iter()
.map(|x| Value::String { val: x, span })
.collect::<Vec<Value>>();
Ok(Value::List { vals: result, span }.into_pipeline_data())
} else {
let mut result = result_vec.join(&sep);
result.push_str(&terminator);
let result = result.lines().collect();
Ok(Value::String { val: result, span }.into_pipeline_data())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SeqChar {})
}
}