use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::LazyLock;
use regex::Regex;
use tokio::process::Command;
use crate::argv::{
child_folder_names, empty_tasks_message, is_empty_tasks_error, normalize_folder,
search_command_args, tasks_command_args, tasks_scope,
};
use crate::diagnostics::{append_warning, is_notebook_not_found, is_selector_not_found};
use crate::error::NbError;
use crate::fingerprint;
use crate::fingerprint::Fingerprint;
use crate::gate;
use crate::git::derive_git_notebook_name;
use crate::git_env::scrub_git_env;
use crate::git_signing::apply_git_signing_env;
use crate::lines::{
note_line_from_body_line, require_contiguous_body, search_lines, split_body_lines,
};
use crate::output::strip_empty_result_hint;
use crate::parser::{ParseContext, parse};
use crate::transaction::{self, Transaction};
use crate::types::{
BodyFragment, ByteString, CommitOutcome, LineEdit, NoteTarget, Occurrence, SearchNoteLines,
ShowNote, ShowNoteLines,
};
use crate::validate::{
detect_duplicate_title_heading, parse_qualified_selector, validate_destination,
validate_folder_option, validate_folder_path, validate_notebook_name,
};
use crate::{Config, NbClient, SearchMode, TaskStatus};
static ANSI_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|[ -/]*[0-~])").unwrap());
fn strip_ansi(text: &str) -> String {
ANSI_REGEX.replace_all(text, "").into_owned()
}
enum ShowClassification {
Textual,
NonTextual { actual_type: String },
ProbeFailure,
}
const FOLDER_REQUIRED_MESSAGE: &str = "This server is configured to require `folder` for new notes. Use the `nb.mkdir` tool to create new folders and the `nb.folders` tool to list existing folders.";
impl NbClient {
pub fn new(config: &Config) -> anyhow::Result<Self> {
let default_notebook = config
.notebook
.as_deref()
.map(String::from)
.or_else(derive_git_notebook_name);
Ok(Self {
default_notebook,
create_notebook: config.create_notebook,
disable_git_signing: config.disable_git_signing,
allow_top_level_notes: config.allow_top_level_notes,
gate_timeout: config.gate_timeout,
})
}
pub async fn transaction(&self, notebook: Option<&str>) -> Result<Transaction, NbError> {
let notebook = self.resolve_notebook_name(notebook)?;
if notebook_dir_from_env(¬ebook).is_none() {
self.ensure_notebook(¬ebook).await?;
}
Ok(Transaction::new(self.clone(), notebook, self.gate_timeout))
}
pub fn gate_timeout(&self) -> std::time::Duration {
self.gate_timeout
}
pub(crate) async fn show_notebook_path_unguarded(
&self,
notebook: Option<&str>,
) -> Result<PathBuf, NbError> {
let notebook = self.resolve_notebook_name(notebook)?;
if let Some(path) = notebook_dir_from_env(¬ebook) {
return Ok(path);
}
self.ensure_notebook(¬ebook).await?;
let output = self
.exec_vec(vec![
"notebooks".to_string(),
"show".to_string(),
notebook.clone(),
"--path".to_string(),
])
.await?;
let path = output.trim();
if path.is_empty() {
return Err(NbError::CommandFailed {
command: format!("nb notebooks show {notebook} --path"),
stderr: "nb notebooks path output was empty".to_string(),
exit_code: None,
});
}
Ok(PathBuf::from(path))
}
async fn with_notebook_gate<F, T>(&self, notebook: &str, f: F) -> Result<T, NbError>
where
F: std::future::Future<Output = Result<T, NbError>>,
{
let root = self.show_notebook_path_unguarded(Some(notebook)).await?;
let key = gate::git_common_dir_realpath(&root)?;
let _hold = gate::acquire_notebook(key, self.gate_timeout, false).await?;
f.await
}
fn require_folder_for_new_note(&self, folder: Option<&str>) -> Result<(), NbError> {
if self.allow_top_level_notes || folder.is_some_and(|value| !value.trim().is_empty()) {
return Ok(());
}
Err(NbError::ValidationError {
reason: FOLDER_REQUIRED_MESSAGE.to_string(),
location: None,
})
}
async fn resolve_target_selector(
&self,
id: &str,
notebook: Option<&str>,
) -> Result<(String, String), NbError> {
if let Some((embedded_notebook, path)) = parse_qualified_selector(id)? {
let notebook = match notebook {
Some(value) => {
validate_notebook_name(value)?;
if value != embedded_notebook {
return Err(NbError::ValidationError {
reason: format!(
"ambiguous selector: id targets notebook `{embedded_notebook}`, but notebook field is `{value}`"
),
location: None,
});
}
embedded_notebook.to_string()
}
_ => embedded_notebook.to_string(),
};
self.ensure_existing_notebook(¬ebook).await?;
return Ok((notebook, format!("{}:{}", embedded_notebook, path)));
}
let notebook = self.resolve_notebook(notebook).await?;
Ok((notebook.clone(), format!("{}:{}", notebook, id)))
}
fn append_notebook_warning(&self, output: String, notebook: &str) -> String {
let Some(default_notebook) = self.default_notebook.as_deref() else {
return output;
};
if default_notebook == notebook {
return output;
}
append_warning(
output,
format!(
"Warning: wrote to notebook `{notebook}`, not the project default notebook `{default_notebook}`. If this was unintended, move or delete the note and retry with the correct notebook/folder."
),
)
}
fn resolve_notebook_name(&self, notebook: Option<&str>) -> Result<String, NbError> {
if let Some(name) = notebook {
validate_notebook_name(name)?;
return Ok(name.to_string());
}
if let Some(name) = self.default_notebook.as_deref() {
validate_notebook_name(name)?;
return Ok(name.to_string());
}
Err(NbError::ValidationError {
reason: "notebook not configured; set --notebook or NB_MCP_NOTEBOOK".to_string(),
location: None,
})
}
async fn resolve_notebook(&self, notebook: Option<&str>) -> Result<String, NbError> {
let name = self.resolve_notebook_name(notebook)?;
self.ensure_notebook(&name).await?;
Ok(name)
}
async fn ensure_notebook(&self, notebook: &str) -> Result<(), NbError> {
match self.check_notebook(notebook).await {
Ok(()) => Ok(()),
Err(err @ (NbError::ExecutableNotFound { .. } | NbError::Io { .. })) => Err(err),
Err(NbError::NotFound { .. }) => {
if !self.create_notebook {
return Err(NbError::ValidationError {
reason: format!(
"notebook not found; create it with the nb CLI (`nb notebooks add {}`) \
or remove --no-create-notebook",
notebook
),
location: None,
});
}
self.exec_vec(vec![
"notebooks".to_string(),
"add".to_string(),
notebook.to_string(),
])
.await?;
Ok(())
}
Err(err) => Err(err),
}
}
async fn ensure_existing_notebook(&self, notebook: &str) -> Result<(), NbError> {
match self.check_notebook(notebook).await {
Ok(()) => Ok(()),
Err(err @ (NbError::ExecutableNotFound { .. } | NbError::Io { .. })) => Err(err),
Err(err @ NbError::NotFound { .. }) => Err(err),
Err(err) => Err(err),
}
}
async fn check_notebook(&self, notebook: &str) -> Result<(), NbError> {
let show_result = self
.exec_vec(vec![
"notebooks".to_string(),
"show".to_string(),
notebook.to_string(),
"--path".to_string(),
])
.await;
match show_result {
Ok(output) => {
if output.trim().is_empty() {
return Err(NbError::CommandFailed {
command: format!("nb notebooks show {notebook} --path"),
stderr: "nb notebooks path output was empty".to_string(),
exit_code: None,
});
}
Ok(())
}
Err(err) => {
match err {
NbError::ExecutableNotFound { .. } | NbError::Io { .. } => Err(err),
NbError::CommandFailed { ref stderr, .. }
if is_notebook_not_found(stderr, notebook) =>
{
Err(NbError::NotFound {
selector: format!("{notebook}:"),
})
}
other => Err(other),
}
}
}
}
async fn exec(&self, args: &[&str]) -> Result<String, NbError> {
tracing::debug!(?args, "executing nb command");
let mut command = Command::new("nb");
scrub_git_env(&mut command);
command
.args(args)
.stdin(Stdio::null()) .stdout(Stdio::piped())
.stderr(Stdio::piped());
if self.disable_git_signing {
apply_git_signing_env(&mut command);
}
let joined = format!("nb {}", args.join(" "));
let output = command
.spawn()
.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
NbError::ExecutableNotFound {
path: "nb".to_string(),
}
} else {
NbError::Io {
path: PathBuf::from("nb"),
source: e.into(),
}
}
})?
.wait_with_output()
.await
.map_err(|e| NbError::Io {
path: PathBuf::from("nb"),
source: e.into(),
})?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
Ok(strip_ansi(&stdout))
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr_text = if stderr.is_empty() {
strip_ansi(&stdout)
} else {
strip_ansi(&stderr)
};
Err(NbError::CommandFailed {
command: joined,
stderr: stderr_text,
exit_code: output.status.code(),
})
}
}
async fn exec_vec(&self, args: Vec<String>) -> Result<String, NbError> {
let args_ref: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
self.exec(&args_ref).await
}
pub async fn show_notebook_status(&self, notebook: Option<&str>) -> Result<String, NbError> {
let notebook = self.resolve_notebook(notebook).await?;
self.exec_vec(vec![format!("{}:", notebook), "status".to_string()])
.await
}
pub async fn list_notebooks(&self) -> Result<String, NbError> {
let _g = gate::acquire_global(self.gate_timeout).await?;
self.exec(&["notebooks", "--no-color"]).await
}
pub async fn show_notebook_path(&self, notebook: Option<&str>) -> Result<PathBuf, NbError> {
let notebook = self.resolve_notebook_name(notebook)?;
self.ensure_notebook(¬ebook).await?;
self.with_notebook_gate(
¬ebook,
self.show_notebook_path_unguarded(Some(¬ebook)),
)
.await
}
pub async fn add_note(
&self,
title: Option<&str>,
content: &str,
tags: &[String],
folder: Option<&str>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
if let Some(t) = title
&& let Some(heading) = detect_duplicate_title_heading(t, content)
{
return Err(NbError::DuplicateTitleHeading {
title: t.to_string(),
heading,
});
}
self.require_folder_for_new_note(folder)?;
validate_folder_option(folder)?;
let filename = transaction::auto_filename("md");
let path = transaction::join_folder_file(folder, &filename);
let mut tx = self.transaction(notebook).await?;
tx.add_note(&path, title, content, tags)?;
tx.commit().await
}
pub async fn show_note(&self, id: &str, notebook: Option<&str>) -> Result<ShowNote, NbError> {
let (notebook, selector) = self.resolve_target_selector(id, notebook).await?;
self.with_notebook_gate(¬ebook, async {
self.show_note_inner(¬ebook, &selector).await
})
.await
}
async fn show_note_inner(&self, notebook: &str, selector: &str) -> Result<ShowNote, NbError> {
match self.probe_show_classification(selector).await {
ShowClassification::NonTextual { actual_type } => {
return Err(NbError::UnsupportedShowTarget {
selector: selector.to_string(),
actual_type,
});
}
ShowClassification::Textual | ShowClassification::ProbeFailure => {}
}
let path = self.resolve_item_path(selector).await?;
let root = self.show_notebook_path_unguarded(Some(notebook)).await?;
let rel = path_relative_to(&root, &path)?;
let source = std::fs::read(&path).map_err(|e| NbError::Io {
path: path.clone(),
source: e.into(),
})?;
let doc = match parse(&source, ParseContext::FromPath(PathBuf::from(&rel))) {
Ok(doc) => doc,
Err(NbError::UnsupportedDocumentFormat { .. }) => {
parse(&source, ParseContext::Explicit(crate::DocumentKind::Note))?
}
Err(err) => return Err(err),
};
let fragments: Vec<BodyFragment> = doc
.body()
.enumerate()
.map(|(i, bytes)| BodyFragment {
index: i as u32,
bytes: ByteString::from_bytes(bytes),
})
.collect();
let body_contiguous = fragments.len() <= 1;
let body_bytes = doc.body_bytes();
let tags: Vec<String> = doc
.tags()
.map(|t| {
String::from_utf8_lossy(t)
.trim_start_matches('#')
.to_string()
})
.collect();
let title = doc.title().map(ByteString::from_bytes);
let title_text = doc.title().map(|bytes| {
let s = String::from_utf8_lossy(bytes);
s.trim_end_matches('\n')
.trim_start_matches('#')
.trim()
.to_string()
});
Ok(ShowNote {
selector: selector.to_string(),
path: rel,
kind: doc.kind(),
todo_state: doc.todo_state(),
title,
title_text,
tags,
body_fragments: fragments,
body_contiguous,
body: ByteString::from_bytes(body_bytes),
fingerprint: fingerprint::fingerprint(&doc),
source: ByteString::from_bytes(source),
})
}
pub async fn show_note_lines(
&self,
target: NoteTarget,
offset: Option<u32>,
limit: Option<u32>,
notebook: Option<&str>,
) -> Result<ShowNoteLines, NbError> {
let (notebook, selector) = self.resolve_note_target(&target, notebook).await?;
self.with_notebook_gate(¬ebook, async {
let shown = self.show_note_inner(¬ebook, &selector).await?;
let source = shown.source.as_bytes()?;
let doc = parse(&source, ParseContext::FromPath(PathBuf::from(&shown.path)))?;
let body = require_contiguous_body(&doc)?;
let lines = split_body_lines(&body);
let total_lines = lines.len() as u32;
let offset = offset.unwrap_or(1);
let limit = limit.unwrap_or(100);
if offset == 0 {
return Err(NbError::InvalidLineWindow {
offset,
limit,
total_lines,
});
}
if total_lines == 0 {
if offset != 1 {
return Err(NbError::InvalidLineWindow {
offset,
limit,
total_lines,
});
}
return Ok(ShowNoteLines {
selector: shown.selector,
path: shown.path,
kind: shown.kind,
total_lines: 0,
offset,
limit,
next_offset: None,
lines: Vec::new(),
title: shown.title,
tags: shown.tags,
body_fingerprint: shown.fingerprint,
});
}
if offset > total_lines + 1 {
return Err(NbError::InvalidLineWindow {
offset,
limit,
total_lines,
});
}
let start_idx = (offset - 1) as usize;
let end_idx = (start_idx + limit as usize).min(lines.len());
let window: Vec<_> = lines[start_idx..end_idx]
.iter()
.map(|l| note_line_from_body_line(l, &body))
.collect();
let next_offset = if end_idx < lines.len() {
Some(offset + window.len() as u32)
} else {
None
};
Ok(ShowNoteLines {
selector: shown.selector,
path: shown.path,
kind: shown.kind,
total_lines,
offset,
limit,
next_offset,
lines: window,
title: shown.title,
tags: shown.tags,
body_fingerprint: shown.fingerprint,
})
})
.await
}
pub async fn search_note_lines(
&self,
target: NoteTarget,
pattern: &[u8],
notebook: Option<&str>,
) -> Result<SearchNoteLines, NbError> {
let (notebook, selector) = self.resolve_note_target(&target, notebook).await?;
self.with_notebook_gate(¬ebook, async {
let shown = self.show_note_inner(¬ebook, &selector).await?;
let source = shown.source.as_bytes()?;
let doc = parse(&source, ParseContext::FromPath(PathBuf::from(&shown.path)))?;
let body = require_contiguous_body(&doc)?;
let hits = search_lines(&body, pattern)?;
Ok(SearchNoteLines {
selector: shown.selector,
path: shown.path,
kind: shown.kind,
hits,
body_fingerprint: shown.fingerprint,
})
})
.await
}
async fn resolve_item_path(&self, selector: &str) -> Result<PathBuf, NbError> {
let output = self
.exec_vec(vec![
"show".to_string(),
selector.to_string(),
"--path".to_string(),
"--no-color".to_string(),
])
.await
.map_err(|err| match err {
NbError::CommandFailed { stderr, .. }
if is_selector_not_found(&stderr, selector) =>
{
NbError::NotFound {
selector: selector.to_string(),
}
}
other => other,
})?;
let path = output.trim();
if path.is_empty() {
return Err(NbError::CommandFailed {
command: format!("nb show {selector} --path"),
stderr: "empty path".into(),
exit_code: None,
});
}
Ok(PathBuf::from(path))
}
async fn resolve_note_target(
&self,
target: &NoteTarget,
notebook: Option<&str>,
) -> Result<(String, String), NbError> {
match target {
NoteTarget::Selector { value } => self.resolve_target_selector(value, notebook).await,
NoteTarget::Path { value } => {
let notebook = self.resolve_notebook(notebook).await?;
Ok((notebook.clone(), format!("{notebook}:{value}")))
}
}
}
async fn probe_show_classification(&self, selector: &str) -> ShowClassification {
let textual = self
.exec_vec(vec![
"show".to_string(),
selector.to_string(),
"--type".to_string(),
"text".to_string(),
"--no-color".to_string(),
])
.await;
if textual.is_ok() {
return ShowClassification::Textual;
}
match self
.exec_vec(vec![
"show".to_string(),
selector.to_string(),
"--type".to_string(),
"--no-color".to_string(),
])
.await
{
Ok(stdout) => {
let trimmed = stdout.trim();
if trimmed.is_empty() {
ShowClassification::ProbeFailure
} else {
ShowClassification::NonTextual {
actual_type: trimmed.to_string(),
}
}
}
Err(_) => ShowClassification::ProbeFailure,
}
}
pub async fn list_notes(
&self,
folder: Option<&str>,
tags: &[String],
limit: Option<u32>,
notebook: Option<&str>,
) -> Result<String, NbError> {
let mut args = Vec::new();
validate_folder_option(folder)?;
let notebook = self.resolve_notebook(notebook).await?;
let cmd = match folder {
Some(f) => format!("{}:{}/", notebook, f),
None => format!("{}:", notebook),
};
args.push("list".to_string());
args.push(cmd);
args.push("--no-color".to_string());
if let Some(n) = limit {
args.push("-n".to_string());
args.push(n.to_string());
}
for tag in tags {
args.push("--tags".to_string());
let tag_str = if tag.starts_with('#') {
tag.clone()
} else {
format!("#{}", tag)
};
args.push(tag_str);
}
self.exec_vec(args)
.await
.map(|output| strip_empty_result_hint(&output))
}
pub async fn search_notes(
&self,
queries: &[String],
mode: SearchMode,
tags: &[String],
folder: Option<&str>,
notebook: Option<&str>,
) -> Result<String, NbError> {
validate_folder_option(folder)?;
if queries.is_empty() {
return Err(NbError::ValidationError {
reason: "at least one search query is required".to_string(),
location: None,
});
}
let notebook = self.resolve_notebook(notebook).await?;
let scope = match folder {
Some(f) => format!("{}:{}/", notebook, f),
None => format!("{}:", notebook),
};
let args = search_command_args(scope, queries, mode, tags);
self.exec_vec(args).await
}
pub async fn delete_note(
&self,
id: &str,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let (nb, selector) = self.resolve_target_selector(id, notebook).await?;
let target = self.note_target_for_selector(&nb, &selector).await?;
let mut tx = self.transaction(Some(&nb)).await?;
tx.delete_note(target)?;
tx.commit().await
}
pub async fn move_note(
&self,
id: &str,
destination: &str,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
validate_destination(destination)?;
let (nb, selector) = self.resolve_target_selector(id, notebook).await?;
let target = self.note_target_for_selector(&nb, &selector).await?;
let mut tx = self.transaction(Some(&nb)).await?;
tx.move_note(target, destination)?;
tx.commit().await
}
async fn note_target_for_selector(
&self,
notebook: &str,
selector: &str,
) -> Result<NoteTarget, NbError> {
let root = self.show_notebook_path_unguarded(Some(notebook)).await?;
let stripped = selector
.rsplit_once(':')
.map(|(_, rest)| rest)
.unwrap_or(selector);
let direct = root.join(stripped);
if direct.is_file() {
return Ok(NoteTarget::path(stripped));
}
let abs = self.resolve_item_path(selector).await?;
let rel = path_relative_to(&root, &abs)?;
Ok(NoteTarget::path(rel))
}
pub async fn add_todo(
&self,
title: &str,
description: Option<&str>,
tasks: &[String],
tags: &[String],
folder: Option<&str>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
self.require_folder_for_new_note(folder)?;
validate_folder_option(folder)?;
let filename = transaction::auto_filename("todo.md");
let path = transaction::join_folder_file(folder, &filename);
let mut tx = self.transaction(notebook).await?;
tx.add_todo(&path, title, description, tasks, tags)?;
tx.commit().await
}
pub async fn mark_task_done(
&self,
id: &str,
task_number: Option<u32>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let (nb, selector) = self.resolve_target_selector(id, notebook).await?;
let target = self.note_target_for_selector(&nb, &selector).await?;
let mut tx = self.transaction(Some(&nb)).await?;
tx.mark_task_done(target, task_number)?;
tx.commit().await
}
pub async fn unmark_task_done(
&self,
id: &str,
task_number: Option<u32>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let (nb, selector) = self.resolve_target_selector(id, notebook).await?;
let target = self.note_target_for_selector(&nb, &selector).await?;
let mut tx = self.transaction(Some(&nb)).await?;
tx.unmark_task_done(target, task_number)?;
tx.commit().await
}
pub async fn replace_note_body(
&self,
target: NoteTarget,
new_body: impl AsRef<[u8]>,
fingerprint: Fingerprint,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let mut tx = self.transaction(notebook).await?;
tx.replace_note_body(target, new_body, fingerprint)?;
tx.commit().await
}
#[allow(clippy::too_many_arguments)]
pub async fn edit_note_substring(
&self,
target: NoteTarget,
pattern: impl AsRef<[u8]>,
replacement: impl AsRef<[u8]>,
occurrence: Occurrence,
expected_count: u32,
fingerprint: Option<Fingerprint>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let mut tx = self.transaction(notebook).await?;
tx.edit_note_substring(
target,
pattern,
replacement,
occurrence,
expected_count,
fingerprint,
)?;
tx.commit().await
}
pub async fn edit_note_lines(
&self,
target: NoteTarget,
edits: Vec<LineEdit>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let mut tx = self.transaction(notebook).await?;
tx.edit_note_lines(target, edits)?;
tx.commit().await
}
pub async fn retitle_note(
&self,
target: NoteTarget,
title: impl AsRef<[u8]>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let mut tx = self.transaction(notebook).await?;
tx.retitle_note(target, title)?;
tx.commit().await
}
pub async fn edit_note_tags(
&self,
target: NoteTarget,
add: &[String],
remove: &[String],
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
let mut tx = self.transaction(notebook).await?;
tx.edit_note_tags(target, add, remove)?;
tx.commit().await
}
pub async fn list_tasks(
&self,
folder: Option<&str>,
status: Option<TaskStatus>,
recursive: bool,
notebook: Option<&str>,
) -> Result<String, NbError> {
validate_folder_option(folder)?;
let notebook = self.resolve_notebook(notebook).await?;
let folder = folder.map(normalize_folder);
let scopes = if recursive {
self.tasks_scopes_recursive(¬ebook, folder.as_deref())
.await?
} else {
vec![tasks_scope(¬ebook, folder.as_deref())]
};
let mut outputs: Vec<String> = Vec::new();
let mut saw_empty = false;
for scope in scopes {
match self.exec_vec(tasks_command_args(scope, status)).await {
Ok(output) => {
let output = output.trim();
if !output.is_empty() {
outputs.push(output.to_string());
}
}
Err(NbError::CommandFailed { stderr, .. }) if is_empty_tasks_error(&stderr) => {
saw_empty = true;
}
Err(err) => return Err(err),
}
}
if outputs.is_empty() && saw_empty {
return Err(NbError::ValidationError {
reason: empty_tasks_message(status),
location: None,
});
}
Ok(outputs.join("\n"))
}
async fn tasks_scopes_recursive(
&self,
notebook: &str,
folder: Option<&str>,
) -> Result<Vec<String>, NbError> {
let notebook_root = self.show_notebook_path(Some(notebook)).await?;
let start = folder.unwrap_or_default().to_string();
let mut queue = VecDeque::new();
queue.push_back(start.clone());
let mut scopes = vec![tasks_scope(notebook, folder)];
while let Some(current) = queue.pop_front() {
let base = if current.is_empty() {
notebook_root.clone()
} else {
notebook_root.join(¤t)
};
let children = child_folder_names(&base)?;
for child in children {
let next = if current.is_empty() {
child
} else {
format!("{}/{}", current, child)
};
scopes.push(tasks_scope(notebook, Some(&next)));
queue.push_back(next);
}
}
Ok(scopes)
}
pub async fn add_bookmark(
&self,
url: &str,
title: Option<&str>,
tags: &[String],
comment: Option<&str>,
folder: Option<&str>,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
self.require_folder_for_new_note(folder)?;
validate_folder_option(folder)?;
let filename = transaction::auto_filename("bookmark.md");
let path = transaction::join_folder_file(folder, &filename);
let mut tx = self.transaction(notebook).await?;
tx.add_bookmark(&path, url, title, tags, comment)?;
tx.commit().await
}
pub async fn list_folders(
&self,
parent: Option<&str>,
notebook: Option<&str>,
) -> Result<String, NbError> {
let mut args = vec!["list".to_string()];
validate_folder_option(parent)?;
let notebook = self.resolve_notebook(notebook).await?;
let path = match parent {
Some(p) => format!("{}:{}/", notebook, p),
None => format!("{}:", notebook),
};
args.push(path);
args.push("--type".to_string());
args.push("folder".to_string());
args.push("--no-color".to_string());
self.exec_vec(args)
.await
.map(|output| strip_empty_result_hint(&output))
}
pub async fn add_folder(
&self,
path: &str,
notebook: Option<&str>,
) -> Result<CommitOutcome, NbError> {
validate_folder_path(path)?;
let mut tx = self.transaction(notebook).await?;
tx.add_folder(path)?;
tx.commit().await
}
pub async fn import_note(
&self,
source: &str,
folder: Option<&str>,
filename: Option<&str>,
convert: bool,
notebook: Option<&str>,
) -> Result<String, NbError> {
let mut args = Vec::new();
self.require_folder_for_new_note(folder)?;
validate_folder_option(folder)?;
let notebook = self.resolve_notebook(notebook).await?;
self.with_notebook_gate(¬ebook, async {
let cmd = format!("{}:import", notebook);
args.push(cmd);
args.push(source.to_string());
if convert {
args.push("--convert".to_string());
}
if folder.is_some() || filename.is_some() {
let dest = match (folder, filename) {
(Some(f), Some(n)) => format!("{}/{}", f, n),
(Some(f), None) => format!("{}/", f),
(None, Some(n)) => n.to_string(),
(None, None) => unreachable!(),
};
args.push(dest);
}
self.exec_vec(args)
.await
.map(|output| self.append_notebook_warning(output, ¬ebook))
})
.await
}
}
fn notebook_dir_from_env(notebook: &str) -> Option<PathBuf> {
let nb_dir = std::env::var_os("NB_DIR")?;
let path = PathBuf::from(nb_dir).join(notebook);
if path.is_dir() && path.join(".git").exists() {
Some(path)
} else {
None
}
}
fn path_relative_to(root: &Path, path: &Path) -> Result<String, NbError> {
let root = root.canonicalize().map_err(|e| NbError::Io {
path: root.to_path_buf(),
source: e.into(),
})?;
let path = path.canonicalize().map_err(|e| NbError::Io {
path: path.to_path_buf(),
source: e.into(),
})?;
let rel = path
.strip_prefix(&root)
.map_err(|_| NbError::ValidationError {
reason: format!(
"path {} is not under notebook root {}",
path.display(),
root.display()
),
location: None,
})?;
Ok(rel.to_string_lossy().replace('\\', "/"))
}