mod author;
mod browse;
mod home;
mod install;
mod theme;
mod updates;
mod widgets;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use bubbletea_rs::event::{QuitMsg, WindowSizeMsg};
use bubbletea_rs::{command, Cmd, Model, Msg, Program};
use lipgloss::{place, CENTER};
use author::AuthorModel;
use browse::BrowseModel;
use home::HomeModel;
use install::InstallModel;
use updates::UpdatesModel;
pub use install::{Parse, Resolve};
use knixl_pipeline::install::HostInfo;
static CONFIG: OnceLock<TuiConfig> = OnceLock::new();
#[derive(Debug, Clone)]
pub struct Verified {
pub preview: String,
pub resolves: Resolve,
pub parses: Parse,
}
pub type VerifyFn = Arc<dyn Fn(&str, &HostInfo) -> Verified + Send + Sync>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuildOutcome {
Ok,
Failed,
Skipped,
}
pub type BuildFn = Arc<dyn Fn(&str, &str) -> BuildOutcome + Send + Sync>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PinOutcome {
Resolved(String),
NotFound,
Unavailable,
Failed,
}
pub type PinFn = Arc<dyn Fn(&str, &str) -> PinOutcome + Send + Sync>;
#[derive(Debug, Clone)]
pub enum StrategyOutcome {
Chosen {
strategy: knixl_lock::model::PinStrategy,
label: String,
},
Failed(String),
}
pub type UpdatesFn = std::sync::Arc<
dyn Fn() -> Result<Vec<knixl_pipeline::updates::HostReport>, String> + Send + Sync,
>;
pub type StrategyFn = Arc<dyn Fn(&str, &str, &str) -> StrategyOutcome + Send + Sync>;
#[derive(Debug, Clone)]
pub struct BrowseModule {
pub node: String,
pub kind: String,
pub doc: String,
pub skeleton: String,
pub manifest: Option<PathBuf>,
}
pub enum Entry {
Hub,
Install {
pkg: String,
strict: bool,
host: Option<String>,
version: Option<String>,
no_abi_check: bool,
},
}
pub enum Outcome {
Quit,
Cancelled,
Install {
host: HostInfo,
pkg: String,
strict: bool,
version: Option<String>,
pin: Option<String>,
no_abi_check: bool,
strategy: Option<knixl_lock::model::PinStrategy>,
strategy_reason: Option<String>,
},
Insert {
host: HostInfo,
node: String,
skeleton: String,
},
Scaffold { name: String, manifest: String },
SaveModule {
path: std::path::PathBuf,
text: String,
},
Updates {
refresh: bool,
if_security: bool,
pins: Vec<(String, String, String)>,
},
}
pub struct TuiConfig {
#[allow(dead_code)]
pub root: PathBuf,
pub hosts: Vec<HostInfo>,
pub entry: Entry,
pub verify: VerifyFn,
pub modules: Vec<BrowseModule>,
pub build: Option<BuildFn>,
pub pin: Option<PinFn>,
pub strategy: Option<StrategyFn>,
pub updates: Option<UpdatesFn>,
}
fn config() -> &'static TuiConfig {
CONFIG.get().expect("TUI config set before run")
}
pub enum Nav {
Stay,
Quit,
Back,
Goto(&'static str),
Apply {
host: HostInfo,
pkg: String,
strict: bool,
version: Option<String>,
pin: Option<String>,
no_abi_check: bool,
strategy: Option<knixl_lock::model::PinStrategy>,
strategy_reason: Option<String>,
},
Insert {
host: HostInfo,
node: String,
skeleton: String,
},
Scaffold {
name: String,
manifest: String,
},
SaveModule {
path: std::path::PathBuf,
text: String,
},
EditModule {
manifest: std::path::PathBuf,
},
ApplyUpdates {
refresh: bool,
if_security: bool,
pins: Vec<(String, String, String)>,
},
}
pub struct Step {
pub nav: Nav,
pub cmd: Option<Cmd>,
}
impl Step {
fn stay() -> Step {
Step {
nav: Nav::Stay,
cmd: None,
}
}
fn nav(nav: Nav) -> Step {
Step { nav, cmd: None }
}
}
enum Screen {
Home(Box<HomeModel>),
Install(Box<InstallModel>),
Browse(Box<BrowseModel>),
Author(Box<AuthorModel>),
Updates(Box<UpdatesModel>),
}
pub struct App {
size: (u16, u16),
screen: Screen,
outcome: Outcome,
}
impl Model for App {
fn init() -> (Self, Option<Cmd>) {
let size = (80, 24);
let (screen, cmd) = match config().entry {
Entry::Install { .. } => {
let (m, cmd) = InstallModel::enter(size);
(Screen::Install(Box::new(m)), cmd)
}
Entry::Hub => (Screen::Home(Box::new(HomeModel::new())), None),
};
let init_cmd = match cmd {
Some(c) => command::batch(vec![command::window_size(), c]),
None => command::window_size(),
};
(
App {
size,
screen,
outcome: Outcome::Quit,
},
Some(init_cmd),
)
}
fn update(&mut self, msg: Msg) -> Option<Cmd> {
if msg.downcast_ref::<QuitMsg>().is_some() {
return None;
}
if let Some(ws) = msg.downcast_ref::<WindowSizeMsg>() {
self.size = (ws.width, ws.height);
}
let step = match &mut self.screen {
Screen::Home(h) => h.update(msg, self.size),
Screen::Install(i) => i.update(msg, self.size),
Screen::Browse(b) => b.update(msg, self.size),
Screen::Author(a) => a.update(msg, self.size),
Screen::Updates(u) => u.update(msg, self.size),
};
self.apply(step)
}
fn view(&self) -> String {
let content = match &self.screen {
Screen::Home(h) => h.view(self.size),
Screen::Install(i) => i.view(self.size),
Screen::Browse(b) => b.view(self.size),
Screen::Author(a) => a.view(self.size),
Screen::Updates(u) => u.view(self.size),
};
let (w, h) = self.size;
place(w as i32, h as i32, CENTER, CENTER, &content, &[])
}
}
impl App {
fn apply(&mut self, step: Step) -> Option<Cmd> {
match step.nav {
Nav::Stay => step.cmd,
Nav::Quit => Some(command::quit()),
Nav::Apply {
host,
pkg,
strict,
version,
pin,
no_abi_check,
strategy,
strategy_reason,
} => {
self.outcome = Outcome::Install {
host,
pkg,
strict,
version,
pin,
no_abi_check,
strategy,
strategy_reason,
};
Some(command::quit())
}
Nav::Insert {
host,
node,
skeleton,
} => {
self.outcome = Outcome::Insert {
host,
node,
skeleton,
};
Some(command::quit())
}
Nav::Scaffold { name, manifest } => {
self.outcome = Outcome::Scaffold { name, manifest };
Some(command::quit())
}
Nav::SaveModule { path, text } => {
self.outcome = Outcome::SaveModule { path, text };
Some(command::quit())
}
Nav::ApplyUpdates {
refresh,
if_security,
pins,
} => {
self.outcome = Outcome::Updates {
refresh,
if_security,
pins,
};
Some(command::quit())
}
Nav::EditModule { manifest } => {
if let Ok(text) = std::fs::read_to_string(&manifest) {
if let Ok(m) = AuthorModel::edit(self.size, manifest, &text) {
self.screen = Screen::Author(Box::new(m));
}
}
None
}
Nav::Back => match config().entry {
Entry::Install { .. } => {
self.outcome = Outcome::Cancelled;
Some(command::quit())
}
Entry::Hub => {
self.screen = Screen::Home(Box::new(HomeModel::new()));
None
}
},
Nav::Goto("install") => {
let (m, cmd) = InstallModel::enter(self.size);
self.screen = Screen::Install(Box::new(m));
cmd
}
Nav::Goto("browse") => {
self.screen = Screen::Browse(Box::new(BrowseModel::enter(self.size)));
None
}
Nav::Goto("author") => {
self.screen = Screen::Author(Box::new(AuthorModel::enter(self.size)));
None
}
Nav::Goto("updates") => {
let (m, cmd) = UpdatesModel::enter(self.size);
self.screen = Screen::Updates(Box::new(m));
cmd
}
Nav::Goto(_) => None,
}
}
}
#[allow(clippy::too_many_arguments)]
pub fn run(
entry: Entry,
root: PathBuf,
hosts: Vec<HostInfo>,
verify: VerifyFn,
modules: Vec<BrowseModule>,
build: Option<BuildFn>,
pin: Option<PinFn>,
strategy: Option<StrategyFn>,
updates: Option<UpdatesFn>,
) -> Result<Outcome, String> {
let _ = CONFIG.set(TuiConfig {
root,
hosts,
entry,
verify,
modules,
build,
pin,
strategy,
updates,
});
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|e| e.to_string())?;
rt.block_on(async {
let program = Program::<App>::builder()
.alt_screen(true)
.build()
.map_err(|e| e.to_string())?;
let app = program.run().await.map_err(|e| e.to_string())?;
Ok(app.outcome)
})
}