deepseek-tui 0.8.32

Terminal UI for DeepSeek
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
//! Note command: manage persistent workspace notes.

use crate::tui::app::App;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

use super::CommandResult;

const USAGE: &str = "/note <text> | /note add <text> | /note list | /note show <n> | /note edit <n> <text> | /note remove <n> | /note clear | /note path";

/// Manage the persistent workspace notes file.
pub fn note(app: &mut App, content: Option<&str>) -> CommandResult {
    let input = match content {
        Some(c) => c.trim(),
        None => {
            return CommandResult::error(format!("Usage: {USAGE}"));
        }
    };

    if input.is_empty() {
        return CommandResult::error("Note content cannot be empty");
    }

    let notes_path = notes_path(app);
    let (command, rest) = split_command(input);

    match command.to_ascii_lowercase().as_str() {
        "add" => append_note_command(&notes_path, rest),
        "list" => list_notes_command(&notes_path),
        "show" => show_note_command(&notes_path, rest),
        "edit" => edit_note_command(&notes_path, rest),
        "remove" | "rm" | "delete" => remove_note_command(&notes_path, rest),
        "clear" => clear_notes_command(&notes_path),
        "path" => CommandResult::message(format!("Notes path: {}", notes_path.display())),
        "help" => CommandResult::message(format!("Usage: {USAGE}")),
        _ => append_note_command(&notes_path, Some(input)),
    }
}

fn notes_path(app: &App) -> PathBuf {
    app.workspace.join(".deepseek").join("notes.md")
}

fn split_command(input: &str) -> (&str, Option<&str>) {
    match input.find(char::is_whitespace) {
        Some(index) => (&input[..index], Some(input[index..].trim())),
        None => (input, None),
    }
}

fn append_note_command(notes_path: &Path, content: Option<&str>) -> CommandResult {
    let Some(note_content) = content.map(str::trim).filter(|content| !content.is_empty()) else {
        return CommandResult::error("Usage: /note add <text>");
    };

    match append_note(notes_path, note_content) {
        Ok(()) => CommandResult::message(format!("Note appended to {}", notes_path.display())),
        Err(e) => CommandResult::error(e),
    }
}

fn list_notes_command(notes_path: &Path) -> CommandResult {
    let notes = match read_notes(notes_path) {
        Ok(notes) => notes,
        Err(e) => return CommandResult::error(e),
    };

    if notes.is_empty() {
        return CommandResult::message(format!("No notes found at {}", notes_path.display()));
    }

    let mut output = format!("Notes in {}:", notes_path.display());
    for (index, note) in notes.iter().enumerate() {
        output.push_str(&format!("\n\n{}. {}", index + 1, note_preview(note)));
    }
    CommandResult::message(output)
}

fn show_note_command(notes_path: &Path, rest: Option<&str>) -> CommandResult {
    let notes = match read_notes(notes_path) {
        Ok(notes) => notes,
        Err(e) => return CommandResult::error(e),
    };
    let index = match parse_note_index(rest, notes.len(), "/note show <n>") {
        Ok(index) => index,
        Err(e) => return CommandResult::error(e),
    };

    CommandResult::message(format!("Note {}:\n\n{}", index + 1, notes[index]))
}

fn edit_note_command(notes_path: &Path, rest: Option<&str>) -> CommandResult {
    let Some(rest) = rest else {
        return CommandResult::error("Usage: /note edit <n> <text>");
    };
    let (index_text, new_content) = match split_command(rest) {
        (index_text, Some(new_content)) if !new_content.trim().is_empty() => {
            (index_text, new_content.trim())
        }
        _ => return CommandResult::error("Usage: /note edit <n> <text>"),
    };

    let mut notes = match read_notes(notes_path) {
        Ok(notes) => notes,
        Err(e) => return CommandResult::error(e),
    };
    let index = match parse_note_index(Some(index_text), notes.len(), "/note edit <n> <text>") {
        Ok(index) => index,
        Err(e) => return CommandResult::error(e),
    };

    notes[index] = new_content.to_string();
    match write_notes(notes_path, &notes) {
        Ok(()) => CommandResult::message(format!(
            "Note {} updated in {}",
            index + 1,
            notes_path.display()
        )),
        Err(e) => CommandResult::error(e),
    }
}

fn remove_note_command(notes_path: &Path, rest: Option<&str>) -> CommandResult {
    let mut notes = match read_notes(notes_path) {
        Ok(notes) => notes,
        Err(e) => return CommandResult::error(e),
    };
    let index = match parse_note_index(rest, notes.len(), "/note remove <n>") {
        Ok(index) => index,
        Err(e) => return CommandResult::error(e),
    };

    notes.remove(index);
    match write_notes(notes_path, &notes) {
        Ok(()) => CommandResult::message(format!(
            "Note {} removed from {}",
            index + 1,
            notes_path.display()
        )),
        Err(e) => CommandResult::error(e),
    }
}

