use serde::{Deserialize, Serialize};
use std::time::Instant;
use std::process::Child;
#[derive(Clone, Serialize, Deserialize, Default, Debug)]
pub struct AppConfig {
pub id: String,
pub name: String,
pub icon_emoji: String,
pub working_dir: String,
pub commands: Vec<String>,
}
impl AppConfig {
pub fn new(name: String) -> Self {
Self {
id: crate::utils::uuid_simple(),
name,
..Default::default()
}
}
pub fn has_commands(&self) -> bool {
!self.commands.is_empty()
}
pub fn command_count(&self) -> usize {
self.commands.len()
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AppState {
pub apps: Vec<AppConfig>,
}
impl Default for AppState {
fn default() -> Self {
Self { apps: Vec::new() }
}
}
impl AppState {
pub fn add_app(&mut self, app: AppConfig) {
self.apps.push(app);
}
pub fn remove_app(&mut self, index: usize) -> Option<AppConfig> {
if index < self.apps.len() {
Some(self.apps.remove(index))
} else {
None
}
}
pub fn find_by_id(&self, id: &str) -> Option<&AppConfig> {
self.apps.iter().find(|app| app.id == id)
}
pub fn find_by_id_mut(&mut self, id: &str) -> Option<&mut AppConfig> {
self.apps.iter_mut().find(|app| app.id == id)
}
pub fn app_count(&self) -> usize {
self.apps.len()
}
}
pub struct RunningProcess {
#[allow(dead_code)]
pub child: Child,
pub console_pid: Option<u32>,
#[allow(dead_code)]
pub started_at: Instant,
}
impl RunningProcess {
pub fn new(child: Child, console_pid: Option<u32>) -> Self {
Self {
child,
console_pid,
started_at: Instant::now(),
}
}
}
#[derive(Clone, Debug)]
pub struct IconInfo {
pub name: String,
#[allow(dead_code)]
pub filename: String,
}
impl IconInfo {
pub fn new(name: String, filename: String) -> Self {
Self { name, filename }
}
}