1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
pub mod add;
pub mod ai;
pub mod backup;
pub mod bund;
pub mod delete;
pub mod export;
pub mod import_help;
pub mod import_typst_help;
pub mod init;
pub mod list;
pub mod mv;
pub mod reindex;
pub mod restore;
pub mod search;
use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand, ValueEnum};
use crate::store::NodeKind;
#[derive(Debug, Parser)]
#[command(name = "inkhaven", version, about = "TUI literary work editor for Typst books")]
pub struct Cli {
/// Path to a project root. For `init`, this is the project to create. For
/// every other subcommand, defaults to the current directory.
#[arg(long, global = true)]
pub project: Option<PathBuf>,
#[command(subcommand)]
pub command: Option<Command>,
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Initialize a new inkhaven project at the given path.
Init {
/// Project directory to create.
path: PathBuf,
/// Overwrite existing configuration if present.
#[arg(long)]
force: bool,
},
/// Add a node (book / chapter / subchapter / paragraph) to the hierarchy.
Add {
/// Node kind.
#[arg(value_enum)]
kind: CliNodeKind,
/// Display title.
title: String,
/// Slash-separated slug path to the parent (e.g. `my-book/01-chapter`).
/// Required for everything except `book` when not using --after.
#[arg(long)]
parent: Option<String>,
/// Override the auto-assigned slug (defaults to slugified title).
#[arg(long)]
slug: Option<String>,
/// Insert the new node immediately after an existing sibling of the
/// same kind. Pass the sibling's slug path here; --parent is then
/// implicit (taken from the anchor's parent).
#[arg(long)]
after: Option<String>,
},
/// Print the hierarchy as a tree.
List,
/// Delete a node (and its descendants) by slash-separated slug path.
Delete {
/// e.g. `my-book/the-storm/morning-light`
path: String,
/// Required confirmation flag — without it we just dry-run.
#[arg(long)]
yes: bool,
},
/// Reorder a node within its siblings by swapping with the neighbor.
Mv {
/// Slash-separated slug path to the node.
path: String,
/// `up` or `down`.
#[arg(value_enum)]
direction: mv::Direction,
},
/// Run a semantic search across the project.
Search {
query: String,
#[arg(short, long, default_value_t = 10)]
limit: usize,
},
/// Re-index all `.typ` files from disk into the document store.
Reindex {
/// Remove store records whose file is missing on disk.
#[arg(long)]
prune: bool,
/// Register every orphan .typ file under the deepest hierarchy
/// branch whose filesystem path matches the orphan's parent dir.
#[arg(long)]
adopt: bool,
},
/// Export the book(s) to a target format.
Export {
#[arg(value_enum, default_value_t = ExportFormat::Typst)]
format: ExportFormat,
/// Output path (file for typst, directory for pdf builds).
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Run a one-shot AI inference from the command line.
Ai {
prompt: String,
#[arg(short, long)]
provider: Option<String>,
},
/// Import a directory tree into the Help system book. Subdirectories
/// become chapters / subchapters / (flattened) and files become
/// paragraphs. Filenames and directory names supply the displayed
/// titles. Wipes Help's existing contents first.
ImportHelp {
/// Source directory whose contents will be ingested under the Help
/// system book. Files at the root land as paragraphs directly under
/// Help; subdirectories become chapters (then subchapters, etc.).
#[arg(long)]
documents_directory: PathBuf,
},
/// Import inkhaven's curated Typst reference into the Help system
/// book. Creates / refreshes a `Typst reference` chapter so F1
/// (RAG over Help) can answer typst questions from grounded
/// context. Offline — the reference is bundled with the binary.
ImportTypstHelp,
/// Zip the project into a dated backup archive
/// (`blackinkhaven_YYYYDDMM_HHMMSS.zip`).
Backup {
/// Output directory for the archive. Created if missing.
/// Omit to use the project-relative default
/// (`<parent-of-project>/inkhaven-backups/<project-basename>/`)
/// — same location the TUI's exit hook writes to.
#[arg(long)]
out: Option<PathBuf>,
},
/// Restore a backup archive into a fresh directory.
Restore {
/// Path to the `.zip` backup file.
archive: PathBuf,
/// Destination directory. Must not already contain
/// `inkhaven.hjson` — pick a fresh directory or wipe the old one
/// first.
#[arg(long)]
to: PathBuf,
},
/// Evaluate a Bund expression against the Adam VM and print the
/// top of the workbench. Phase-0 smoke command — does not open
/// the project store. Use this to verify the scripting layer
/// works on your install and to experiment with Bund syntax.
Bund {
/// The Bund script to run, e.g. `"40 2 + ."`.
code: String,
},
/// Launch the TUI editor (default if no subcommand is given).
Tui,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum CliNodeKind {
Book,
Chapter,
Subchapter,
Paragraph,
/// Bund script — a `.bund` file `bund.eval`'d into Adam at
/// project open. Default home is the `Scripts` system book,
/// but Scripts can also live inside any user Book.
Script,
}
impl From<CliNodeKind> for NodeKind {
fn from(k: CliNodeKind) -> Self {
match k {
CliNodeKind::Book => NodeKind::Book,
CliNodeKind::Chapter => NodeKind::Chapter,
CliNodeKind::Subchapter => NodeKind::Subchapter,
CliNodeKind::Paragraph => NodeKind::Paragraph,
CliNodeKind::Script => NodeKind::Script,
}
}
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ExportFormat {
Typst,
Pdf,
}
impl Cli {
pub fn run(self) -> Result<()> {
let project = self
.project
.clone()
.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
match self.command.unwrap_or(Command::Tui) {
Command::Init { path, force } => init::run(&path, force).map_err(Into::into),
Command::Add {
kind,
title,
parent,
slug,
after,
} => add::run(
&project,
kind.into(),
&title,
parent.as_deref(),
slug.as_deref(),
after.as_deref(),
)
.map_err(Into::into),
Command::List => list::run(&project).map_err(Into::into),
Command::Delete { path, yes } => delete::run(&project, &path, yes).map_err(Into::into),
Command::Mv { path, direction } => {
mv::run(&project, &path, direction).map_err(Into::into)
}
Command::Search { query, limit } => {
search::run(&project, &query, limit).map_err(Into::into)
}
Command::Reindex { prune, adopt } => {
reindex::run(&project, prune, adopt).map_err(Into::into)
}
Command::Export { format, output } => {
export::run(&project, format, output.as_deref()).map_err(Into::into)
}
Command::Ai { prompt, provider } => {
ai::run(&project, &prompt, provider.as_deref()).map_err(Into::into)
}
Command::ImportHelp {
documents_directory,
} => import_help::run(&project, &documents_directory).map_err(Into::into),
Command::ImportTypstHelp => {
import_typst_help::run(&project).map_err(Into::into)
}
Command::Backup { out } => backup::run(&project, out.as_deref()).map_err(Into::into),
Command::Restore { archive, to } => {
restore::run(&archive, &to).map_err(Into::into)
}
Command::Bund { code } => bund::run(&code, &project),
Command::Tui => crate::tui::run(Some(&project)).map_err(Into::into),
}
}
}