sparcli 0.1.1

Lightweight, cross-platform toolkit for styled CLI output and interactive input widgets
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Numeric input with bounds, step adjustment and a calculator mode.

use crate::core::render::Rendered;
use crate::core::style::Style;
use crate::core::terminal::is_input_tty;
use crate::core::theme::theme;
use crate::error::{Result, SparcliError};
use crate::input::Outcome;
use crate::input::event::{CrosstermSource, EventSource, InputEvent, KeyPress};
use crate::input::field::{error_line, field_line, value_line};
use crate::input::guard::TerminalGuard;
use crate::input::line_edit::LineEditor;
use crate::input::prompt::{Flow, run_prompt};

/// Mutable state of a running number prompt.
struct State {
    editor: LineEditor,
    error: Option<String>,
}

/// A numeric input prompt with optional calculator expressions.
pub struct NumberInput {
    prompt: String,
    initial: f64,
    min: f64,
    max: f64,
    step: f64,
    decimals: usize,
    calculator: bool,
}

impl NumberInput {
    /// Creates a number prompt with the given label.
    pub fn new(prompt: impl Into<String>) -> Self {
        Self {
            prompt: prompt.into(),
            initial: 0.0,
            min: f64::NEG_INFINITY,
            max: f64::INFINITY,
            step: 1.0,
            decimals: 0,
            calculator: false,
        }
    }

    /// Sets the initial value.
    #[must_use]
    pub fn initial(mut self, value: f64) -> Self {
        self.initial = value;
        self
    }

    /// Sets the inclusive `[min, max]` bounds.
    #[must_use]
    pub fn range(mut self, min: f64, max: f64) -> Self {
        self.min = min;
        self.max = max;
        self
    }

    /// Sets the step used by Up/Down.
    #[must_use]
    pub fn step(mut self, step: f64) -> Self {
        self.step = step;
        self
    }

    /// Sets the number of decimal places shown by step adjustments.
    #[must_use]
    pub fn decimals(mut self, decimals: usize) -> Self {
        self.decimals = decimals;
        self
    }

    /// Enables calculator expressions (`+ - * / ( )`).
    #[must_use]
    pub fn calculator(mut self) -> Self {
        self.calculator = true;
        self
    }

    /// Runs the prompt on the real terminal.
    ///
    /// # Errors
    /// Returns [`SparcliError::NoTerminal`] without an interactive terminal,
    /// or [`SparcliError::Io`] on a terminal failure.
    pub fn run(self) -> Result<Outcome<f64>> {
        if !is_input_tty() {
            return Err(SparcliError::NoTerminal);
        }
        let _guard = TerminalGuard::new()?;
        let mut source = CrosstermSource;
        self.run_with(&mut source)
    }

    /// Runs the prompt against any event source (used for tests).
    fn run_with(&self, source: &mut impl EventSource) -> Result<Outcome<f64>> {
        let mut state = State {
            editor: LineEditor::new(&self.format(self.initial), false),
            error: None,
        };
        run_prompt(
            source,
            &mut state,
            |state, final_frame| self.render(state, final_frame),
            |state, event| self.handle(state, event),
        )
    }

    /// Renders the prompt's static frame without running it (for previews
    /// and README screenshots).
    pub fn frame(&self) -> Rendered {
        let state = State {
            editor: LineEditor::new(&self.format(self.initial), false),
            error: None,
        };
        self.render(&state, false)
    }

    /// Builds the prompt frame.
    fn render(&self, state: &State, final_frame: bool) -> Rendered {
        let theme = theme();
        let value = state.editor.value();
        if final_frame {
            let line = value_line(&self.prompt, &value, Style::new(), &theme);
            return Rendered::new(vec![line]);
        }
        let mut lines = vec![field_line(
            &self.prompt,
            &value,
            state.editor.cursor(),
            Style::new(),
            &theme,
        )];
        if let Some(error) = &state.error {
            lines.push(error_line(error, &theme));
        }
        Rendered::new(lines)
    }

