use crate::tui::app::App;
use crate::tui::ui::{
render_idle_output, render_run_buffer, render_service_logs, render_watcher,
run_indicator_style, service_indicator_style, watcher_indicator_style,
};
use crate::tui::views::control_center::state::ItemKind;
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
pub trait SidebarItem {
fn indicator_style(&self, name: &str, app: &App) -> Style;
fn render_output(&self, name: &str, app: &App, accent: Color, frame: &mut Frame, area: Rect);
}
pub struct RuntimeItem;
pub struct ServiceItem;
pub struct WatcherItem;
pub struct RecipeItem;
pub struct ScriptItem;
impl SidebarItem for RuntimeItem {
fn indicator_style(&self, _name: &str, app: &App) -> Style {
run_indicator_style(app.lifecycle_run())
}
fn render_output(&self, _name: &str, app: &App, accent: Color, frame: &mut Frame, area: Rect) {
match app.lifecycle_run() {
Some(run) => render_run_buffer(run, accent, frame, area),
None => render_idle_output(
accent,
frame,
area,
"no lifecycle action has run yet",
Some("U up all · D down all · R restart all · S stop all"),
),
}
}
}
impl SidebarItem for ServiceItem {
fn indicator_style(&self, name: &str, app: &App) -> Style {
service_indicator_style(app, name)
}
fn render_output(&self, _name: &str, app: &App, accent: Color, frame: &mut Frame, area: Rect) {
if let Some(service) = app.selected_service() {
render_service_logs(service, accent, frame, area);
}
}
}
impl SidebarItem for WatcherItem {
fn indicator_style(&self, name: &str, app: &App) -> Style {
watcher_indicator_style(app, name)
}
fn render_output(&self, _name: &str, app: &App, accent: Color, frame: &mut Frame, area: Rect) {
if let Some(watcher) = app.selected_watcher() {
render_watcher(watcher, accent, frame, area);
}
}
}
impl SidebarItem for RecipeItem {
fn indicator_style(&self, name: &str, app: &App) -> Style {
run_indicator_style(app.run_for(ItemKind::Recipe, name))
}
fn render_output(&self, _name: &str, app: &App, accent: Color, frame: &mut Frame, area: Rect) {
render_command_output(app, accent, frame, area);
}
}
impl SidebarItem for ScriptItem {
fn indicator_style(&self, name: &str, app: &App) -> Style {
run_indicator_style(app.run_for(ItemKind::Script, name))
}
fn render_output(&self, _name: &str, app: &App, accent: Color, frame: &mut Frame, area: Rect) {
render_command_output(app, accent, frame, area);
}
}
fn render_command_output(app: &App, accent: Color, frame: &mut Frame, area: Rect) {
match app.selected_run() {
Some(run) => render_run_buffer(run, accent, frame, area),
None => render_idle_output(accent, frame, area, "press enter to run", None),
}
}
pub fn for_kind(kind: ItemKind) -> &'static dyn SidebarItem {
match kind {
ItemKind::Runtime => &RuntimeItem,
ItemKind::Service => &ServiceItem,
ItemKind::Watcher => &WatcherItem,
ItemKind::Recipe => &RecipeItem,
ItemKind::Script => &ScriptItem,
}
}