Skip to main content

fur_cli/commands/
chat.rs

1use std::fs;
2use std::io::{self, Read, Write};
3use std::path::Path;
4use chrono::Utc;
5use colored::*;
6
7use crate::commands::jot::{self, JotArgs};
8
9/// Interactive chat-style jot for long / structured messages
10pub fn run_chat(avatar: Option<String>) {
11    println!("{}", "💬 Write / Copy-Paste your Markdown or text below.".bright_cyan());
12    println!("{}", "↪ Finish with Ctrl+D (Linux/macOS) or Ctrl+Z then Enter (Windows).".white());
13    println!("{}", "↪ Press Ctrl+C to cancel.".white());
14
15    // --- Capture multi-line input
16    let mut buffer = String::new();
17    io::stdin().read_to_string(&mut buffer).unwrap();
18
19    if buffer.trim().is_empty() {
20        println!("⚠️ No content provided. Aborting.");
21        return;
22    }
23
24    // --- Confirm
25    print!("You have finished writing. Continue? [Y/n]: ");
26    io::stdout().flush().unwrap();
27    let mut confirm = String::new();
28    io::stdin().read_line(&mut confirm).unwrap();
29    if confirm.trim().eq_ignore_ascii_case("n") {
30        println!("❌ Cancelled.");
31        return;
32    }
33
34    // --- Filename suggestion
35    let default_name = format!("chats/CHAT-{}.md", Utc::now().format("%Y%m%d-%H%M%S"));
36    println!("Save as? (default: {})", default_name);
37    print!("> ");
38    io::stdout().flush().unwrap();
39    let mut fname = String::new();
40    io::stdin().read_line(&mut fname).unwrap();
41    let fname = fname.trim();
42    let path = if fname.is_empty() { default_name } else { fname.to_string() };
43
44    // Ensure chats dir exists
45    if let Some(parent) = Path::new(&path).parent() {
46        fs::create_dir_all(parent).ok();
47    }
48
49    fs::write(&path, &buffer).expect("❌ Failed to write file");
50    println!("💾 Saved to {}", path.green());
51
52    // --- Reuse jot logic to attach to conversation
53    let args = JotArgs {
54        avatar,
55        positional_text: None,
56        text: None,
57        markdown: Some(path),
58        img: None,
59        parent: None,
60    };
61    jot::run_jot(args);
62}