    /// Handles one event.
    fn handle(&self, state: &mut State, event: InputEvent) -> Flow<f64> {
        let InputEvent::Key(key) = event else {
            return Flow::Continue;
        };
        self.handle_key(state, key)
    }

    /// Handles a single key press.
    fn handle_key(&self, state: &mut State, key: KeyPress) -> Flow<f64> {
        use crate::input::event::KeyCode::{
            Backspace, Char, Delete, Down, Enter, Esc, Left, Right, Up,
        };
        if key.ctrl && key.code == Char('u') {
            state.editor.kill_to_line_start();
            return Flow::Continue;
        }
        match key.code {
            Esc => return Flow::Cancel,
            Enter => return self.submit(state),
            Up => self.adjust(state, self.step),
            Down => self.adjust(state, -self.step),
            Left => state.editor.move_left(false),
            Right => state.editor.move_right(false),
            Backspace => state.editor.backspace(),
            Delete => state.editor.delete(),
            Char(c) if self.accepts(c) => {
                state.editor.insert_char(c);
                state.error = None;
            }
            _ => {}
        }
        Flow::Continue
    }

    /// Returns whether `ch` is a valid input character.
    fn accepts(&self, ch: char) -> bool {
        let numeric = ch.is_ascii_digit() || matches!(ch, '.' | ',' | '-');
        let calc = matches!(ch, '+' | '*' | '/' | '(' | ')' | ' ');
        numeric || (self.calculator && calc)
    }

    /// Adjusts the current value by `delta` and reformats the field.
    fn adjust(&self, state: &mut State, delta: f64) {
        let current = parse_number(&state.editor.value()).unwrap_or(0.0);
        let value = self.clamp(current + delta);
        state.editor.set_value(&self.format(value));
        state.error = None;
    }

    /// Evaluates the field and submits the clamped value.
    fn submit(&self, state: &mut State) -> Flow<f64> {
        let text = state.editor.value();
        let parsed = if self.calculator {
            eval(&text)
        } else {
            parse_number(&text).ok_or_else(|| "not a number".to_string())
        };
        match parsed {
            Ok(value) => Flow::Submit(self.clamp(value)),
            Err(message) => {
                state.error = Some(message);
                Flow::Continue
            }
        }
    }

    /// Clamps `value` to the configured range.
    fn clamp(&self, value: f64) -> f64 {
        value.clamp(self.min, self.max)
    }

    /// Formats `value` with the configured number of decimals.
    fn format(&self, value: f64) -> String {
        format!("{value:.*}", self.decimals)
    }
}

/// Parses a number, accepting `,` or `.` as the decimal separator.
fn parse_number(text: &str) -> Option<f64> {
    let normalized = text.trim().replace(',', ".");
    if normalized.is_empty() {
        return None;
    }
    normalized.parse().ok()
}

/// Evaluates an arithmetic expression with `+ - * / ( )`.
///
/// Accepts `,` or `.` as the decimal separator. Returns an error message on
/// malformed input or division by zero.
///
/// # Examples
/// ```
/// # use sparcli::input::number::eval;
/// assert_eq!(eval("2 + 3 * 4").unwrap(), 14.0);
/// ```
pub fn eval(expr: &str) -> std::result::Result<f64, String> {
    let normalized = expr.replace(',', ".");
    let mut parser = Calc {
        chars: normalized.chars().collect(),
        pos: 0,
    };
    let value = parser.expression()?;
    parser.skip_spaces();
    if parser.pos != parser.chars.len() {
        return Err("unexpected trailing input".to_string());
    }
    Ok(value)
}

/// A minimal recursive-descent arithmetic parser.
struct Calc {
    chars: Vec<char>,
    pos: usize,
}

