Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tui-term = "0.3.4"
bytes = "1.8.0"
portable-pty = "0.9.0"
sysinfo = "0.39.5"
clap = { version = "4.6.6", features = ["derive"] }
[target.'cfg(unix)'.dependencies]
nix = { version = "0.29.0", features = ["signal"] }

Expand Down
27 changes: 25 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::event::{AppEvent, Event, EventHandler};
use crate::formation::Formation;
use crate::process::Process;
use crate::procfile;
use anyhow::Result;
Expand Down Expand Up @@ -66,12 +67,34 @@ const COLORS: &[Color] = &[
];

impl App {
pub fn new(procfile_path: String) -> Self {
pub fn new(procfile_path: String, formation: Option<Formation>) -> Self {
let entries = procfile::parse(&procfile_path).unwrap_or_default();

// Expand each Procfile entry according to the formation:
// count=0 → skip the process entirely
// count=1 → single instance, keep the original name
// count>1 → numbered instances: name.1, name.2, …
let processes = entries
.into_iter()
.flat_map(|e| {
let count = formation
.as_ref()
.map(|f| f.count_for(&e.name))
.unwrap_or(1);

(1..=count)
.map(|n| {
let name = if count == 1 {
e.name.clone()
} else {
format!("{}.{}", e.name, n)
};
(name, e.command.clone())
})
.collect::<Vec<_>>()
})
.enumerate()
.map(|(i, e)| Process::new(e.name, e.command, COLORS[i % COLORS.len()]))
.map(|(i, (name, command))| Process::new(name, command, COLORS[i % COLORS.len()]))
.collect();

Self {
Expand Down
48 changes: 48 additions & 0 deletions src/formation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! This program is free software: you can redistribute it and/or modify
//! it under the terms of the GNU General Public License as published by
//! the Free Software Foundation, either version 3 of the License, or
//! (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::collections::HashMap;

/// Maps process names to their desired instance counts.
/// Supports an `all` wildcard that acts as the default for any unlisted process.
pub struct Formation(pub HashMap<String, usize>);

impl From<String> for Formation {
fn from(s: String) -> Self {
let map = s
.split(',')
.filter_map(|part| {
let mut kv = part.splitn(2, '=');
let key = kv.next()?.trim().to_string();
let val: usize = kv.next()?.trim().parse().ok()?;
Some((key, val))
})
.collect();
Self(map)
}
}

impl Formation {
/// Returns the number of instances to run for a given process name.
/// Falls back to the `all` entry if the name is not explicitly listed,
/// and defaults to 1 if neither is present.
pub fn count_for(&self, name: &str) -> usize {
if let Some(&n) = self.0.get(name) {
n
} else if let Some(&n) = self.0.get("all") {
n
} else {
1
}
}
}
31 changes: 27 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,40 @@

pub mod app;
pub mod event;
pub mod formation;
pub mod process;
pub mod procfile;
pub mod ui;
use clap::Parser;
use formation::Formation;
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Specify an alternate location for the application's Procfile.
#[arg(short, long)]
procfile: Option<PathBuf>,

/// Specify the number of each process type to run. The value passed in should be in the format process=num,process=num
#[arg(short = 'm', long)]
formation: Option<String>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let procfile_path = std::env::args()
.nth(1)
.unwrap_or_else(|| "Procfile".to_string());
let args = Args::parse();

let formation = args.formation.map(Formation::from);

let procfile_path = if let Some(procfile) = args.procfile {
String::from(procfile.to_str().unwrap())
} else {
String::from("Procfile")
};

let terminal = ratatui::init();
let result = app::App::new(procfile_path).run(terminal).await;
let result = app::App::new(procfile_path, formation).run(terminal).await;
ratatui::restore();
result
}