fn clear_notes_command(notes_path: &Path) -> CommandResult {
    match write_notes(notes_path, &[]) {
        Ok(()) => CommandResult::message(format!("Notes cleared in {}", notes_path.display())),
        Err(e) => CommandResult::error(e),
    }
}

fn append_note(notes_path: &Path, note_content: &str) -> Result<(), String> {
    ensure_notes_parent(notes_path)?;

    let mut file = match fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(notes_path)
    {
        Ok(f) => f,
        Err(e) => {
            return Err(format!("Failed to open notes file: {e}"));
        }
    };

    // Write separator and note content
    if let Err(e) = writeln!(file, "\n---\n{}", note_content) {
        return Err(format!("Failed to write note: {e}"));
    }

    Ok(())
}

fn read_notes(notes_path: &Path) -> Result<Vec<String>, String> {
    match fs::read_to_string(notes_path) {
        Ok(content) => Ok(parse_notes(&content)),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()),
        Err(e) => Err(format!("Failed to read notes file: {e}")),
    }
}

fn write_notes(notes_path: &Path, notes: &[String]) -> Result<(), String> {
    ensure_notes_parent(notes_path)?;
    let content = notes
        .iter()
        .map(|note| format!("---\n{}", note.trim()))
        .collect::<Vec<_>>()
        .join("\n\n");
    fs::write(notes_path, content).map_err(|e| format!("Failed to write notes file: {e}"))
}

fn ensure_notes_parent(notes_path: &Path) -> Result<(), String> {
    if let Some(parent) = notes_path.parent() {
        fs::create_dir_all(parent).map_err(|e| format!("Failed to create notes directory: {e}"))?;
    }
    Ok(())
}

fn parse_notes(content: &str) -> Vec<String> {
    let mut notes = Vec::new();
    let mut current = Vec::new();
    let mut saw_separator = false;

    for line in content.lines() {
        if line.trim() == "---" {
            if saw_separator || !current.is_empty() {
                push_note(&mut notes, &current);
                current.clear();
            }
            saw_separator = true;
        } else if saw_separator || !line.trim().is_empty() {
            current.push(line);
        }
    }

    if saw_separator {
        push_note(&mut notes, &current);
    } else {
        let trimmed = content.trim();
        if !trimmed.is_empty() {
            notes.push(trimmed.to_string());
        }
    }

    notes
}

fn push_note(notes: &mut Vec<String>, lines: &[&str]) {
    let note = lines.join("\n").trim().to_string();
    if !note.is_empty() {
        notes.push(note);
    }
}

fn note_preview(note: &str) -> String {
    let first_line = note
        .lines()
        .find_map(|line| {
            let trimmed = line.trim();
            (!trimmed.is_empty()).then_some(trimmed)
        })
        .unwrap_or("(empty note)");
    if note.lines().filter(|line| !line.trim().is_empty()).count() > 1 {
        format!("{first_line} ...")
    } else {
        first_line.to_string()
    }
}

