fur_cli/commands/
tree.rs

1use std::fs;
2use std::path::Path;
3use serde_json::Value;
4use clap::Parser;
5use std::collections::HashMap;
6use crate::frs::avatars::resolve_avatar;
7use colored::*;
8
9#[derive(Parser, Clone)]
10pub struct TreeArgs {
11    #[clap(skip)]
12    pub conversation_override: Option<String>,
13}
14
15pub fn run_tree(args: TreeArgs) {
16    let fur_dir = Path::new(".fur");
17    let index_path = fur_dir.join("index.json");
18
19    if !index_path.exists() {
20        eprintln!("{}", "🚨 .fur/ not found. Run `fur new` first.".red().bold());
21        return;
22    }
23
24    // Load index and conversation
25    let index_data: Value =
26        serde_json::from_str(&fs::read_to_string(&index_path).expect("❌ Cannot read index.json"))
27            .unwrap();
28
29    let conversation_id = if let Some(ref override_id) = args.conversation_override {
30        override_id
31    } else {
32        index_data["active_thread"].as_str().unwrap_or("")
33    };
34    let convo_path = fur_dir.join("threads").join(format!("{}.json", conversation_id));
35    let conversation_data: Value =
36        serde_json::from_str(&fs::read_to_string(&convo_path).expect("❌ Cannot read conversation"))
37            .unwrap();
38
39    // Load avatars.json once
40    let avatars: Value = serde_json::from_str(
41        &fs::read_to_string(fur_dir.join("avatars.json")).unwrap_or_else(|_| "{}".to_string())
42    ).unwrap_or(serde_json::json!({}));
43
44    println!(
45        "{} {}",
46        "🌳 Conversation Tree:".bold().cyan(),
47        conversation_data["title"].as_str().unwrap_or("Untitled").green().bold()
48    );
49
50    if let Some(messages) = conversation_data["messages"].as_array() {
51        let id_to_message = load_conversation_messages(&fur_dir, &conversation_data);
52        for (idx, msg_id) in messages.iter().enumerate() {
53            if let Some(mid) = msg_id.as_str() {
54                render_message(&id_to_message, mid, "", idx == messages.len() - 1, &avatars);
55            }
56        }
57    }
58}
59
60fn load_conversation_messages(fur_dir: &Path, conversation: &Value) -> HashMap<String, Value> {
61    let mut id_to_message = HashMap::new();
62    let mut to_visit: Vec<String> = conversation["messages"]
63        .as_array()
64        .unwrap_or(&vec![])
65        .iter()
66        .filter_map(|id| id.as_str().map(|s| s.to_string()))
67        .collect();
68
69    while let Some(mid) = to_visit.pop() {
70        let path = fur_dir.join("messages").join(format!("{}.json", mid));
71        if let Ok(content) = fs::read_to_string(path) {
72            if let Ok(json) = serde_json::from_str::<Value>(&content) {
73                // enqueue children + branches
74                if let Some(children) = json["children"].as_array() {
75                    for c in children {
76                        if let Some(cid) = c.as_str() {
77                            to_visit.push(cid.to_string());
78                        }
79                    }
80                }
81                if let Some(branches) = json["branches"].as_array() {
82                    for block in branches {
83                        if let Some(arr) = block.as_array() {
84                            for c in arr {
85                                if let Some(cid) = c.as_str() {
86                                    to_visit.push(cid.to_string());
87                                }
88                            }
89                        }
90                    }
91                }
92                id_to_message.insert(mid.clone(), json);
93            }
94        }
95    }
96    id_to_message
97}
98
99
100fn render_message(
101    id_to_message: &HashMap<String, Value>,
102    msg_id: &str,
103    prefix: &str,
104    is_last: bool,
105    avatars: &Value,
106) {
107    if let Some(msg) = id_to_message.get(msg_id) {
108        // build tree connector
109        let branch_symbol = if is_last { "└──" } else { "├──" };
110        let tree_prefix = format!("{}{}", prefix, branch_symbol.bright_green());
111
112        let avatar_key = msg["avatar"].as_str().unwrap_or("???");
113        let (name, emoji) = resolve_avatar(avatars, avatar_key);
114
115        let text = msg.get("text").and_then(|v| v.as_str()).unwrap_or_else(|| {
116            msg.get("markdown")
117                .and_then(|v| v.as_str())
118                .unwrap_or("<no content>")
119        });
120
121        let id_display = msg_id[..8].to_string();
122
123        if msg.get("markdown").is_some() {
124            println!(
125                "{} {} {} {} {} {}",
126                tree_prefix,
127                "[Root]".cyan(),
128                emoji.yellow(),
129                format!("[{}]", name).bright_yellow(),
130                text.white(),
131                format!("📄 {}", id_display).magenta()
132            );
133        } else {
134            println!(
135                "{} {} {} {} {}",
136                tree_prefix,
137                "[Root]".cyan(),
138                emoji.yellow(),
139                format!("[{}]", name).bright_yellow(),
140                format!("{} {}", text.white(), id_display.bright_black())
141            );
142        }
143
144        // Lifetime-safe empty vec
145        let empty: Vec<Value> = Vec::new();
146        let children = msg["children"].as_array().unwrap_or(&empty);
147        let branches = msg["branches"].as_array().unwrap_or(&empty);
148
149        // merge both: if branches exist, prefer them
150        if !branches.is_empty() {
151            for (_b_idx, branch) in branches.iter().enumerate() {
152                if let Some(arr) = branch.as_array() {
153                    for (i, child_id) in arr.iter().enumerate() {
154                        if let Some(cid) = child_id.as_str() {
155                            let new_prefix = format!(
156                                "{}{}   ",
157                                prefix,
158                                if is_last { "    " } else { "│  " }.bright_green()
159                            );
160                            render_message(id_to_message, cid, &new_prefix, i == arr.len() - 1, avatars);
161                        }
162                    }
163                }
164            }
165        } else {
166            for (i, child_id) in children.iter().enumerate() {
167                if let Some(cid) = child_id.as_str() {
168                    let new_prefix = format!(
169                        "{}{}   ",
170                        prefix,
171                        if is_last { "    " } else { "│  " }.bright_green()
172                    );
173                    render_message(id_to_message, cid, &new_prefix, i == children.len() - 1, avatars);
174                }
175            }
176        }
177    }
178}