wasmind_cli 0.1.0

Command-line interface for Wasmind AI agent coordination system
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! This is a direct copy and paste from: https://github.com/veeso/tui-realm-textarea/blob/main/src/lib.rs
//! I'm putting this here as we will need to edit this in the future (hopefully near future lol)
//!
//! # tui-realm-textarea
//!
//! [tui-realm-textarea](https://github.com/veeso/tui-realm-textarea) is a
//! [tui-realm](https://github.com/veeso/tui-realm) implementation of a textarea component.
//! The tree engine is based on [Orange-trees](https://docs.rs/orange-trees/).
//!
//! ## Get Started
//!
//! ### Adding `tui-realm-textarea` as dependency
//!
//! ```toml
//! tui-realm-textarea = "2"
//! ```
//!
//! Or if you don't use **Crossterm**, define the backend as you would do with tui-realm:
//!
//! ```toml
//! tui-realm-textarea = { version = "2", default-features = false, features = [ "termion" ] }
//! ```
//!
//! #### Features ⚙️

//! These features can be enabled in tui-realm-textarea:
//!
//! - `clipboard` enables system clipboard support
//! - `search` enables the string search in the textarea
//!
//! ## Component API
//!
//! **Commands**:
//!
//! | Cmd                                            | Result         | Behaviour                               |
//! |------------------------------------------------|----------------|-----------------------------------------|
//! | `Custom($TEXTAREA_CMD_NEWLINE)`                | `None`         | Insert newline                          |
//! | `Custom($TEXTAREA_CMD_DEL_LINE_BY_END)`        | `None`         | Delete line by end to current position  |
//! | `Custom($TEXTAREA_CMD_DEL_LINE_BY_HEAD)`       | `None`         | Delete line by head to current position |
//! | `Custom($TEXTAREA_CMD_DEL_WORD)`               | `None`         | Delete the current word                 |
//! | `Custom($TEXTAREA_CMD_DEL_NEXT_WORD)`          | `None`         | Delete the next word                    |
//! | `Custom($TEXTAREA_CMD_MOVE_WORD_FORWARD)`      | `None`         | Move to the next word                   |
//! | `Custom($TEXTAREA_CMD_MOVE_WORD_BACK)`         | `None`         | Move to the previous word               |
//! | `Custom($TEXTAREA_CMD_MOVE_PARAGRAPH_BACK)`    | `None`         | Move to the previous paragraph          |
//! | `Custom($TEXTAREA_CMD_MOVE_PARAGRAPH_FORWARD)` | `None`         | Move to the next paragraph              |
//! | `Custom($TEXTAREA_CMD_MOVE_TOP)`               | `None`         | Move to the beginning of the file       |
//! | `Custom($TEXTAREA_CMD_MOVE_BOTTOM)`            | `None`         | Move to the end of the file             |
//! | `Custom($TEXTAREA_CMD_UNDO)`                   | `None`         | Undo last change                        |
//! | `Custom($TEXTAREA_CMD_REDO)`                   | `None`         | Redo last change                        |
//! | `Custom($TEXTAREA_CMD_PASTE)`                  | `None`         | Paste the current content of the buffer |
//! | `Custom($TEXTAREA_CMD_SEARCH_BACK)`            | `None`         | Go to the previous search match         |
//! | `Custom($TEXTAREA_CMD_SEARCH_FORWARD)`         | `None`         | Go to the next search match             |
//! | `Cancel`                                       | `None`         | Delete next char                        |
//! | `Delete`                                       | `None`         | Delete previous char                    |
//! | `GoTo(Begin)`                                  | `None`         | Go to the head of the line              |
//! | `GoTo(End)`                                    | `None`         | Go to the end of the line               |
//! | `Move(Down)`                                   | `None`         | Move to the line below                  |
//! | `Move(Up)`                                     | `None`         | Move to the line above                  |
//! | `Move(Left)`                                   | `None`         | Move cursor to the left                 |
//! | `Move(Right)`                                  | `None`         | Move cursor to the right                |
//! | `Scroll(Up)`                                   | `None`         | Move by scroll_step lines up            |
//! | `Scroll(Down)`                                 | `None`         | Move by scroll_step lines down          |
//! | `Type(ch)`                                     | `None`         | Type a char in the editor               |
//! | `Submit`                                       | `Submit`       | Get current lines                       |
//!
//! > ❗ Paste command is supported only if the `clipboard` feature is enabled
//!
//! **State**: the state returned is a `Vec(String)` containing the lines in the text area.
//!
//! **Properties**:
//!
//! - `Borders(Borders)`: set borders properties for component
//! - `Custom($TREE_IDENT_SIZE, Size)`: Set space to render for each each depth level
//! - `Custom($TEXTAREA_MAX_HISTORY, Payload(One(Usize)))`: Set the history steps to record
//! - `Custom($TEXTAREA_CURSOR_STYLE, Style)`: Set the cursor style
//! - `Custom($TEXTAREA_CURSOR_LINE_STYLE, Style)`: Set the current line style
//! - `Custom($TEXTAREA_FOOTER_FMT, Payload(Tup2(Str, Style)))`: Set the format and the style for the footer bar
//! - `Custom($TEXTAREA_LINE_NUMBER_STYLE, Style)`: set the style for the line number
//! - `Custom($TEXTAREA_STATUS_FMT, Payload(Tup2(Str, Style)))`: Set the format and the style for the status bar
//! - `Custom($TEXTAREA_SEARCH_PATTERN, String`: Set search pattern
//! - `Custom($TEXTAREA_SEARCH_STYLE, Style`: Set search style
//! - `Custom($TEXTAREA_SINGLE_LINE, Style`: Act as single-line input
//! - `Style(Style)`: Set the general style for the textarea
//! - `Custom($TEXTAREA_TAB_SIZE, Size)`: Set the tab size to display
//! - `FocusStyle(Style)`: inactive style
//! - `ScrollStep(Length)`: Defines the maximum amount of rows to scroll
//! - `Title(Title)`: Set box title
//!
#[cfg(feature = "clipboard")]
use cli_clipboard::{ClipboardContext, ClipboardProvider};
use ratatui::text::Span;
use ratatui::widgets::Widget;
use tui_textarea::{CursorMove, TextArea as TextAreaWidget};
use tuirealm::command::{Cmd, CmdResult, Direction, Position};
use tuirealm::props::{
    Alignment, AttrValue, Attribute, Borders, PropPayload, PropValue, Props, Style, TextModifiers,
};
use tuirealm::ratatui::layout::Rect;
use tuirealm::ratatui::widgets::Block;
use tuirealm::{Frame, MockComponent, State, StateValue};