fn parse_note_index(rest: Option<&str>, note_count: usize, usage: &str) -> Result<usize, String> {
    let Some(index_text) = rest.map(str::trim).filter(|text| !text.is_empty()) else {
        return Err(format!("Usage: {usage}"));
    };
    let index = index_text
        .parse::<usize>()
        .map_err(|_| format!("Invalid note number: {index_text}"))?;
    if index == 0 || index > note_count {
        return Err(format!(
            "Note number {index} out of range; there are {note_count} note(s)"
        ));
    }
    Ok(index - 1)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::tui::app::{App, TuiOptions};
    use std::path::PathBuf;
    use tempfile::TempDir;

    fn create_test_app_with_tmpdir(tmpdir: &TempDir) -> App {
        let options = TuiOptions {
            model: "deepseek-v4-pro".to_string(),
            workspace: tmpdir.path().to_path_buf(),
            config_path: None,
            config_profile: None,
            allow_shell: false,
            use_alt_screen: true,
            use_mouse_capture: false,
            use_bracketed_paste: true,
            max_subagents: 1,
            skills_dir: tmpdir.path().join("skills"),
            memory_path: tmpdir.path().join("memory.md"),
            notes_path: tmpdir.path().join("notes.txt"),
            mcp_config_path: tmpdir.path().join("mcp.json"),
            use_memory: false,
            start_in_agent_mode: false,
            skip_onboarding: true,
            yolo: false,
            resume_session_id: None,
            initial_input: None,
        };
        App::new(options, &Config::default())
    }

    fn notes_path(tmpdir: &TempDir) -> PathBuf {
        tmpdir.path().join(".deepseek").join("notes.md")
    }

    fn message(result: CommandResult) -> String {
        result.message.expect("command message")
    }

    #[test]
    fn test_note_without_content_returns_error() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        let result = note(&mut app, None);
        assert!(result.message.is_some());
        assert!(result.message.unwrap().contains("Usage: /note"));
    }

    #[test]
    fn test_note_with_empty_content_returns_error() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        let result = note(&mut app, Some("   "));
        assert!(result.message.is_some());
        assert!(result.message.unwrap().contains("cannot be empty"));
    }

    #[test]
    fn test_note_appends_to_file() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        let result = note(&mut app, Some("Test note content"));
        assert!(result.message.is_some());
        let msg = message(result);
        assert!(msg.contains("Note appended to"));

        let notes_path = notes_path(&tmpdir);
        assert!(notes_path.exists());
        let content = std::fs::read_to_string(&notes_path).unwrap();
        assert!(content.contains("Test note content"));
    }

    #[test]
    fn test_note_multiple_appends() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        note(&mut app, Some("First note"));
        note(&mut app, Some("Second note"));

        let notes_path = notes_path(&tmpdir);
        let content = std::fs::read_to_string(&notes_path).unwrap();
        assert!(content.contains("First note"));
        assert!(content.contains("Second note"));
        // Should have two separators
        assert_eq!(content.matches("---").count(), 2);
    }

    #[test]
    fn test_note_list_numbers_entries_without_storing_numbers() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        note(&mut app, Some("Alpha note"));
        note(&mut app, Some("Beta note"));

        let listed = message(note(&mut app, Some("list")));
        assert!(listed.contains("1. Alpha note"));
        assert!(listed.contains("2. Beta note"));

        let content = std::fs::read_to_string(notes_path(&tmpdir)).unwrap();
        assert!(content.contains("Alpha note"));
        assert!(!content.contains("1. Alpha note"));
    }

    #[test]
    fn test_note_show_displays_full_multiline_note() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        note(&mut app, Some("add first line\nsecond line"));

        let shown = message(note(&mut app, Some("show 1")));
        assert!(shown.contains("Note 1:"));
        assert!(shown.contains("first line\nsecond line"));
    }

    #[test]
    fn test_note_edit_updates_numbered_entry() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        note(&mut app, Some("First note"));
        note(&mut app, Some("Second note"));

        let edited = message(note(&mut app, Some("edit 2 Updated second note")));
        assert!(edited.contains("Note 2 updated"));

        let content = std::fs::read_to_string(notes_path(&tmpdir)).unwrap();
        assert!(content.contains("First note"));
        assert!(content.contains("Updated second note"));
        assert!(!content.contains("Second note"));
    }

    #[test]
    fn test_note_remove_renumbers_remaining_entries() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        note(&mut app, Some("First note"));
        note(&mut app, Some("Second note"));
        note(&mut app, Some("Third note"));

        let removed = message(note(&mut app, Some("remove 2")));
        assert!(removed.contains("Note 2 removed"));

        let listed = message(note(&mut app, Some("list")));
        assert!(listed.contains("1. First note"));
        assert!(listed.contains("2. Third note"));
        assert!(!listed.contains("Second note"));
    }

    #[test]
    fn test_note_clear_empties_file() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        note(&mut app, Some("First note"));

        let cleared = message(note(&mut app, Some("clear")));
        assert!(cleared.contains("Notes cleared"));
        assert_eq!(std::fs::read_to_string(notes_path(&tmpdir)).unwrap(), "");
    }

    #[test]
    fn test_note_path_prints_workspace_notes_file() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);

        let path = message(note(&mut app, Some("path")));
        assert!(path.contains(".deepseek"));
        assert!(path.contains("notes.md"));
    }

    #[test]
    fn test_note_rejects_out_of_range_index() {
        let tmpdir = TempDir::new().unwrap();
        let mut app = create_test_app_with_tmpdir(&tmpdir);
        note(&mut app, Some("Only note"));

        let result = note(&mut app, Some("show 2"));
        assert!(result.message.unwrap().contains("out of range"));
    }

    #[test]
    fn test_parse_notes_handles_plain_text_before_separator() {
        let parsed = parse_notes("plain note\n---\nseparated note");
        assert_eq!(parsed, vec!["plain note", "separated note"]);
    }
}