use std::{
collections::HashMap,
fs::File,
io::{self, Write},
};
use crate::engines::tplot::TPlotStyle;
#[derive(Debug, Clone)]
pub struct OutgroupState {
pub taxa: Vec<usize>,
}
impl OutgroupState {
pub fn new(mut taxa: Vec<usize>) -> Self {
taxa.sort_unstable();
taxa.dedup();
Self { taxa }
}
pub fn first(&self) -> Option<usize> {
self.taxa.first().copied()
}
pub fn describe_with_dataset(&self, ds: Option<&crate::engines::dataset::Dataset>) -> String {
if self.taxa.is_empty() {
return "none".to_string();
}
match ds {
Some(ds) => {
let parts = self
.taxa
.iter()
.map(|&t| {
let name = ds.taxa.get(t).cloned().unwrap_or_else(|| format!("?{t}"));
format!("{t}:{name}")
})
.collect::<Vec<_>>();
parts.join(" ")
}
None => self.taxa.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" "),
}
}
}
#[derive(Debug)]
pub struct State {
pub display_to_terminal: bool,
pub log_file: Option<File>,
pub dataset: Option<crate::engines::dataset::Dataset>,
pub outgroup: Option<OutgroupState>,
pub char_config: Option<crate::engines::ccode::CharConfig>,
pub apo_optimizations: Option<Vec<crate::ast::ApoOptimization>>,
pub ccode_slots: std::collections::HashMap<u8, crate::engines::ccode::CharConfig>,
pub current_tree_slot: Option<u8>,
pub tree_slots: HashMap<u8, crate::engines::trees::TreeSet>,
pub tree_plot_style: TPlotStyle,
pub show_tree_node_labels: bool,
pub ttags_enabled: bool,
pub ttags_target_tree: Option<crate::engines::trees::Tree>,
pub ttags_labels: HashMap<usize, String>,
pub log_path: Option<std::path::PathBuf>,
pub log_enabled: bool,
pub procedure_exec_enabled: bool,
pub watch_enabled: bool,
pub should_quit: bool,
}
impl State {
pub fn new() -> Self {
Self {
display_to_terminal: false,
log_path: None,
log_file: None,
log_enabled: false,
procedure_exec_enabled: true,
should_quit: false,
dataset: None,
outgroup: None,
char_config: None,
apo_optimizations: None,
ccode_slots: HashMap::new(),
current_tree_slot: None,
tree_slots: HashMap::new(),
tree_plot_style: TPlotStyle::Unicode,
show_tree_node_labels: false,
ttags_enabled: false,
ttags_target_tree: None,
ttags_labels: HashMap::new(),
watch_enabled: false,
}
}
pub fn write_line(&mut self, s: &str) -> io::Result<()> {
if self.display_to_terminal {
println!("{s}");
}
if self.log_enabled {
if let Some(f) = &mut self.log_file {
writeln!(f, "{s}")?;
}
}
Ok(())
}
pub fn ensure_dataset(&self) -> Result<(), String> {
if self.dataset.is_some() {
Ok(())
} else {
Err("dataset not loaded (use xread ... ; or procedure <file>; first)".to_string())
}
}
pub const WORKING_TREE_SLOT: u8 = 0;
pub fn working_tree_set(&self) -> Option<&crate::engines::trees::TreeSet> {
self.tree_slots.get(&Self::WORKING_TREE_SLOT)
}
pub fn append_to_working_tree_set(
&mut self,
title: String,
mut trees: Vec<crate::engines::trees::Tree>,
) {
let slot = Self::WORKING_TREE_SLOT;
if let Some(ts) = self.tree_slots.get_mut(&slot) {
ts.trees.append(&mut trees);
if ts.title.trim().is_empty() {
ts.title = title;
}
} else {
self.tree_slots.insert(slot, crate::engines::trees::TreeSet::new(title, trees));
}
self.current_tree_slot = Some(slot);
self.refresh_working_title_auto();
}
pub fn set_working_tree_set(&mut self, ts: crate::engines::trees::TreeSet) {
let slot = Self::WORKING_TREE_SLOT;
self.tree_slots.insert(slot, ts);
self.current_tree_slot = Some(slot);
}
pub fn refresh_working_title_auto(&mut self) {
let slot = Self::WORKING_TREE_SLOT;
let Some(ts) = self.tree_slots.get_mut(&slot) else {
return;
};
let n = ts.trees.len();
if ts.title.trim().to_ascii_lowercase().starts_with("set of ") {
ts.title = format!("set of {} trees", n);
}
}
pub fn working_tree_set_len(&self) -> usize {
self.working_tree_set().map(|ts| ts.trees.len()).unwrap_or(0)
}
pub fn current_tree_set(&self) -> Option<&crate::engines::trees::TreeSet> {
self.current_tree_slot.and_then(|slot| self.tree_slots.get(&slot))
}
pub fn clear_all_trees(&mut self) {
self.current_tree_slot = None;
self.tree_slots.clear();
self.ttags_target_tree = None;
self.ttags_labels.clear();
}
pub fn append_to_current_tree_set(
&mut self,
title: String,
mut trees: Vec<crate::engines::trees::Tree>,
) {
let slot = self.current_tree_slot.unwrap_or(0);
if let Some(ts) = self.tree_slots.get_mut(&slot) {
ts.trees.append(&mut trees);
} else {
self.tree_slots.insert(slot, crate::engines::trees::TreeSet::new(title, trees));
self.current_tree_slot = Some(slot);
}
}
}