// -- props
pub const TEXTAREA_CURSOR_LINE_STYLE: &str = "cursor-line-style";
pub const TEXTAREA_CURSOR_STYLE: &str = "cursor-style";
pub const TEXTAREA_LINE_NUMBER_STYLE: &str = "line-number-style";
pub const TEXTAREA_MAX_HISTORY: &str = "max-history";
pub const TEXTAREA_TAB_SIZE: &str = "tab-size";
pub const TEXTAREA_HARD_TAB: &str = "hard-tab";
pub const TEXTAREA_SINGLE_LINE: &str = "single-line";
pub const TEXTAREA_LAYOUT_MARGIN: &str = "layout-margin";
pub const INACTIVE_BORDERS: &str = "inactive-borders";
pub const TITLE_STYLE: &str = "title-style";

// -- cmd
pub const TEXTAREA_CMD_NEWLINE: &str = "0";
pub const TEXTAREA_CMD_DEL_LINE_BY_END: &str = "1";
pub const TEXTAREA_CMD_DEL_LINE_BY_HEAD: &str = "2";
pub const TEXTAREA_CMD_DEL_WORD: &str = "3";
pub const TEXTAREA_CMD_DEL_NEXT_WORD: &str = "4";
pub const TEXTAREA_CMD_MOVE_WORD_FORWARD: &str = "5";
pub const TEXTAREA_CMD_MOVE_WORD_BACK: &str = "6";
pub const TEXTAREA_CMD_MOVE_PARAGRAPH_FORWARD: &str = "7";
pub const TEXTAREA_CMD_MOVE_PARAGRAPH_BACK: &str = "8";
pub const TEXTAREA_CMD_MOVE_TOP: &str = "9";
pub const TEXTAREA_CMD_MOVE_BOTTOM: &str = "a";
pub const TEXTAREA_CMD_UNDO: &str = "b";
pub const TEXTAREA_CMD_REDO: &str = "c";
#[cfg(feature = "clipboard")]
pub const TEXTAREA_CMD_PASTE: &str = "d";
pub const TEXTAREA_CMD_CLEAR: &str = "e";

