use abstracttui::prelude::*;
use abstracttui::widgets::{Block, Table};
use crate::config::Snapshot;
use crate::store::Loadable;
use super::sections::with_snapshot;
use super::util::{line, span, span_bold};
use super::widths;
use super::{config_identity_line, screen_for_section, Ctx};
pub fn view(_cx: Scope, ctx: &Ctx, theme: Signal<&'static abstracttui::theme::Theme>) -> View {
let store = ctx.store;
let ui = ctx.ui;
let identity = dyn_view(LayoutStyle::column().shrink(0.0), move || {
let t = theme.get().tokens;
let rows = Element::new()
.style(LayoutStyle::column())
.child(line({
let mut spans = vec![span(" file ", t.text_faint)];
spans.extend(config_identity_line(&t, &store));
spans
}))
.child(line({
let mut spans = vec![span(" cli ", t.text_faint)];
match store.cli.get() {
Some(info) => {
spans.push(span(info.bin.display().to_string(), t.text));
spans.push(span(format!(" ({})", info.source), t.text_faint));
}
None => {
spans.push(span_bold("not found", t.warn));
spans.push(span(
" — file mirror works; derived views and writes need it ($ABSTRACTCORE_CLI)",
t.text_muted,
));
}
}
spans
}))
.build();
let mut col = Element::new().style(LayoutStyle::column()).child(rows);
for l in agreement_lines(&t, &store) {
col = col.child(line(l));
}
if let Some(fb) = store.python_fallback.get() {
col = col
.child(line(vec![span_bold(
" ✗ PYTHON REFUSES THIS FILE — every abstractcore run backs it up and uses DEFAULTS",
t.error,
)]))
.child(line(vec![span(
format!(" {fb}"),
t.text_muted,
)]));
}
Block::new()
.title("Config identity")
.child(col.build())
.element(&t)
.build()
});
let table = dyn_view_scoped(LayoutStyle::default().grow(1.0), move |gcx| {
let t = theme.get().tokens;
with_snapshot(&t, &store, |snap, defaults_only| {
let mut col = Element::new().style(LayoutStyle::column());
if defaults_only {
col = col.child(line(vec![span(
" no config file yet — Python runs on built-in defaults; the first write creates it",
t.info,
)]));
}
for refusal in snap.python_refusals.iter().take(4) {
col = col.child(line(vec![
span_bold(" ✗ Python will refuse this file: ", t.error),
span(refusal.clone(), t.text),
]));
}
col = col.child(sections_table(gcx, Jump { ui, store }, &t, snap));
if !snap.unknown_sections.is_empty() {
col = col.child(line(vec![
span_bold(" ⚠ unknown sections: ", t.warn),
span(snap.unknown_sections.join(", "), t.text),
span(
" — preserved by this console; a Python-side save drops them",
t.text_muted,
),
]));
}
col.build()
})
});
Element::new()
.style(LayoutStyle::column().grow(1.0))
.child(identity)
.child(table)
.build()
}
#[derive(Clone, Copy)]
struct Jump {
ui: super::UiState,
store: crate::store::Store,
}
pub fn same_path(a: &str, b: &str) -> bool {
use std::path::Path;
Path::new(a).components().eq(Path::new(b).components())
}
fn agreement_lines(t: &TokenSet, store: &crate::store::Store) -> Vec<Vec<super::util::SpanSpec>> {
let mine = match store.cfg.get() {
Loadable::Ready(m) => Some(m.path.path.display().to_string()),
_ => None,
};
let cli_echo = match store.routes.get() {
Loadable::Ready(r) => r.config_file,
_ => None,
};
let lead = || span(" agree ", t.text_faint);
if store.python_fallback.with(|f| f.is_some()) {
return vec![vec![
lead(),
span_bold("✗ ", t.error),
span(
"the CLI reads the same path but REFUSES its content (see above)",
t.text,
),
]];
}
match (mine, cli_echo) {
(Some(a), Some(b)) if same_path(&a, &b) => vec![vec![
lead(),
span_bold("✓ ", t.ok),
span(
"the abstractcore CLI reads the same file this console shows",
t.text_muted,
),
]],
(Some(a), Some(b)) => vec![
vec![
lead(),
span_bold("✗ DIFFERENT FILES ", t.error),
span(format!("— the CLI reads {b}"), t.text),
],
vec![
span(" ", t.text_faint),
span(
format!("console shows {a} — check ABSTRACTCORE_CONFIG_FILE/_DIR"),
t.text_muted,
),
],
],
_ => vec![vec![
lead(),
span(
"— unknown until the CLI view loads (r reloads)",
t.text_muted,
),
]],
}
}
fn sections_table(cx: Scope, jump: Jump, t: &TokenSet, snap: &Snapshot) -> View {
use crate::schema::SectionKind;
struct Row {
name: &'static str,
state: String,
details: String,
}
let mut rows_data: Vec<Row> = Vec::new();
for sv in &snap.sections {
let (state, details) = match sv.spec.kind {
SectionKind::Fields => {
let set = sv.set_count();
let broken = sv.broken_count();
if sv.spec.name == "api_keys" {
let set_keys: Vec<&str> = sv
.fields
.iter()
.filter(|f| matches!(f.state, crate::config::FieldState::Set))
.map(|f| f.key)
.collect();
if broken > 0 {
rows_data.push(Row {
name: sv.spec.name,
state: format!("✗ {broken} broken"),
details: first_broken_detail(sv),
});
continue;
}
let total = sv.fields.len();
let state = if set_keys.is_empty() {
"· none".to_string()
} else {
format!("● {} of {total}", set_keys.len())
};
(
state,
if set_keys.is_empty() {
"no keys stored".to_string()
} else {
set_keys.join(", ")
},
)
} else if broken > 0 {
(format!("✗ {broken} broken"), first_broken_detail(sv))
} else if set > 0 {
(format!("● {set} set"), set_preview(sv))
} else {
("· default".to_string(), String::new())
}
}
SectionKind::Routes => {
let in_file = snap.routes_in_file.len();
let details = match jump.store.routes.get() {
Loadable::Ready(r) if r.ok => {
format!(
"{} of {} routes configured",
r.configured_count(),
r.rows.len()
)
}
Loadable::Loading => "⟳ loading the derived view…".to_string(),
Loadable::Failed(_) => {
format!("{in_file} route entries in file (CLI view failed)")
}
_ => format!("{in_file} route entries in file"),
};
let state = if in_file > 0 {
format!("● {in_file} set")
} else {
"· default".to_string()
};
(state, details)
}
SectionKind::Email => {
let details = match jump.store.email.get() {
Loadable::Ready(doc) => super::email::summary_line(&doc),
Loadable::Loading => "⟳ loading the email view…".to_string(),
_ => match &snap.email_account_in_file {
Some(a) => format!("{a} (Email screen for status)"),
None => "not connected".to_string(),
},
};
let state = if snap.email_account_in_file.is_some() {
"● connected".to_string()
} else {
"· none".to_string()
};
(state, details)
}
SectionKind::Profiles => {
let n = snap.profiles_in_file;
let details = match jump.store.profiles.get() {
Loadable::Ready(p) => {
let with_key = p.profiles.iter().filter(|x| x.api_key_set).count();
let ids: Vec<String> = p.profiles.iter().map(|x| x.id.clone()).collect();
format!("{} ({with_key} with key): {}", ids.len(), ids.join(", "))
}
Loadable::Loading => "⟳ loading the derived view…".to_string(),
Loadable::Failed(_) => format!("{n} in file (CLI view failed)"),
_ => format!("{n} in file"),
};
let state = if n > 0 {
format!("● {n} set")
} else {
"· none".to_string()
};
(state, details)
}
};
rows_data.push(Row {
name: sv.spec.name,
state,
details,
});
}
let w = abstracttui::app::use_viewport(cx).get().w;
let mut rows: Vec<Vec<String>> = rows_data
.iter()
.map(|r| {
let mut row = vec![r.name.to_string(), r.state.clone()];
row.push(r.details.clone());
if w >= 96 {
row.push(format!("→ {}", super::SCREENS[screen_for_section(r.name)]));
}
row
})
.collect();
let mut rules = vec![
widths::ColRule::head("section", 14),
widths::ColRule::head("state", 11),
widths::ColRule::head("details", 24),
];
if w >= 96 {
rules.push(widths::ColRule::head("edit on", 12));
}
let cols = widths::columns(&rules, &mut rows, w);
let names: Vec<&'static str> = rows_data.iter().map(|r| r.name).collect();
Table::new(cols)
.rows(rows)
.selection(jump.ui.overview_sel)
.on_activate(move |i| {
if let Some(name) = names.get(i) {
let target = screen_for_section(name);
jump.ui.screen.set(target);
jump.store
.notice
.set(Some(format!("→ {}", super::SCREENS[target])));
}
})
.layout(LayoutStyle::default().grow(1.0))
.element(cx, t)
.autofocus()
.build()
}
fn first_broken_detail(sv: &crate::config::SectionView) -> String {
sv.fields
.iter()
.find_map(|f| match &f.state {
crate::config::FieldState::Broken(reason) => Some(format!("{}: {reason}", f.key)),
_ => None,
})
.unwrap_or_default()
}
fn set_preview(sv: &crate::config::SectionView) -> String {
let parts: Vec<String> = sv
.fields
.iter()
.filter(|f| matches!(f.state, crate::config::FieldState::Set))
.take(3)
.map(|f| format!("{}={}", f.key, f.display))
.collect();
parts.join(" ")
}