use crate::args::Args;
use crate::event::VivacKind;
use crate::failure::{Failure, R};
use crate::output::outln;
struct HookInput {
source: String,
session: Option<String>,
}
impl HookInput {
fn read() -> HookInput {
use std::io::IsTerminal;
let mut raw = String::new();
if !std::io::stdin().is_terminal() {
use std::io::Read;
std::io::stdin().read_to_string(&mut raw).ok();
}
let v: serde_json::Value = serde_json::from_str(&raw).unwrap_or(serde_json::Value::Null);
HookInput {
source: v
.get("source")
.and_then(|s| s.as_str())
.unwrap_or("unknown")
.to_string(),
session: v
.get("session_id")
.and_then(|s| s.as_str())
.map(str::to_string),
}
}
}
pub fn start(ctx: &mut crate::ops::Ctx, a: &Args, project: &str) -> R {
if !a.has("hook") {
return crate::brief::brief(&ctx.tree, &ctx.store.root, &ctx.lane_dir, a, project);
}
let text = crate::brief::to_text(&ctx.tree, &ctx.store.root, &ctx.lane_dir, a, project)?;
print!("{text}");
let hook = HookInput::read();
let shown_focus = ctx.tree.focus().map(|n| n.id.clone());
let shown_vivac = ctx.tree.last_vivac().map(|v| v.id.clone());
match ctx.lock_for_write() {
Ok(mine) => {
crate::ops::session_started(ctx, &hook.source, hook.session, shown_focus, shown_vivac)
.ok();
if mine {
ctx.unlock();
}
}
Err(Failure::Busy(_)) => {
outln!(
" Session not recorded: another vivac process held the tree for {} seconds.",
crate::store::LOCK_DEADLINE.as_secs()
);
}
Err(_) => {}
}
Ok(())
}
pub fn end(ctx: &mut crate::ops::Ctx, a: &Args) -> R {
if nothing_to_stop(&ctx.tree, a) {
return Ok(());
}
let mine = match ctx.lock_for_write() {
Ok(mine) => mine,
Err(_) if a.has("hook") => return Ok(()),
Err(e) => return Err(e),
};
if nothing_to_stop(&ctx.tree, a) {
return Ok(());
}
let next = a.opt_or("next");
let label = segment_label(&ctx.tree);
let num = ctx.tree.next_vivac_num.max(1);
crate::ops::auto_vivac(ctx, VivacKind::Auto, &next, &label)?;
if mine {
ctx.unlock();
}
if !a.has("hook") {
outln!(" v{num} automatic stop at session close");
}
Ok(())
}
fn nothing_to_stop(t: &crate::model::Tree, a: &Args) -> bool {
if t.stack().is_empty() {
if !a.has("hook") {
outln!(" Empty stack: no stop worth saving.");
}
return true;
}
if t.state().seq_change <= t.state().seq_vivac {
if !a.has("hook") {
outln!(" Nothing changed since the last stop.");
}
return true;
}
false
}
fn segment_label(t: &crate::model::Tree) -> String {
let s = t.state();
let mut parts = Vec::new();
if s.seg_new > 0 {
parts.push(format!("{} new", s.seg_new));
}
if s.seg_closed > 0 {
parts.push(format!("{} closed", s.seg_closed));
}
if s.seg_notes == 1 {
parts.push("1 note".to_string());
} else if s.seg_notes > 1 {
parts.push(format!("{} notes", s.seg_notes));
}
if parts.is_empty() && s.seg_events > 0 {
parts.push(if s.seg_events == 1 {
"1 change".to_string()
} else {
format!("{} changes", s.seg_events)
});
}
parts.join(", ")
}
pub fn dispatch(ctx: &mut crate::ops::Ctx, a: &Args, project: &str) -> R {
match a.positional(0) {
Some("start") => start(ctx, a, project),
Some("end") => end(ctx, a),
_ => Err(Failure::usage("usage: vivac session start|end [--hook]")),
}
}