/// textarea tui-realm component
pub struct TextArea<'a> {
    props: Props,
    widget: TextAreaWidget<'a>,
    /// Act as single-line input
    single_line: bool,
}

impl<I> From<I> for TextArea<'_>
where
    I: IntoIterator,
    I::Item: Into<String>,
{
    fn from(i: I) -> Self {
        Self::new(i.into_iter().map(|s| s.into()).collect::<Vec<String>>())
    }
}

impl Default for TextArea<'_> {
    fn default() -> Self {
        Self::new(Vec::default())
    }
}

impl<'a> TextArea<'a> {
    pub fn new(lines: Vec<String>) -> Self {
        Self {
            props: Props::default(),
            widget: TextAreaWidget::new(lines),
            single_line: false,
        }
    }

    /// Set another style from default to use when component is inactive
    pub fn inactive(mut self, s: Style) -> Self {
        self.attr(Attribute::FocusStyle, AttrValue::Style(s));
        self
    }

    /// Set widget border properties
    pub fn borders(mut self, b: Borders) -> Self {
        self.attr(Attribute::Borders, AttrValue::Borders(b));
        self
    }

    /// Set widget title
    pub fn title<S: AsRef<str>>(mut self, t: S, a: Alignment) -> Self {
        self.attr(
            Attribute::Title,
            AttrValue::Title((t.as_ref().to_string(), a)),
        );
        self
    }

    /// Set scroll step for scrolling command
    pub fn scroll_step(mut self, step: usize) -> Self {
        self.attr(Attribute::ScrollStep, AttrValue::Length(step));
        self
    }

    /// Set how many modifications are remembered for undo/redo. Setting 0 disables undo/redo.
    pub fn max_histories(mut self, max: usize) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_MAX_HISTORY),
            AttrValue::Payload(PropPayload::One(PropValue::Usize(max))),
        );
        self
    }

    /// Set text editor cursor style
    pub fn cursor_style(mut self, s: Style) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_CURSOR_STYLE),
            AttrValue::Style(s),
        );
        self
    }

    /// Set text editor style for selected line
    pub fn cursor_line_style(mut self, s: Style) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_CURSOR_LINE_STYLE),
            AttrValue::Style(s),
        );
        self
    }

    /// Set text editor style for line numbers
    pub fn line_number_style(mut self, s: Style) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_LINE_NUMBER_STYLE),
            AttrValue::Style(s),
        );
        self
    }

    /// Set text style for editor
    pub fn style(mut self, s: Style) -> Self {
        self.attr(Attribute::Style, AttrValue::Style(s));
        self
    }

    /// Set `<TAB>` size
    pub fn tab_length(mut self, l: u8) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_TAB_SIZE),
            AttrValue::Size(l as u16),
        );
        self
    }

    /// Set another style from default to use when component is inactive
    pub fn hard_tab(mut self, enabled: bool) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_HARD_TAB),
            AttrValue::Flag(enabled),
        );
        self
    }

    /// Set single-line behavior
    pub fn single_line(mut self, single_line: bool) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_SINGLE_LINE),
            AttrValue::Flag(single_line),
        );
        self
    }

    /// Set margin of layout
    pub fn layout_margin(mut self, margin: u16) -> Self {
        self.attr(
            Attribute::Custom(TEXTAREA_LAYOUT_MARGIN),
            AttrValue::Size(margin),
        );
        self
    }

    // -- private
    fn get_block(&self) -> Option<Block<'a>> {
        let mut block = Block::default();
        if let Some(AttrValue::Title((title, alignment))) = self.query(Attribute::Title) {
            let title_style = self
                .props
                .get_or(
                    Attribute::Custom(TITLE_STYLE),
                    AttrValue::Style(Style::new()),
                )
                .unwrap_style();
            let title = Span::from(title).style(title_style);
            block = block.title(title).title_alignment(alignment);
        }
        let focus = self
            .props
            .get_or(Attribute::Focus, AttrValue::Flag(false))
            .unwrap_flag();

        if focus {
            if let Some(AttrValue::Borders(borders)) = self.query(Attribute::Borders) {
                return Some(
                    block
                        .border_style(borders.style())
                        .border_type(borders.modifiers)
                        .borders(borders.sides),
                );
            }
        } else if let Some(AttrValue::Borders(borders)) =
            self.query(Attribute::Custom(INACTIVE_BORDERS))
        {
            return Some(
                block
                    .border_style(borders.style())
                    .border_type(borders.modifiers)
                    .borders(borders.sides),
            );
        }

        None
    }

    #[cfg(feature = "clipboard")]
    fn paste(&mut self) {
        // get content from context
        if let Ok(Ok(yank)) = ClipboardContext::new().map(|mut ctx| ctx.get_contents()) {
            // TODO: It's desired to set and paste yanked text, but pasting new lines as part of the yanked
            // text is currently not supported by the textarea widget. Therefor, each line is inserted
            // separately. The disadvantage of this workaround is, that each newly inserted line is a
            // separate entry in the history and therefor a separate undo step.
            if self.single_line {
                self.widget.insert_str(yank);
            } else {
                for line in yank.lines() {
                    self.widget.insert_str(line);
                    self.widget.insert_newline();
                }
            }
        }
    }
}

