use std::path::PathBuf;
use serde::Deserialize;
pub type Rgba = [f32; 4];
#[derive(Deserialize, Clone, Debug)]
#[serde(default)]
pub struct Config {
pub font_family: String,
pub font_size: f32,
pub line_height: f32,
pub shell: String,
pub shell_args: Vec<String>,
pub padding: f32,
pub gap: f32,
pub radius: f32,
pub scrollback: usize,
pub cursor_blink: bool,
pub animation_speed: f32,
pub copy_on_select: bool,
pub shell_integration: bool,
pub glass: bool,
pub glass_opacity: f32,
pub theme: Theme,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(default)]
pub struct Theme {
pub background: String,
pub surface: String,
pub border: String,
pub foreground: String,
pub cursor: String,
pub accent: String,
pub selection: String,
pub ansi: [String; 16],
}
impl Default for Config {
fn default() -> Self {
Self {
font_family: "Cascadia Mono".into(),
font_size: 11.0,
line_height: 1.3,
shell: String::new(),
shell_args: vec!["--login".into(), "-i".into()],
padding: 12.0,
gap: 6.0,
radius: 10.0,
scrollback: 20_000,
cursor_blink: true,
animation_speed: 1.0,
copy_on_select: false,
shell_integration: true,
glass: true,
glass_opacity: 0.66,
theme: Theme::default(),
}
}
}
impl Default for Theme {
fn default() -> Self {
let s = |v: &str| v.to_string();
Self {
background: s("#08090b"),
surface: s("#101216"),
border: s("#1b1f26"),
foreground: s("#d5d9e0"),
cursor: s("#e6e9ee"),
accent: s("#52b6cc"),
selection: s("#2b4a5a"),
ansi: [
s("#1d2128"), s("#ef7a7f"), s("#a3cf8c"), s("#e6c27f"), s("#78a3f2"), s("#c49ae6"), s("#5ccbe0"), s("#c4c9d1"), s("#5d6573"), s("#ff9196"), s("#b9e3a2"), s("#f2d49a"), s("#9bbcff"), s("#d8b2f5"), s("#8be0f0"), s("#eef0f4"), ],
}
}
}
#[derive(Clone, Debug)]
pub struct Palette {
pub background: Rgba,
pub surface: Rgba,
pub border: Rgba,
pub foreground: Rgba,
pub cursor: Rgba,
pub accent: Rgba,
pub selection: Rgba,
pub ansi: [Rgba; 16],
}
impl Palette {
pub fn from_theme(t: &Theme) -> Self {
let d = Theme::default();
let p = |v: &str, fallback: &str| parse_hex(v).or_else(|| parse_hex(fallback)).unwrap();
let mut ansi = [[0.0; 4]; 16];
for (i, c) in ansi.iter_mut().enumerate() {
*c = p(&t.ansi[i], &d.ansi[i]);
}
Self {
background: p(&t.background, &d.background),
surface: p(&t.surface, &d.surface),
border: p(&t.border, &d.border),
foreground: p(&t.foreground, &d.foreground),
cursor: p(&t.cursor, &d.cursor),
accent: p(&t.accent, &d.accent),
selection: p(&t.selection, &d.selection),
ansi,
}
}
pub fn indexed(&self, idx: u8) -> Rgba {
match idx {
0..=15 => self.ansi[idx as usize],
16..=231 => {
let i = idx - 16;
let step = |v: u8| if v == 0 { 0.0 } else { (55.0 + v as f32 * 40.0) / 255.0 };
[step(i / 36), step((i / 6) % 6), step(i % 6), 1.0]
}
_ => {
let v = (8.0 + (idx - 232) as f32 * 10.0) / 255.0;
[v, v, v, 1.0]
}
}
}
}
pub fn parse_hex(s: &str) -> Option<Rgba> {
let s = s.trim().trim_start_matches('#');
let byte = |i: usize| u8::from_str_radix(s.get(i..i + 2)?, 16).ok().map(|b| b as f32 / 255.0);
match s.len() {
6 => Some([byte(0)?, byte(2)?, byte(4)?, 1.0]),
8 => Some([byte(0)?, byte(2)?, byte(4)?, byte(6)?]),
_ => None,
}
}
pub fn config_path() -> Option<PathBuf> {
dirs::config_dir().map(|d| d.join("rustimal").join("config.toml"))
}
impl Config {
pub fn load() -> Self {
let Some(path) = config_path() else { return Self::default() };
match std::fs::read_to_string(&path) {
Ok(text) => toml::from_str(&text).unwrap_or_else(|e| {
log::error!("invalid config {}: {e}", path.display());
Self::default()
}),
Err(_) => {
if let Some(dir) = path.parent() {
let _ = std::fs::create_dir_all(dir);
let _ = std::fs::write(&path, DEFAULT_CONFIG);
}
Self::default()
}
}
}
pub fn shell_program(&self) -> (String, Vec<String>) {
if !self.shell.is_empty() {
return (self.shell.clone(), self.shell_args.clone());
}
let mut candidates: Vec<PathBuf> =
vec![r"C:\Program Files\Git\bin\bash.exe".into(), r"C:\Program Files (x86)\Git\bin\bash.exe".into()];
if let Some(local) = dirs::data_local_dir() {
candidates.push(local.join(r"Programs\Git\bin\bash.exe"));
}
if let Some(home) = dirs::home_dir() {
candidates.push(home.join(r"scoop\apps\git\current\bin\bash.exe"));
}
for c in candidates {
if c.exists() {
return (c.to_string_lossy().into_owned(), self.shell_args.clone());
}
}
("powershell.exe".into(), vec!["-NoLogo".into()])
}
}
const DEFAULT_CONFIG: &str = r##"# rustimal configuration. Delete a line to use the default.
# font_family = "Cascadia Mono"
# font_size = 11.0 # points
# line_height = 1.3
# shell = "" # empty = Git Bash
# shell_args = ["--login", "-i"]
# padding = 12.0
# gap = 6.0
# radius = 10.0
# scrollback = 20000
# cursor_blink = true
# animation_speed = 1.0 # 0 disables animations
# copy_on_select = false
# shell_integration = true # minimal prompt + Claude session picker
# glass = true # see-through, blurred window and glass panes
# glass_opacity = 0.66 # 0..1, how solid the panes are
# [theme]
# background = "#08090b"
# surface = "#101216"
# border = "#1b1f26"
# foreground = "#d5d9e0"
# cursor = "#e6e9ee"
# accent = "#52b6cc"
# selection = "#2b4a5a"
# ansi = ["#1d2128", "#ef7a7f", "#a3cf8c", "#e6c27f", "#78a3f2", "#c49ae6", "#5ccbe0", "#c4c9d1",
# "#5d6573", "#ff9196", "#b9e3a2", "#f2d49a", "#9bbcff", "#d8b2f5", "#8be0f0", "#eef0f4"]
"##;
const SHELL_INTEGRATION: &str = include_str!("shell/rustimal.bash");
impl Config {
pub fn shell_env(&self) -> Vec<(String, String)> {
let (program, _) = self.shell_program();
if !self.shell_integration || !program.to_ascii_lowercase().contains("bash") {
return Vec::new();
}
let Some(dir) = config_path().and_then(|p| p.parent().map(|d| d.to_path_buf())) else { return Vec::new() };
let path = dir.join("rustimal.bash");
let _ = std::fs::create_dir_all(&dir);
if let Err(e) = std::fs::write(&path, SHELL_INTEGRATION.replace("\r\n", "\n")) {
log::error!("could not write {}: {e}", path.display());
return Vec::new();
}
let fwd = |p: &std::path::Path| p.to_string_lossy().replace('\\', "/");
let mut env = vec![("PROMPT_COMMAND".into(), format!("[ -z \"$__RUSTIMAL\" ] && . '{}'", fwd(&path)))];
let state = state_dir();
let script = dir.join("statusline.sh");
let settings = dir.join("claude-settings.json");
let claude_settings = serde_json::json!({
"statusLine": { "type": "command", "command": format!("bash \"{}\"", fwd(&script)), "padding": 0 },
"spinnerTipsEnabled": false,
});
let ok = state.as_ref().is_some_and(|s| std::fs::create_dir_all(s).is_ok())
&& std::fs::write(&script, STATUSLINE.replace("\r\n", "\n")).is_ok()
&& std::fs::write(&settings, claude_settings.to_string()).is_ok();
if let (true, Some(state)) = (ok, state) {
env.push(("RUSTIMAL_STATE".into(), fwd(&state)));
env.push(("RUSTIMAL_CLAUDE_SETTINGS".into(), fwd(&settings)));
}
env
}
}
impl Config {
pub fn take_install_hint(&self) -> bool {
if self.shell_env().is_empty() {
return false; }
let Some(state) = state_dir() else { return false };
let marker = state.join("install-hint-shown");
let from_source_tree = std::env::current_exe().is_ok_and(|e| e.components().any(|c| c.as_os_str() == "target"));
let installed = dirs::config_dir()
.is_some_and(|d| d.join(r"Microsoft\Windows\Start Menu\Programs\rustimal.lnk").exists());
if from_source_tree || installed || marker.exists() {
return false;
}
let _ = std::fs::create_dir_all(&state);
std::fs::write(&marker, "").is_ok()
}
}
const STATUSLINE: &str = include_str!("shell/statusline.sh");
pub fn state_dir() -> Option<PathBuf> {
config_path().and_then(|p| p.parent().map(|d| d.join("state")))
}