juglans 0.2.16

Compiler and runtime for Juglans Workflow Language
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
// src/ui/editor.rs
//! Custom multiline editor with Shift+Enter for newlines

use anyhow::Result;
use crossterm::{
    cursor,
    event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
    execute, queue,
    style::{Color, Print, SetForegroundColor, ResetColor},
    terminal::{self, Clear, ClearType},
};
use std::io::{stdout, Write};

pub struct MultilineEditor {
    lines: Vec<String>,
    cursor_row: usize,
    cursor_col: usize,
    start_row: Option<u16>,  // Tracks the starting row of the input box
}

impl MultilineEditor {
    pub fn new() -> Self {
        Self {
            lines: vec![String::new()],
            cursor_row: 0,
            cursor_col: 0,
            start_row: None,
        }
    }

    /// Edit multiline text
    /// Enter: submit
    /// Shift+Enter: new line
    /// Ctrl+C: cancel
    pub fn edit(&mut self, agent_name: &str, chat_id: Option<&str>) -> Result<Option<String>> {
        let mut stdout = stdout();

        // Get current cursor position
        let (_, term_height) = terminal::size()?;

        // Make room for the input box: calculate required lines
        let total_height = 5u16;  // top border + input line + bottom border + status bar + help

        // Ensure enough space
        for _ in 0..total_height {
            println!();
        }
        stdout.flush()?;

        // Record the starting row of the input box (fixed position from bottom)
        self.start_row = Some(term_height.saturating_sub(total_height));

        // Enter raw mode (keep cursor visible)
        terminal::enable_raw_mode()?;

        let result = self.edit_loop(agent_name, chat_id);

        // Exit raw mode, clear input area
        if let Some(start) = self.start_row {
            for i in 0..(total_height + self.lines.len() as u16) {
                queue!(stdout, cursor::MoveTo(0, start + i), Clear(ClearType::CurrentLine))?;
            }
        }

        terminal::disable_raw_mode()?;

        // Reset state
        self.start_row = None;

        result
    }

    fn edit_loop(&mut self, agent_name: &str, chat_id: Option<&str>) -> Result<Option<String>> {
        let mut stdout = stdout();

        // Get terminal size
        let (term_width, _) = terminal::size()?;

        loop {
            // Redraw interface
            self.render(&mut stdout, term_width, agent_name, chat_id)?;

            // Read keyboard event
            if let Event::Key(key) = event::read()? {
                match self.handle_key(key)? {
                    EditorAction::Submit => {
                        // Submit content
                        let content = self.lines.join("\n");
                        self.clear();
                        return Ok(Some(content));
                    }
                    EditorAction::Cancel => {
                        self.clear();
                        return Ok(None);
                    }
                    EditorAction::Exit => {
                        self.clear();
                        return Ok(None);
                    }
                    EditorAction::Continue => {
                        // Continue editing
                    }
                }
            }
        }
    }

    fn handle_key(&mut self, key: KeyEvent) -> Result<EditorAction> {
        match (key.code, key.modifiers) {
            // Enter: submit
            (KeyCode::Enter, KeyModifiers::NONE) => {
                Ok(EditorAction::Submit)
            }

            // Shift+Enter: new line
            (KeyCode::Enter, KeyModifiers::SHIFT) => {
                self.insert_newline();
                Ok(EditorAction::Continue)
            }

            // Ctrl+C: cancel
            (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
                Ok(EditorAction::Cancel)
            }

            // Ctrl+D: exit
            (KeyCode::Char('d'), KeyModifiers::CONTROL) => {
                Ok(EditorAction::Exit)
            }

            // Backspace: delete character
            (KeyCode::Backspace, _) => {
                self.backspace();
                Ok(EditorAction::Continue)
            }

            // Delete: delete character to the right
            (KeyCode::Delete, _) => {
                self.delete();
                Ok(EditorAction::Continue)
            }

            // Left arrow
            (KeyCode::Left, _) => {
                self.move_cursor_left();
                Ok(EditorAction::Continue)
            }

            // Right arrow
            (KeyCode::Right, _) => {
                self.move_cursor_right();
                Ok(EditorAction::Continue)
            }

            // Up arrow
            (KeyCode::Up, _) => {
                self.move_cursor_up();
                Ok(EditorAction::Continue)
            }

            // Down arrow
            (KeyCode::Down, _) => {
                self.move_cursor_down();
                Ok(EditorAction::Continue)
            }

            // Home: beginning of line
            (KeyCode::Home, _) => {
                self.cursor_col = 0;
                Ok(EditorAction::Continue)
            }

            // End: end of line
            (KeyCode::End, _) => {
                self.cursor_col = self.current_line().chars().count();
                Ok(EditorAction::Continue)
            }

            // Input character
            (KeyCode::Char(c), _) => {
                self.insert_char(c);
                Ok(EditorAction::Continue)
            }

            _ => Ok(EditorAction::Continue),
        }
    }