impl MockComponent for TextArea<'_> {
    fn view(&mut self, frame: &mut Frame, mut area: Rect) {
        if self.props.get_or(Attribute::Display, AttrValue::Flag(true)) == AttrValue::Flag(true) {
            // Remove cursor if not in focus
            let focus = self
                .props
                .get_or(Attribute::Focus, AttrValue::Flag(false))
                .unwrap_flag();
            if !focus {
                self.widget.set_cursor_style(Style::reset());
            } else {
                let style = self
                    .props
                    .get_or(
                        Attribute::Custom(TEXTAREA_CURSOR_STYLE),
                        AttrValue::Style(Style::default().add_modifier(TextModifiers::REVERSED)),
                    )
                    .unwrap_style();
                self.widget.set_cursor_style(style);
            }

            area.x += 3;
            area.y += 1;
            area.height -= 2;
            area.width -= 4;
            self.widget.set_block(Block::new());
            frame.render_widget(&self.widget, area);

            area.x -= 3;
            let span = Span::raw(" > ");
            span.render(area, frame.buffer_mut());

            area.y -= 1;
            area.width += 4;
            area.height += 2;
            if let Some(block) = self.get_block() {
                block.render(area, frame.buffer_mut());
            }
        }
    }

    fn query(&self, attr: Attribute) -> Option<AttrValue> {
        self.props.get(attr)
    }

    fn attr(&mut self, attr: Attribute, value: AttrValue) {
        self.props.set(attr, value.clone());
        match (attr, value) {
            (Attribute::Custom(TEXTAREA_CURSOR_STYLE), AttrValue::Style(s)) => {
                self.widget.set_cursor_style(s);
            }
            (Attribute::Custom(TEXTAREA_CURSOR_LINE_STYLE), AttrValue::Style(s)) => {
                self.widget.set_cursor_line_style(s);
            }
            (
                Attribute::Custom(TEXTAREA_MAX_HISTORY),
                AttrValue::Payload(PropPayload::One(PropValue::Usize(max))),
            ) => {
                self.widget.set_max_histories(max);
            }
            (Attribute::Custom(TEXTAREA_LINE_NUMBER_STYLE), AttrValue::Style(s)) => {
                self.widget.set_line_number_style(s);
            }
            (Attribute::Custom(TEXTAREA_TAB_SIZE), AttrValue::Size(size)) => {
                self.widget.set_tab_length(size as u8);
            }
            (Attribute::Custom(TEXTAREA_HARD_TAB), AttrValue::Flag(enabled)) => {
                self.widget.set_hard_tab_indent(enabled);
            }
            (Attribute::Custom(TEXTAREA_SINGLE_LINE), AttrValue::Flag(single_line)) => {
                self.single_line = single_line;
            }
            (Attribute::Style, AttrValue::Style(s)) => {
                self.widget.set_style(s);
            }
            (_, _) => {
                if let Some(block) = self.get_block() {
                    self.widget.set_block(block);
                }
            }
        }
    }

    fn state(&self) -> State {
        State::Vec(
            self.widget
                .lines()
                .iter()
                .map(|x| StateValue::String(x.to_string()))
                .collect(),
        )
    }

    fn perform(&mut self, cmd: Cmd) -> CmdResult {
        match cmd {
            Cmd::Cancel => {
                self.widget.delete_next_char();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_CLEAR) => {
                self.widget.select_all();
                self.widget.cut();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_DEL_LINE_BY_END) => {
                self.widget.delete_line_by_end();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_DEL_LINE_BY_HEAD) => {
                self.widget.delete_line_by_head();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_DEL_NEXT_WORD) => {
                self.widget.delete_next_word();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_DEL_WORD) => {
                self.widget.delete_word();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_MOVE_PARAGRAPH_BACK) => {
                self.widget.move_cursor(CursorMove::ParagraphBack);
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_MOVE_PARAGRAPH_FORWARD) => {
                self.widget.move_cursor(CursorMove::ParagraphForward);
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_MOVE_WORD_BACK) => {
                self.widget.move_cursor(CursorMove::WordBack);
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_MOVE_WORD_FORWARD) => {
                self.widget.move_cursor(CursorMove::WordForward);
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_MOVE_BOTTOM) => {
                if !self.single_line {
                    self.widget.move_cursor(CursorMove::Bottom);
                }
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_MOVE_TOP) => {
                if !self.single_line {
                    self.widget.move_cursor(CursorMove::Top);
                }
                CmdResult::None
            }
            #[cfg(feature = "clipboard")]
            Cmd::Custom(TEXTAREA_CMD_PASTE) => {
                self.paste();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_REDO) => {
                self.widget.redo();
                CmdResult::None
            }
            Cmd::Custom(TEXTAREA_CMD_UNDO) => {
                self.widget.undo();
                CmdResult::None
            }
            Cmd::Delete => {
                self.widget.delete_char();
                CmdResult::None
            }
            Cmd::GoTo(Position::Begin) => {
                self.widget.move_cursor(CursorMove::Head);
                CmdResult::None
            }
            Cmd::GoTo(Position::End) => {
                self.widget.move_cursor(CursorMove::End);
                CmdResult::None
            }
            Cmd::Move(Direction::Down) => {
                if !self.single_line {
                    self.widget.move_cursor(CursorMove::Down);
                }
                CmdResult::None
            }
            Cmd::Move(Direction::Left) => {
                self.widget.move_cursor(CursorMove::Back);
                CmdResult::None
            }
            Cmd::Move(Direction::Right) => {
                self.widget.move_cursor(CursorMove::Forward);
                CmdResult::None
            }
            Cmd::Move(Direction::Up) => {
                if !self.single_line {
                    self.widget.move_cursor(CursorMove::Up);
                }
                CmdResult::None
            }
            Cmd::Scroll(Direction::Down) => {
                if !self.single_line {
                    let step = self
                        .props
                        .get_or(Attribute::ScrollStep, AttrValue::Length(8))
                        .unwrap_length();
                    (0..step).for_each(|_| self.widget.move_cursor(CursorMove::Down));
                }
                CmdResult::None
            }
            Cmd::Scroll(Direction::Up) => {
                if !self.single_line {
                    let step = self
                        .props
                        .get_or(Attribute::ScrollStep, AttrValue::Length(8))
                        .unwrap_length();
                    (0..step).for_each(|_| self.widget.move_cursor(CursorMove::Up));
                }
                CmdResult::None
            }
            Cmd::Type('\t') => {
                self.widget.insert_tab();
                CmdResult::None
            }
            Cmd::Type('\n') | Cmd::Custom(TEXTAREA_CMD_NEWLINE) => {
                if !self.single_line {
                    self.widget.insert_newline();
                }
                CmdResult::None
            }
            Cmd::Type(ch) => {
                self.widget.insert_char(ch);
                CmdResult::None
            }
            Cmd::Submit => CmdResult::Submit(self.state()),
            _ => CmdResult::None,
        }
    }
}