mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-17 04:45:33 -08:00
Add the plan2code-app desktop app, its CI workflows and docs
AI Assisted
This commit is contained in:
1 parent
681c4a4b85
commit
a5a3a81c5f
55 files changed
+13277
No files matched your search
@@ -0,0 +1,242 @@
|
||||
//! The app's own settings file, `~/.plan2code/app.json`. It holds what only the
|
||||
//! app remembers (today: the agent last picked in the bar). The console's
|
||||
//! folders under `~/.plan2code/console/` are never written to from here.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::agents::AgentKind;
|
||||
|
||||
const LAST_AGENT_KEY: &str = "lastAgent";
|
||||
const NUDGE_KEY: &str = "CONTEXT_NUDGE_PERCENT";
|
||||
const TERM_DOCK_KEY: &str = "termDock";
|
||||
const TERM_FRACTION_KEY: &str = "termFraction";
|
||||
const TERM_COLLAPSED_KEY: &str = "termCollapsed";
|
||||
const FIRST_RUN_DONE_KEY: &str = "firstRunDone";
|
||||
|
||||
/// The context percent at which the bar suggests a Restart, when the file
|
||||
/// names no other.
|
||||
pub const DEFAULT_NUDGE_PERCENT: u8 = 70;
|
||||
|
||||
/// The terminal pane's remembered shape: which edge it docks to, how much of
|
||||
/// the window it takes (a fraction, not collapsed or maximized), and whether
|
||||
/// it was left collapsed.
|
||||
#[derive(Clone, Debug, PartialEq, serde::Serialize)]
|
||||
pub struct TermPanelSettings {
|
||||
pub dock: String,
|
||||
pub fraction: f64,
|
||||
pub collapsed: bool,
|
||||
}
|
||||
|
||||
pub const DEFAULT_TERM_DOCK: &str = "bottom";
|
||||
pub const DEFAULT_TERM_FRACTION: f64 = 0.25;
|
||||
const TERM_FRACTION_MIN: f64 = 0.05;
|
||||
const TERM_FRACTION_MAX: f64 = 0.9;
|
||||
|
||||
pub fn settings_file() -> Option<PathBuf> {
|
||||
Some(std::env::home_dir()?.join(".plan2code").join("app.json"))
|
||||
}
|
||||
|
||||
fn read(file: &Path) -> serde_json::Map<String, serde_json::Value> {
|
||||
std::fs::read_to_string(file)
|
||||
.ok()
|
||||
.and_then(|text| serde_json::from_str(&text).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The agent saved in `file`, when it still names one the app can run.
|
||||
pub fn load_last_agent(file: &Path) -> Option<AgentKind> {
|
||||
AgentKind::from_id(read(file).get(LAST_AGENT_KEY)?.as_str()?)
|
||||
}
|
||||
|
||||
/// The context percent that triggers the Restart suggestion. A value outside
|
||||
/// 1 to 100, or one that is not a number, means the default.
|
||||
pub fn load_nudge_percent(file: &Path) -> u8 {
|
||||
read(file)
|
||||
.get(NUDGE_KEY)
|
||||
.and_then(|value| value.as_u64())
|
||||
.filter(|percent| (1..=100).contains(percent))
|
||||
.map_or(DEFAULT_NUDGE_PERCENT, |percent| percent as u8)
|
||||
}
|
||||
|
||||
/// Save the pick, keeping any other key the file holds.
|
||||
pub fn save_last_agent(file: &Path, kind: AgentKind) -> Result<(), String> {
|
||||
let mut settings = read(file);
|
||||
settings.insert(LAST_AGENT_KEY.to_string(), serde_json::Value::String(kind.id().to_string()));
|
||||
write(file, settings)
|
||||
}
|
||||
|
||||
/// The terminal pane's shape, from the file or the defaults. A dock other
|
||||
/// than `bottom` or `right`, or a fraction outside its range, is the default.
|
||||
pub fn load_term_panel(file: &Path) -> TermPanelSettings {
|
||||
let settings = read(file);
|
||||
let dock = settings
|
||||
.get(TERM_DOCK_KEY)
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|d| *d == "bottom" || *d == "right")
|
||||
.unwrap_or(DEFAULT_TERM_DOCK)
|
||||
.to_string();
|
||||
let fraction = settings
|
||||
.get(TERM_FRACTION_KEY)
|
||||
.and_then(|v| v.as_f64())
|
||||
.filter(|f| (TERM_FRACTION_MIN..=TERM_FRACTION_MAX).contains(f))
|
||||
.unwrap_or(DEFAULT_TERM_FRACTION);
|
||||
let collapsed = settings.get(TERM_COLLAPSED_KEY).and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
TermPanelSettings { dock, fraction, collapsed }
|
||||
}
|
||||
|
||||
/// Save the terminal pane's shape, keeping any other key the file holds. An
|
||||
/// invalid dock or an out-of-range fraction is rejected rather than saved.
|
||||
pub fn save_term_panel(file: &Path, dock: &str, fraction: f64, collapsed: bool) -> Result<(), String> {
|
||||
if dock != "bottom" && dock != "right" {
|
||||
return Err(format!("unknown dock: {dock}"));
|
||||
}
|
||||
if !(TERM_FRACTION_MIN..=TERM_FRACTION_MAX).contains(&fraction) {
|
||||
return Err(format!("fraction out of range: {fraction}"));
|
||||
}
|
||||
let mut settings = read(file);
|
||||
settings.insert(TERM_DOCK_KEY.to_string(), serde_json::Value::String(dock.to_string()));
|
||||
settings.insert(
|
||||
TERM_FRACTION_KEY.to_string(),
|
||||
serde_json::Number::from_f64(fraction).map(serde_json::Value::Number).unwrap_or(serde_json::Value::Null),
|
||||
);
|
||||
settings.insert(TERM_COLLAPSED_KEY.to_string(), serde_json::Value::Bool(collapsed));
|
||||
write(file, settings)
|
||||
}
|
||||
|
||||
/// Whether the first-run checklist has already passed once. Once it has,
|
||||
/// later launches go straight to the launch card (Task 9.4).
|
||||
pub fn load_first_run_done(file: &Path) -> bool {
|
||||
read(file).get(FIRST_RUN_DONE_KEY).and_then(|v| v.as_bool()).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Remember that the checklist passed, keeping any other key the file holds.
|
||||
pub fn save_first_run_done(file: &Path) -> Result<(), String> {
|
||||
let mut settings = read(file);
|
||||
settings.insert(FIRST_RUN_DONE_KEY.to_string(), serde_json::Value::Bool(true));
|
||||
write(file, settings)
|
||||
}
|
||||
|
||||
fn write(file: &Path, settings: serde_json::Map<String, serde_json::Value>) -> Result<(), String> {
|
||||
if let Some(parent) = file.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
|
||||
}
|
||||
let json = serde_json::to_string_pretty(&serde_json::Value::Object(settings)).map_err(|e| e.to_string())?;
|
||||
std::fs::write(file, json).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn temp_file(name: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("plan2code-settings-{}-{name}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
dir.join("app.json")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_file_has_no_choice() {
|
||||
assert_eq!(load_last_agent(&temp_file("missing")), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_choice_round_trips_and_other_keys_survive() {
|
||||
let file = temp_file("round-trip");
|
||||
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file, r#"{"other": 1}"#).unwrap();
|
||||
save_last_agent(&file, AgentKind::Devin).unwrap();
|
||||
assert_eq!(load_last_agent(&file), Some(AgentKind::Devin));
|
||||
assert!(std::fs::read_to_string(&file).unwrap().contains("\"other\""));
|
||||
let _ = std::fs::remove_dir_all(file.parent().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_nudge_defaults_to_seventy() {
|
||||
assert_eq!(load_nudge_percent(&temp_file("nudge-missing")), 70);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_nudge_reads_a_named_percent() {
|
||||
let file = temp_file("nudge-set");
|
||||
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file, r#"{"CONTEXT_NUDGE_PERCENT": 85}"#).unwrap();
|
||||
assert_eq!(load_nudge_percent(&file), 85);
|
||||
let _ = std::fs::remove_dir_all(file.parent().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_nudge_outside_one_to_one_hundred_or_not_a_number_is_the_default() {
|
||||
for text in [
|
||||
r#"{"CONTEXT_NUDGE_PERCENT": 0}"#,
|
||||
r#"{"CONTEXT_NUDGE_PERCENT": 150}"#,
|
||||
r#"{"CONTEXT_NUDGE_PERCENT": "high"}"#,
|
||||
] {
|
||||
let file = temp_file("nudge-bad");
|
||||
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file, text).unwrap();
|
||||
assert_eq!(load_nudge_percent(&file), 70, "{text}");
|
||||
let _ = std::fs::remove_dir_all(file.parent().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_agent_is_ignored() {
|
||||
let file = temp_file("unknown");
|
||||
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file, r#"{"lastAgent": "nope"}"#).unwrap();
|
||||
assert_eq!(load_last_agent(&file), None);
|
||||
let _ = std::fs::remove_dir_all(file.parent().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_term_panel_defaults_to_bottom_at_a_quarter() {
|
||||
let want = TermPanelSettings { dock: "bottom".into(), fraction: 0.25, collapsed: false };
|
||||
assert_eq!(load_term_panel(&temp_file("term-missing")), want);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_term_panel_round_trips_and_other_keys_survive() {
|
||||
let file = temp_file("term-round-trip");
|
||||
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file, r#"{"other": 1}"#).unwrap();
|
||||
save_term_panel(&file, "right", 0.4, true).unwrap();
|
||||
assert_eq!(
|
||||
load_term_panel(&file),
|
||||
TermPanelSettings { dock: "right".into(), fraction: 0.4, collapsed: true }
|
||||
);
|
||||
assert!(std::fs::read_to_string(&file).unwrap().contains("\"other\""));
|
||||
let _ = std::fs::remove_dir_all(file.parent().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_dock_or_an_out_of_range_fraction_is_refused() {
|
||||
let file = temp_file("term-bad-save");
|
||||
assert!(save_term_panel(&file, "top", 0.3, false).is_err());
|
||||
assert!(save_term_panel(&file, "bottom", 0.0, false).is_err());
|
||||
assert!(save_term_panel(&file, "bottom", 1.0, false).is_err());
|
||||
assert!(!file.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_run_is_not_done_until_it_is_saved() {
|
||||
let file = temp_file("first-run");
|
||||
assert!(!load_first_run_done(&file));
|
||||
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file, r#"{"other": 1}"#).unwrap();
|
||||
save_first_run_done(&file).unwrap();
|
||||
assert!(load_first_run_done(&file));
|
||||
assert!(std::fs::read_to_string(&file).unwrap().contains("\"other\""));
|
||||
let _ = std::fs::remove_dir_all(file.parent().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bad_dock_or_out_of_range_fraction_on_disk_reads_as_the_default() {
|
||||
let file = temp_file("term-bad-load");
|
||||
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
std::fs::write(&file, r#"{"termDock": "top", "termFraction": 5}"#).unwrap();
|
||||
let loaded = load_term_panel(&file);
|
||||
assert_eq!(loaded.dock, "bottom");
|
||||
assert_eq!(loaded.fraction, 0.25);
|
||||
let _ = std::fs::remove_dir_all(file.parent().unwrap());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user