use colored::Colorize;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
pub fn save_link(path: &str) -> Result<(), std::io::Error> {
let history_file = "/tmp/.me_history";
let context_file = "/tmp/.me_context";
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(history_file)?;
writeln!(file, "{}", path)?;
fs::write(context_file, path)?;
println!("{} {}", "π Linking to:".bright_cyan().bold(), path);
println!("{} '{}'", "β
Context updated to".bright_green().bold(), path);
Ok(())
}
pub fn show_history() {
match fs::read_to_string("/tmp/.me_history") {
Ok(data) => {
println!("{}", "π Link history:".bright_white().bold());
for (i, line) in data.lines().enumerate() {
println!("{} {}", format!("{:>2}.", i + 1).bright_black(), line);
}
}
Err(_) => println!("π No link history found."),
}
}
pub fn go_back() {
let history_file = "/tmp/.me_history";
if let Ok(data) = fs::read_to_string(history_file) {
let mut lines: Vec<_> = data.lines().collect();
if lines.len() > 1 {
lines.pop();
if let Some(prev) = lines.last() {
fs::write("/tmp/.me_context", prev).unwrap_or_default();
fs::write(history_file, lines.join("\n")).unwrap_or_default();
println!("π Returned to '{}'", prev);
}
} else {
println!("β οΈ No previous context.");
}
} else {
println!("π No history file found.");
}
}