impl Calc {
    /// Parses `term (('+'|'-') term)*`.
    fn expression(&mut self) -> std::result::Result<f64, String> {
        let mut value = self.term()?;
        loop {
            self.skip_spaces();
            match self.peek() {
                Some('+') => {
                    self.pos += 1;
                    value += self.term()?;
                }
                Some('-') => {
                    self.pos += 1;
                    value -= self.term()?;
                }
                _ => break,
            }
        }
        Ok(value)
    }

    /// Parses `factor (('*'|'/') factor)*`.
    fn term(&mut self) -> std::result::Result<f64, String> {
        let mut value = self.factor()?;
        loop {
            self.skip_spaces();
            match self.peek() {
                Some('*') => {
                    self.pos += 1;
                    value *= self.factor()?;
                }
                Some('/') => {
                    self.pos += 1;
                    let divisor = self.factor()?;
                    if divisor == 0.0 {
                        return Err("division by zero".to_string());
                    }
                    value /= divisor;
                }
                _ => break,
            }
        }
        Ok(value)
    }

    /// Parses a number, parenthesized expression or unary minus.
    fn factor(&mut self) -> std::result::Result<f64, String> {
        self.skip_spaces();
        match self.peek() {
            Some('(') => {
                self.pos += 1;
                let value = self.expression()?;
                self.skip_spaces();
                if self.peek() != Some(')') {
                    return Err("missing closing parenthesis".to_string());
                }
                self.pos += 1;
                Ok(value)
            }
            Some('-') => {
                self.pos += 1;
                Ok(-self.factor()?)
            }
            _ => self.number(),
        }
    }

    /// Parses a decimal number literal.
    fn number(&mut self) -> std::result::Result<f64, String> {
        let start = self.pos;
        while matches!(self.peek(), Some(c) if c.is_ascii_digit() || c == '.') {
            self.pos += 1;
        }
        let text: String = self.chars[start..self.pos].iter().collect();
        text.parse().map_err(|_| "invalid number".to_string())
    }

    /// Returns the current character without consuming it.
    fn peek(&self) -> Option<char> {
        self.chars.get(self.pos).copied()
    }

    /// Skips spaces.
    fn skip_spaces(&mut self) {
        while self.peek() == Some(' ') {
            self.pos += 1;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::input::event::{KeyCode, ScriptedSource};

    #[test]
    fn types_and_submits_number() {
        let outcome = NumberInput::new("n")
            .run_with(&mut ScriptedSource::keys([
                KeyCode::Char('4'),
                KeyCode::Char('2'),
                KeyCode::Enter,
            ]))
            .unwrap();
        assert_eq!(outcome, Outcome::Submitted(42.0));
    }

    #[test]
    fn up_arrow_adds_step() {
        let outcome = NumberInput::new("n")
            .initial(5.0)
            .step(2.0)
            .run_with(&mut ScriptedSource::keys([KeyCode::Up, KeyCode::Enter]))
            .unwrap();
        assert_eq!(outcome, Outcome::Submitted(7.0));
    }

    #[test]
    fn value_is_clamped_to_range() {
        let outcome = NumberInput::new("n")
            .range(0.0, 10.0)
            .run_with(&mut ScriptedSource::keys([
                KeyCode::Char('9'),
                KeyCode::Char('9'),
                KeyCode::Enter,
            ]))
            .unwrap();
        assert_eq!(outcome, Outcome::Submitted(10.0));
    }

    #[test]
    fn esc_cancels() {
        let outcome = NumberInput::new("n")
            .run_with(&mut ScriptedSource::keys([KeyCode::Esc]))
            .unwrap();
        assert_eq!(outcome, Outcome::Cancelled);
    }

    #[test]
    fn eval_respects_precedence() {
        assert_eq!(eval("2 + 3 * 4").unwrap(), 14.0);
        assert_eq!(eval("(2 + 3) * 4").unwrap(), 20.0);
    }

    #[test]
    fn eval_reports_division_by_zero() {
        assert!(eval("1 / 0").is_err());
    }

    #[test]
    fn eval_accepts_comma_decimal() {
        assert_eq!(eval("1,5 + 1,5").unwrap(), 3.0);
    }
}