    fn render(&self, stdout: &mut impl Write, term_width: u16, agent_name: &str, chat_id: Option<&str>) -> Result<()> {
        let (_, term_height) = terminal::size()?;

        // Use the recorded start row, or calculate one if not set
        let start_row = self.start_row.unwrap_or_else(|| {
            let input_lines = self.lines.len() as u16;
            let total_height = input_lines + 4;
            term_height.saturating_sub(total_height)
        });

        let input_lines = self.lines.len() as u16;
        let total_height = input_lines + 4;  // top border + input lines + bottom border + status bar + help

        // Clear input area (including possibly expanded lines)
        for i in 0..(total_height + 5) {  // Clear a few extra lines to prevent artifacts
            queue!(
                stdout,
                cursor::MoveTo(0, start_row + i),
                Clear(ClearType::CurrentLine)
            )?;
        }

        // Draw top border
        queue!(
            stdout,
            cursor::MoveTo(0, start_row),
            SetForegroundColor(Color::DarkGrey),
            Print("".repeat(term_width as usize)),
            ResetColor,
        )?;

        // Draw input content
        for (i, line) in self.lines.iter().enumerate() {
            queue!(
                stdout,
                cursor::MoveTo(0, start_row + 1 + i as u16)
            )?;
            if i == 0 {
                queue!(stdout, Print("> "), Print(line))?;
            } else {
                queue!(stdout, Print("  "), Print(line))?;
            }
        }

        // Draw bottom border
        let bottom_border = start_row + 1 + input_lines;
        queue!(
            stdout,
            cursor::MoveTo(0, bottom_border),
            SetForegroundColor(Color::DarkGrey),
            Print("".repeat(term_width as usize)),
            ResetColor,
        )?;

        // Status bar position
        let status_row = bottom_border + 1;

        // Draw status bar (agent name and chat id)
        queue!(
            stdout,
            cursor::MoveTo(0, status_row),
            SetForegroundColor(Color::DarkGrey),
            Print(format!("{}", agent_name)),
        )?;

        if let Some(id) = chat_id {
            queue!(
                stdout,
                Print(" ["),
                Print(&id[..8.min(id.len())]),
                Print("]"),
            )?;
        }

        queue!(stdout, ResetColor)?;

        // Draw help info
        queue!(
            stdout,
            cursor::MoveTo(0, status_row + 1),
            SetForegroundColor(Color::DarkGrey),
            Print("Enter: submit │ Shift+Enter: new line │ Ctrl+C: cancel │ Ctrl+D: exit"),
            ResetColor,
        )?;

        // Move cursor to editing position
        let cursor_x = 2 + self.cursor_col;
        let cursor_y = start_row + 1 + self.cursor_row as u16;
        queue!(stdout, cursor::MoveTo(cursor_x as u16, cursor_y))?;

        stdout.flush()?;
        Ok(())
    }

    fn insert_char(&mut self, c: char) {
        let line = &mut self.lines[self.cursor_row];
        // Find the correct byte index
        let byte_pos = line.char_indices()
            .nth(self.cursor_col)
            .map(|(pos, _)| pos)
            .unwrap_or(line.len());
        line.insert(byte_pos, c);
        self.cursor_col += 1;
    }

    fn insert_newline(&mut self) {
        let current_line = self.lines[self.cursor_row].clone();

        // Find the correct byte index to split at
        let byte_pos = current_line.char_indices()
            .nth(self.cursor_col)
            .map(|(pos, _)| pos)
            .unwrap_or(current_line.len());

        let (before, after) = current_line.split_at(byte_pos);

        self.lines[self.cursor_row] = before.to_string();
        self.lines.insert(self.cursor_row + 1, after.to_string());

        self.cursor_row += 1;
        self.cursor_col = 0;
    }

    fn backspace(&mut self) {
        if self.cursor_col > 0 {
            let line = &mut self.lines[self.cursor_row];
            // Find the byte position of the character to delete
            let byte_pos = line.char_indices()
                .nth(self.cursor_col - 1)
                .map(|(pos, _)| pos)
                .unwrap_or(0);
            line.remove(byte_pos);
            self.cursor_col -= 1;
        } else if self.cursor_row > 0 {
            // Merge with previous line
            let current = self.lines.remove(self.cursor_row);
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].chars().count();
            self.lines[self.cursor_row].push_str(&current);
        }
    }

    fn delete(&mut self) {
        let line = &mut self.lines[self.cursor_row];
        let char_count = line.chars().count();

        if self.cursor_col < char_count {
            // Find the byte position of the character to delete
            let byte_pos = line.char_indices()
                .nth(self.cursor_col)
                .map(|(pos, _)| pos)
                .unwrap_or(line.len());
            if byte_pos < line.len() {
                line.remove(byte_pos);
            }
        } else if self.cursor_row < self.lines.len() - 1 {
            // Merge with next line
            let next = self.lines.remove(self.cursor_row + 1);
            self.lines[self.cursor_row].push_str(&next);
        }
    }

    fn move_cursor_left(&mut self) {
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
        } else if self.cursor_row > 0 {
            self.cursor_row -= 1;
            self.cursor_col = self.current_line().len();
        }
    }

    fn move_cursor_right(&mut self) {
        let char_count = self.current_line().chars().count();
        if self.cursor_col < char_count {
            self.cursor_col += 1;
        } else if self.cursor_row < self.lines.len() - 1 {
            self.cursor_row += 1;
            self.cursor_col = 0;
        }
    }

    fn move_cursor_up(&mut self) {
        if self.cursor_row > 0 {
            self.cursor_row -= 1;
            let char_count = self.current_line().chars().count();
            self.cursor_col = self.cursor_col.min(char_count);
        }
    }

    fn move_cursor_down(&mut self) {
        if self.cursor_row < self.lines.len() - 1 {
            self.cursor_row += 1;
            let char_count = self.current_line().chars().count();
            self.cursor_col = self.cursor_col.min(char_count);
        }
    }

    fn current_line(&self) -> &String {
        &self.lines[self.cursor_row]
    }

    fn clear(&mut self) {
        self.lines = vec![String::new()];
        self.cursor_row = 0;
        self.cursor_col = 0;
        self.start_row = None;
    }
}

enum EditorAction {
    Submit,
    Cancel,
    Exit,
    Continue,
}