10 Commits
Author SHA1 Message Date
olivier 33544edf07 fix: v0.4.2 — frontmatter --add/--remove refuse invalid YAML instead of dropping existing items 2026-10-19 12:04:47 -04:00
olivier 34d28c03fa fix: v0.4.1 — read stdin as UTF-8 (section --content - got mojibake from the frozen exe) 2026-10-19 12:01:54 -04:00
olivier d063551a1e feat: v0.4.0 — section, resolve, list, tasks
- section: append to / replace one section, list-aware spacing, fences ignored
- resolve: filename or alias, case/accent-insensitive, --link wikilinks
- list: frontmatter filters (key=value, key!=value, key, !key), --json
- tasks: checkbox tasks by folder, date range, status, --ignore-from template, --json
- libyaml loader for whole-vault scans
2026-10-19 11:59:16 -04:00
olivier 3d3873b699 chore: v0.3.0; tests: drop dead export_to_csv and shadowed set_default tests, restore create checks 2026-10-19 11:51:58 -04:00
olivier 0f763f98ed feat(frontmatter): --add/--remove list items; --edit keeps existing list type
- --edit splits on commas for tags and for keys already holding a list
- --add/--remove change single items, matched by wikilink target, no duplicates
- refuse lists with unquoted [[links]] (parsed as nested lists)
2026-10-19 11:48:39 -04:00
olivier 543ce76207 feat: v0.2.0 — key-only frontmatter edits, link-complete move, --no-open/--print-path
- frontmatter --edit/--clear/--delete splice only the targeted keys; values written
  as Obsidian stores them (plain or double-quoted, no \xE9 escapes, no folding)
- move rewrites [[name|alias]], [[name#heading]], embeds, path and frontmatter links,
  escapes names, refuses to overwrite, leaves ambiguous bare links alone
- periodic commands: --no-open, --print-path
- stdout/stderr are UTF-8 with LF on every OS
2026-10-19 11:42:25 -04:00
olivier d77fac61c8 fix: write notes and config as LF without BOM on every OS 2026-10-19 11:21:03 -04:00
olivier 466fef3c23 docs: add RELEASING.md with the full release/update checklist 2026-09-24 20:10:12 -04:00
olivier 6eb27b7cd6 docs: document Linux build and prebuilt binary installation 2026-09-24 17:25:57 -04:00
olivier 43c50a889d build: use forward-slash path in onyx.spec for cross-platform PyInstaller builds
Backslash path only resolved on Windows; needed a working spec to build
the Linux binary (verified via PyInstaller in WSL Ubuntu 24.04).
2026-09-24 17:24:30 -04:00
5 changed files with 1224 additions and 214 deletions

No files matched your search

+98 -4
View File
@@ -2,16 +2,30 @@
A Python-based command-line interface for interacting with Obsidian vaults. Manage notes directly from the terminal without opening Obsidian.
## Building the Windows Executable (.exe)
## Building a Standalone Executable
You can build a standalone Windows executable using PyInstaller:
The same `onyx.spec` builds on both Windows and Linux — PyInstaller doesn't
cross-compile, so build natively on each platform:
```bash
pip install pyinstaller
python -m PyInstaller onyx.spec
```
The resulting `onyx.exe` will be found in the `dist` folder.
- Windows produces `dist/onyx.exe`
- Linux produces `dist/onyx` (no extension)
Prebuilt binaries for both platforms are also attached to each release:
https://gitea.mithrilforge.dev/olivier/onyx-cli/releases
### Installing the Linux binary
Drop the binary (built or downloaded) anywhere on your `PATH`:
```bash
chmod +x onyx
mv onyx ~/.local/bin/onyx # or /usr/local/bin/onyx (needs sudo)
```
## Installation
@@ -94,8 +108,16 @@ onyx weekly --previous # opens last week's note
onyx monthly --next # opens next month's note
onyx quarterly --next # opens next quarter's note
onyx yearly --previous # opens last year's note
# Scripts and agents: create if missing without opening Obsidian, print only the path
onyx daily --date "2025-11-27" --no-open --print-path
```
Output is UTF-8 with LF line endings on every OS, so `p=$(onyx daily --print-path)` works in
bash on Windows too. With `--no-open`, a newly created note keeps the template's raw Templater
syntax (`<% … %>`) until Obsidian processes it; when Obsidian is running, Templater's "trigger on
new file creation" renders it within seconds.
**Default filename formats** (used when nothing else is configured):
- Daily: `YYYY-MM-DD.md`
- Weekly: `YYYY-WWW.md` (ISO week, e.g. `2025-W48.md`)
@@ -169,7 +191,8 @@ onyx print-note "note-name" --vault "vault-name"
### Moving & Deleting Notes
```bash
# Move or rename a note (auto-updates internal links)
# Move or rename a note; rewrites every wikilink that resolves to it:
# [[name]], [[name|alias]], [[name#heading]], ![[embeds]], [[folder/name]], frontmatter links
onyx move "old-path" "new-path"
# Move in specific vault
@@ -180,7 +203,14 @@ onyx move "old-path" "new-path" --open
# Delete a note
onyx delete "note-path"
```
`move` refuses to overwrite an existing note. A bare `[[name]]` link is only rewritten when that
name was unique in the vault (otherwise it is reported and left alone); the new link stays bare
unless the new name is shared by another note, in which case it becomes a path link. Markdown
`[text](path.md)` links are not rewritten.
```bash
# Delete from specific vault
onyx delete "note-path" --vault "vault-name"
```
@@ -201,6 +231,10 @@ onyx frontmatter "note-name" --edit --key "tags" --value "project"
onyx frontmatter "note-name" --edit --key "tags" --value "project,urgent"
onyx frontmatter "note-name" --edit --key "status" --value "in-progress"
# Add / remove single items of a list property (repeat --value for several items)
onyx frontmatter "note-name" --add --key "participants" --value "[[Haro]]" --value "[[Jeffrey]]"
onyx frontmatter "note-name" --remove --key "participants" --value "[[Haro]]"
# Clear frontmatter values
onyx frontmatter "note-name" --clear --key "tags"
onyx frontmatter "note-name" --clear --key "date,status,tags"
@@ -210,6 +244,62 @@ onyx frontmatter "note-name" --delete --key "tags"
onyx frontmatter "note-name" --delete --key "date,status,tags"
```
Edits only touch the targeted keys: key order, quoting, accents and long values elsewhere in the
frontmatter stay byte-for-byte identical. Values are written the way Obsidian stores them: plain
when YAML reads them back unchanged (`done`, `2026-10-19`, `3`), double-quoted otherwise
(`"[[Haro]]"`). `tags`, and any key that is already a list in the note, take a comma-separated
list with `--edit`, written as a block list. Cleared keys become empty (`status:`), cleared tags
`tags: []`.
`--add` and `--remove` change one list without rewriting it: items are taken whole (no comma
splitting) and matched by wikilink target, so `[[Name|Alias]]` and `[[name]]` are the same item.
`--add` skips duplicates, creates the list if the key is missing, and turns a text value into the
first item. A list holding unquoted `[[links]]` (which YAML reads as nested lists) is refused.
### Editing One Section
```bash
# Append at the end of a section (before the next heading of the same or a higher level)
onyx section "periodic/daily/2026-10-19" --heading "## Journal" --content "### 10:00 - Titre\n- note"
onyx section "periodic/daily/2026-10-19" --heading "### Matin" --content "- [ ] Appeler Haro"
# Replace a section's body; --content - reads stdin; --create adds a missing section at the end
onyx section "periodic/weekly/2026-W43" --heading "## Weekly review" --replace --create --content - < review.md
```
`--heading "## Journal"` matches that level only; `--heading Journal` matches any level (case-insensitive).
Appended text gets a blank line before it, except between two list items, so a list stays one list.
Headings in the frontmatter or in code blocks are ignored, and everything outside the section is left
byte-for-byte. A missing heading is an error (listing the note's headings) unless `--create`.
### Finding Notes
```bash
# By filename or alias: exact, ignoring case and accents; exit code 1 when nothing matches
onyx resolve "Stéphane" --folder entities --link # [[Stéphane Charbonneau|Stéphane]], one line per match
onyx resolve "METC" # entities/businesses/Mousquetons et Cie
# By frontmatter (repeatable --where = AND): key=value, key!=value, key, !key
onyx list --folder projects --where type=project --where status=active
onyx list --where client=Symbolitech --where "status!=done" --json
```
`--where` values compare without case or accents, a `[[link]]` compares by its target, and a list matches
when any item does. `resolve --link` uses a path link when the filename is shared by several notes.
### Tasks
```bash
# Open and in-progress tasks of a week's daily notes, minus the template's recurring ones
onyx tasks --folder periodic/daily --from 2026-10-12 --to 2026-10-18 --status open,doing \
--ignore-from resources/template/daily-note
onyx tasks --folder projects --status open --json # path, line, date, status, heading, text
```
Statuses: `- [ ]` open, `- [/]` doing, `- [x]` done, `- [-]` cancelled. With `--from`/`--to`, a note's
date is the `YYYY-MM-DD` at the start of its filename, else its `date` property; undated notes are
skipped. Output is `path:line: - [ ] text`, like grep.
### Version
```bash
@@ -226,6 +316,8 @@ onyx --version
- **Periodic Notes**: Support for daily, weekly, monthly, quarterly, and yearly notes
- **Frontmatter Management**: Edit, view, clear, and delete YAML frontmatter
- **Link Updates**: Automatically update internal links when moving notes
- **Sections**: Append to or replace one section of a note
- **Queries**: Resolve notes by name/alias, list notes by frontmatter, list tasks by date and status
- **Cross-Platform**: Works on macOS, Windows, and Linux
## Requirements
@@ -236,6 +328,8 @@ onyx --version
## Development
See [RELEASING.md](RELEASING.md) for the full version-bump/build/tag/release checklist.
### Running Tests
```bash
+177
View File
@@ -0,0 +1,177 @@
# Release procedure
Onyx ships as two standalone binaries (Windows `.exe`, Linux ELF) attached to a
Gitea release — no PyPI package, no installer. This document is the checklist
for cutting a new version.
## 1. Land the change
Implement, then verify:
```bash
python -m pytest tests/ -q
```
All tests must pass (skips are fine) before bumping anything.
## 2. Bump the version
The version string lives in exactly one place: the `@click.version_option`
decorator on the `cli` group in `src/onyx.py`.
```python
@click.version_option(version='X.Y.Z', prog_name='onyx')
```
Use semver: patch for fixes only, minor for new commands/flags, major for
breaking changes (removed/renamed commands, changed config schema).
## 3. Build both platform binaries
PyInstaller does **not** cross-compile — each binary must be built natively
on its target platform.
**Windows** (run directly on this machine):
```bash
rm -rf build dist
python -m PyInstaller onyx.spec
# -> dist/onyx.exe
```
**Linux** (via WSL, since this machine is Windows): build on WSL's native
ext4 filesystem, not `/mnt/c/...` — PyInstaller/venvs are unreliable over
the drvfs 9p mount.
```bash
wsl -d Ubuntu-24.04 -- bash -lc '
set -e
rm -rf ~/onyx-build && mkdir -p ~/onyx-build
cp -r "/mnt/c/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/src" ~/onyx-build/
cp "/mnt/c/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/onyx.spec" ~/onyx-build/
cp "/mnt/c/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/requirements.txt" ~/onyx-build/
cd ~/onyx-build
python3 -m venv .venv
.venv/bin/pip install -q --upgrade pip
.venv/bin/pip install -q -r requirements.txt pyinstaller
rm -rf build dist
.venv/bin/pyinstaller onyx.spec
'
```
Copy the result out and rename it (Windows already produced `dist/onyx.exe`
in the same folder, and Linux's output has no extension — rename to avoid
any ambiguity):
```bash
wsl -d Ubuntu-24.04 -- bash -lc "cp /home/oli/onyx-build/dist/onyx '/mnt/c/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/dist/onyx-linux'"
```
> Passing multi-line scripts to `wsl.exe` through a shell tool can mangle
> line endings (CRLF) and silently break variable assignments. If a WSL
> command behaves inexplicably (e.g. `command not found` for something
> that should be a variable), write the script to a file and run that file
> instead of an inline heredoc.
## 4. Verify both binaries
```bash
dist/onyx.exe --version # Windows, via cmd.exe (see gotcha below)
wsl -d Ubuntu-24.04 -- ~/onyx-build/dist/onyx --version
```
Confirm the printed version matches step 2. Smoke-test at least one command
end to end (e.g. `periodic --view`, `weekly --date 2025-11-27`) against a
scratch vault/config — never against the real `~/.onyx-cli/config.yaml`.
> **Windows gotcha**: the plain bash tool can't execute a freshly built
> `.exe` directly (`command not found`) — always invoke it through
> `cmd.exe /c "onyx.exe ..."`.
## 5. Deploy locally (optional)
Copy the Windows build to the installed location so `onyx` on this machine
picks up the new version immediately:
```bash
cp dist/onyx.exe "/c/Users/oli/Documents/o/Resources/app back up/bin/onyx.exe"
```
## 6. Commit, tag, push
```bash
git add -A
git commit -m "chore: bump to vX.Y.Z"
git fetch origin # someone may have committed via the Gitea web UI
git log --oneline origin/main -3
```
If `origin/main` has commits you don't have locally, **rebase, don't
merge** (this repo's history is linear):
```bash
git rebase origin/main
```
Then tag (annotated, with a real changelog since the last tag) and push:
```bash
git tag -a vX.Y.Z -m "vX.Y.Z
- ..."
git push origin main
git push origin vX.Y.Z
```
> If you rebased *after* creating the tag, the tag still points at the
> pre-rebase (now orphaned) commit. Delete and recreate it:
> `git tag -d vX.Y.Z && git tag -a vX.Y.Z -m "..."` before pushing.
## 7. Create the Gitea release
`tea` hangs indefinitely under a plain non-interactive shell call, for
*any* subcommand, including fully local ones. Always launch it as a
supervised process (hub `start`/`wait`/`logs`), never via the bash tool
directly.
Write release notes to a scratch file, then create the release with both
assets in one call:
```
tea releases create --repo olivier/onyx-cli --tag vX.Y.Z --title vX.Y.Z \
--note-file release-notes-vX.Y.Z.md -a dist/onyx.exe -a dist/onyx-linux
```
If the release already exists and you're adding a platform binary later
(e.g. the Linux build wasn't ready yet), attach it separately instead:
```
tea releases assets create --repo olivier/onyx-cli vX.Y.Z dist/<file>
```
Delete the scratch release-notes file afterward — it's not a repo
convention file.
> `tea repos create --owner olivier` fails with `GetOrgByName` — `--owner`
> targets an organization, not your personal account. Omit it entirely
> when creating a repo under your own user.
## 8. Verify the release
```bash
curl -s https://gitea.mithrilforge.dev/api/v1/repos/olivier/onyx-cli/releases/tags/vX.Y.Z \
| python -c "import json,sys; d=json.load(sys.stdin); [print(a['name'], a['size']) for a in d['assets']]"
```
Confirm both `onyx.exe` and `onyx-linux` are listed with nonzero, distinct
sizes.
## 9. Clean up
```bash
wsl -d Ubuntu-24.04 -- bash -lc "rm -rf ~/onyx-build"
rm -f release-notes-vX.Y.Z.md
```
`dist/` and `build/` are gitignored — the binaries never get committed,
only attached to the release.
+1 -1
View File
@@ -2,7 +2,7 @@
a = Analysis(
['src\\onyx.py'],
['src/onyx.py'],
pathex=[],
binaries=[],
datas=[],
+630 -144
View File
@@ -9,9 +9,226 @@ import datetime
import builtins
from pathlib import Path
import re
import unicodedata
from collections import Counter
from typing import List, Union, Optional, Dict, Any
from urllib.parse import quote
def _write_text(path: Path, text: str, mode: str = 'w') -> None:
"""Write UTF-8 without BOM and with LF line endings on every OS.
Python's text mode would otherwise translate '\\n' to os.linesep (CRLF on
Windows), and 'utf-8-sig' would prepend a BOM before the frontmatter.
Reads keep 'utf-8-sig' so files that already carry a BOM still parse.
"""
with path.open(mode, encoding='utf-8', newline='\n') as f:
f.write(text)
_FRONTMATTER = re.compile(r'\A---\n(.*?)^---[ \t]*(?:\n|\Z)', re.S | re.M)
_FRONTMATTER_KEY = re.compile(r'^([^\s#\-][^:]*?):(?:[ \t]|$)')
_WIKILINK = re.compile(r'\[\[([^\[\]|#]+?)(#[^\[\]|]*)?(\|[^\[\]]*)?\]\]')
def _yaml_scalar(value: str) -> str:
"""Render a CLI string as Obsidian stores it: plain when YAML reads it back
as-is (text, numbers, dates, booleans), double-quoted otherwise ([[links]],
': ', '#', leading/trailing spaces)."""
try:
parsed = yaml.safe_load(value)
except yaml.YAMLError:
parsed = None
plain = parsed == value or isinstance(parsed, (bool, int, float, datetime.date))
return value if plain and value == value.strip() else json.dumps(value, ensure_ascii=False)
def _frontmatter_entry(key: str, value: Union[str, list]) -> str:
if isinstance(value, list):
if not value:
return f"{key}: []\n"
return f"{key}:\n" + ''.join(f" - {_yaml_scalar(v)}\n" for v in value)
return f"{key}: {_yaml_scalar(value)}\n" if value else f"{key}:\n"
def _splice_frontmatter(content: str, updates: Dict[str, Union[str, list]], deletions=()) -> str:
"""Replace, add or remove top-level frontmatter keys, leaving every other
line of the note byte-for-byte untouched. A key's block is its line plus the
following indented or '- ' lines. Removing the last key drops the frontmatter."""
match = _FRONTMATTER.match(content)
fm, body = (match.group(1), content[match.end():]) if match else ('', content)
blocks: List[list] = [] # [key or None, raw text]
for line in fm.splitlines(keepends=True):
key_match = _FRONTMATTER_KEY.match(line)
if key_match or not blocks:
blocks.append([key_match.group(1).strip() if key_match else None, line])
else:
blocks[-1][1] += line
if not updates and not any(key in deletions for key, _ in blocks):
return content
out, seen = [], set()
for key, text in blocks:
if key in deletions:
continue
if key in updates:
out.append(_frontmatter_entry(key, updates[key]))
seen.add(key)
else:
out.append(text if text.endswith('\n') else text + '\n')
out.extend(_frontmatter_entry(k, v) for k, v in updates.items() if k not in seen)
new_fm = ''.join(out)
return f"---\n{new_fm}---\n{body}" if new_fm.strip() else body
def _iter_notes(vault_path: Path):
"""Markdown files in the vault, skipping dot folders (.obsidian, .trash, .git)."""
for path in vault_path.rglob('*.md'):
if not any(part.startswith('.') for part in path.relative_to(vault_path).parts[:-1]):
yield path
_HEADING = re.compile(r'^(#{1,6})[ \t]+(.*?)[ \t]*$')
_FENCE = re.compile(r'^[ \t]*(`{3,}|~{3,})')
_LIST_ITEM = re.compile(r'^[ \t]*([-*+]|\d+[.)])[ \t]')
def _section_bounds(lines: List[str], first: int, heading: str) -> Optional[tuple]:
"""(heading line, end line) of the first section titled `heading`, skipping fenced code.
`heading` is "## Title" (that level only) or "Title" (any level); case-insensitive.
The section ends at the next heading of the same or a higher level, or at EOF."""
wanted = _HEADING.match(heading.strip())
level_wanted, title_wanted = (len(wanted.group(1)), wanted.group(2)) if wanted else (None, heading.strip())
fence, start, level = None, None, None
for i in range(first, len(lines)):
line = lines[i].rstrip('\n')
fence_match = _FENCE.match(line)
if fence_match:
marker = fence_match.group(1)[0]
fence = None if fence == marker else (fence or marker)
continue
match = None if fence else _HEADING.match(line)
if not match:
continue
if start is None:
if (match.group(2).casefold() == title_wanted.casefold()
and level_wanted in (None, len(match.group(1)))):
start, level = i, len(match.group(1))
elif len(match.group(1)) <= level:
return start, i
return (start, len(lines)) if start is not None else None
def _write_section(text: str, heading: str, content: str, replace: bool = False, create: bool = False) -> str:
"""Append `content` at the end of a section (or replace the section's body).
Appending adds a blank line before the new content, except between two list items so
a list stays one list. Blank lines before the next heading are preserved. A missing
section raises KeyError unless `create`, which adds it at the end of the note."""
match = _FRONTMATTER.match(text)
lines = text.splitlines(keepends=True)
if lines and not lines[-1].endswith('\n'):
lines[-1] += '\n'
new = [line + '\n' for line in content.strip('\n').split('\n')] if content.strip() else []
bounds = _section_bounds(lines, text[:match.end()].count('\n') if match else 0, heading)
if bounds is None:
if not create:
raise KeyError(heading)
title = heading.strip() if heading.strip().startswith('#') else f"## {heading.strip()}"
while lines and not lines[-1].strip():
lines.pop()
return ''.join(lines + (['\n'] if lines else []) + [title + '\n'] + new)
start, end = bounds
body = lines[start + 1:end]
keep = len(body)
while keep and not body[keep - 1].strip():
keep -= 1
kept, trailing = body[:keep], body[keep:]
if replace:
kept = new
elif new:
lists_touch = kept and _LIST_ITEM.match(kept[-1]) and _LIST_ITEM.match(new[0])
kept = kept + ([] if not kept or lists_touch else ['\n']) + new
return ''.join(lines[:start + 1] + kept + trailing + lines[end:])
_TASK = re.compile(r'^[ \t]*[-*+][ \t]+\[(.)\][ \t]+(.*?)[ \t]*$')
_TASK_STATUS = {' ': 'open', 'x': 'done', 'X': 'done', '/': 'doing', '-': 'cancelled'}
_DATE_PREFIX = re.compile(r'^(\d{4}-\d{2}-\d{2})')
_YAML_LOADER = getattr(yaml, 'CSafeLoader', yaml.SafeLoader) # libyaml: ~7x faster on whole-vault scans
def _read_frontmatter(text: str, needed: Optional[str] = None) -> dict:
"""Frontmatter as a dict; {} when missing or invalid YAML. With `needed`, skip the
YAML parse when that key name doesn't even appear in the frontmatter."""
match = _FRONTMATTER.match(text)
if not match or (needed and needed not in match.group(1)):
return {}
try:
data = yaml.load(match.group(1), Loader=_YAML_LOADER)
except yaml.YAMLError:
return {}
return data if isinstance(data, dict) else {}
def _fold(value: Any) -> str:
"""Comparison form: wikilink target if the value is one link, accents and case removed."""
text = str(value).strip()
link = _WIKILINK.fullmatch(text)
if link:
text = link.group(1).strip()
decomposed = unicodedata.normalize('NFKD', text)
return ''.join(c for c in decomposed if not unicodedata.combining(c)).casefold()
def _where_matches(frontmatter: dict, expression: str) -> bool:
"""key=value, key!=value, key (present and non-empty), !key (missing or empty).
A list matches `=` when any item matches."""
match = re.fullmatch(r'\s*(!?)([^=!]+?)\s*(?:(!?=)\s*(.*?))?\s*', expression)
if not match:
raise click.UsageError(f"Invalid --where '{expression}': use key=value, key!=value, key or !key")
negate_key, key, operator, wanted = match.groups()
value = frontmatter.get(key)
items = value if isinstance(value, list) else ([] if value in (None, '') else [value])
if operator is None:
return not items if negate_key else bool(items)
found = any(_fold(item) == _fold(wanted) for item in items)
return found if operator == '=' else not found
def _iter_tasks(text: str):
"""(line number, status char, text, nearest heading) for each checkbox outside
frontmatter and fenced code."""
match = _FRONTMATTER.match(text)
lines = text.splitlines()
fence, heading = None, ''
for number in range(text[:match.end()].count('\n') if match else 0, len(lines)):
line = lines[number]
fence_match = _FENCE.match(line)
if fence_match:
marker = fence_match.group(1)[0]
fence = None if fence == marker else (fence or marker)
continue
if fence:
continue
heading_match = _HEADING.match(line)
if heading_match:
heading = heading_match.group(2)
continue
task = _TASK.match(line)
if task:
yield number + 1, task.group(1), task.group(2), heading
class OnyxCLI:
_PERIODIC_DEFAULTS: Dict[str, Dict[str, str]] = {
'daily': {'format': 'YYYY-MM-DD', 'folder': '', 'template': ''},
@@ -37,7 +254,7 @@ class OnyxCLI:
def _ensure_config_exists(self):
self.config_file.parent.mkdir(parents=True, exist_ok=True)
if not self.config_file.exists():
self.config_file.write_text(yaml.dump({'default_vault': None, 'vaults': {}}), encoding='utf-8-sig')
_write_text(self.config_file, yaml.dump({'default_vault': None, 'vaults': {}}))
def _load_default_vault(self):
if self.config_file.exists():
@@ -117,7 +334,7 @@ class OnyxCLI:
config['vaults'][vault_name] = vault_config
# Write updated config
self.config_file.write_text(yaml.dump(config, default_flow_style=False), encoding='utf-8-sig')
_write_text(self.config_file, yaml.dump(config, default_flow_style=False))
# Update instance variables
self.default_vault = vault_name
@@ -170,7 +387,8 @@ class OnyxCLI:
return None
def create_note(self, note_name: str, content: str = "", vault: str = None,
overwrite: bool = False, append: bool = False, open_after: bool = False, template: str = None):
overwrite: bool = False, append: bool = False, open_after: bool = False, template: str = None,
quiet: bool = False):
vault_name, vault_path = self._get_vault_and_path(vault)
note_path = vault_path / note_name
if not str(note_path).endswith('.md'):
@@ -209,11 +427,11 @@ class OnyxCLI:
else:
mode = 'w'
with note_path.open(mode, encoding='utf-8-sig') as f:
if append and note_path.exists():
f.write('\n')
f.write(content)
if mode == 'a':
content = '\n' + content
_write_text(note_path, content, mode)
if not quiet:
click.echo(f"Note {'updated' if note_path.exists() else 'created'}: {note_path}")
if open_after:
@@ -325,8 +543,13 @@ class OnyxCLI:
return result
def _create_or_open_periodic_note(self, note_type: str, vault: str = None, date: str = None, next_: bool = False, previous: bool = False):
"""Generic method to create or open periodic notes with date navigation."""
def _create_or_open_periodic_note(self, note_type: str, vault: str = None, date: str = None, next_: bool = False,
previous: bool = False, open_after: bool = True, print_path: bool = False):
"""Generic method to create or open periodic notes with date navigation.
open_after=False creates the note (if missing) without opening Obsidian; Templater
syntax in the template then stays raw until Obsidian processes the new file.
print_path=True prints only the note's absolute path."""
vault_name, vault_path = self._get_vault_and_path(vault)
settings = self._resolve_periodic_settings(vault_name, vault_path, note_type)
@@ -424,29 +647,138 @@ class OnyxCLI:
full_note_path = full_note_path.with_suffix('.md')
if full_note_path.exists():
if open_after:
self.open_note(note_path, vault_name)
else:
self.create_note(note_path, content=content, vault=vault_name, open_after=True)
self.create_note(note_path, content=content, vault=vault_name, open_after=open_after, quiet=print_path)
if print_path:
click.echo(str(full_note_path))
def daily_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False):
def daily_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False,
open_after: bool = True, print_path: bool = False):
"""Create or open a daily note for a specific date, next, or previous day."""
self._create_or_open_periodic_note('daily', vault, date=date, next_=next_, previous=previous)
self._create_or_open_periodic_note('daily', vault, date, next_, previous, open_after, print_path)
def weekly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False):
def weekly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False,
open_after: bool = True, print_path: bool = False):
"""Create or open a weekly note for a specific date, next, or previous week."""
self._create_or_open_periodic_note('weekly', vault, date=date, next_=next_, previous=previous)
self._create_or_open_periodic_note('weekly', vault, date, next_, previous, open_after, print_path)
def monthly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False):
def monthly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False,
open_after: bool = True, print_path: bool = False):
"""Create or open a monthly note for a specific date, next, or previous month."""
self._create_or_open_periodic_note('monthly', vault, date=date, next_=next_, previous=previous)
self._create_or_open_periodic_note('monthly', vault, date, next_, previous, open_after, print_path)
def quarterly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False):
def quarterly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False,
open_after: bool = True, print_path: bool = False):
"""Create or open a quarterly note for a specific date, next, or previous quarter."""
self._create_or_open_periodic_note('quarterly', vault, date=date, next_=next_, previous=previous)
self._create_or_open_periodic_note('quarterly', vault, date, next_, previous, open_after, print_path)
def yearly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False):
def yearly_note(self, vault: str = None, date: str = None, next_: bool = False, previous: bool = False,
open_after: bool = True, print_path: bool = False):
"""Create or open a yearly note for a specific date, next, or previous year."""
self._create_or_open_periodic_note('yearly', vault, date=date, next_=next_, previous=previous)
self._create_or_open_periodic_note('yearly', vault, date, next_, previous, open_after, print_path)
def write_section(self, note_name: str, heading: str, content: str, replace: bool = False,
create: bool = False, vault: str = None):
"""Append to (or replace) one section of a note; the rest of the note is untouched."""
vault_name, vault_path = self._get_vault_and_path(vault)
note_path = self._find_note(vault_path, note_name)
text = note_path.read_text(encoding='utf-8-sig')
try:
new_text = _write_section(text, heading, content, replace, create)
except KeyError:
lines = text.splitlines()
headings = [line for line in lines if _HEADING.match(line)]
raise click.UsageError(f"Heading '{heading}' not found in {note_name} (use --create to add it). "
f"Headings: {', '.join(headings) or 'none'}")
if new_text != text:
_write_text(note_path, new_text)
click.echo(f"{'Replaced' if replace else 'Updated'} section '{heading}' in {note_name}")
def resolve_note(self, name: str, folder: str = None, link: bool = False, vault: str = None) -> int:
"""Print notes whose filename or one of whose aliases equals `name` (case and accents
ignored, no fuzzy matching). Returns the number of matches."""
vault_name, vault_path = self._get_vault_and_path(vault)
notes = list(_iter_notes(vault_path))
stems = Counter(p.stem.casefold() for p in notes)
wanted = _fold(name)
matches = 0
for note in sorted(notes):
if folder and not note.relative_to(vault_path).as_posix().startswith(folder.strip('/') + '/'):
continue
if _fold(note.stem) == wanted:
display = None
else:
text = note.read_text(encoding='utf-8-sig')
aliases = _read_frontmatter(text, needed='aliases').get('aliases') or []
aliases = aliases if isinstance(aliases, list) else [aliases]
display = next((str(a) for a in aliases if a is not None and _fold(a) == wanted), None)
if display is None:
continue
matches += 1
rel = note.relative_to(vault_path).with_suffix('').as_posix()
if link:
target = note.stem if stems[note.stem.casefold()] == 1 else rel
click.echo(f"[[{target}|{display}]]" if display else f"[[{target}]]")
else:
click.echo(rel)
return matches
def _notes_in(self, vault_path: Path, folder: Optional[str]) -> List[Path]:
base = vault_path / folder if folder else vault_path
if not base.is_dir():
raise click.UsageError(f"Folder {folder} does not exist")
return sorted(p for p in _iter_notes(vault_path) if base in p.parents)
def list_notes(self, folder: str = None, where: tuple = (), as_json: bool = False, vault: str = None):
"""Notes whose frontmatter matches every --where expression."""
vault_name, vault_path = self._get_vault_and_path(vault)
results = []
for note in self._notes_in(vault_path, folder):
frontmatter = _read_frontmatter(note.read_text(encoding='utf-8-sig'))
if all(_where_matches(frontmatter, expression) for expression in where):
results.append((note.relative_to(vault_path).with_suffix('').as_posix(), frontmatter))
if as_json:
click.echo(json.dumps([{'path': rel, 'frontmatter': fm} for rel, fm in results],
ensure_ascii=False, indent=2, default=str))
else:
for rel, _ in results:
click.echo(rel)
def list_tasks(self, folder: str = None, date_from: str = None, date_to: str = None,
statuses: Optional[set] = None, ignore_from: str = None, as_json: bool = False,
vault: str = None):
"""Checkbox tasks across notes. With --from/--to, a note's date is its filename's
YYYY-MM-DD prefix, else its `date` property; undated notes are skipped."""
vault_name, vault_path = self._get_vault_and_path(vault)
ignored = set()
if ignore_from:
text = self._find_note(vault_path, ignore_from).read_text(encoding='utf-8-sig')
ignored = {_fold(task_text) for _, _, task_text, _ in _iter_tasks(text)}
results = []
for note in self._notes_in(vault_path, folder):
text = note.read_text(encoding='utf-8-sig')
prefix = _DATE_PREFIX.match(note.stem)
note_date = prefix.group(1) if prefix else str(_read_frontmatter(text).get('date') or '')[:10]
if (date_from or date_to) and not _DATE_PREFIX.match(note_date):
continue
if (date_from and note_date < date_from) or (date_to and note_date > date_to):
continue
rel = note.relative_to(vault_path).as_posix()
for line, char, task_text, heading in _iter_tasks(text):
status = _TASK_STATUS.get(char, char)
if (statuses and status not in statuses) or _fold(task_text) in ignored:
continue
results.append({'path': rel, 'line': line, 'date': note_date or None, 'status': status,
'heading': heading, 'text': task_text, 'char': char})
if as_json:
click.echo(json.dumps([{k: v for k, v in r.items() if k != 'char'} for r in results],
ensure_ascii=False, indent=2))
else:
for r in results:
click.echo(f"{r['path']}:{r['line']}: - [{r['char']}] {r['text']}")
def print_note(self, note_name: str, vault: str = None):
vault_name, vault_path = self._get_vault_and_path(vault)
@@ -460,6 +792,13 @@ class OnyxCLI:
click.echo(note_path.read_text(encoding='utf-8-sig'))
def move_note(self, current_path: str, new_path: str, vault: str = None, open_after: bool = False):
"""Move/rename a note and rewrite wikilinks that resolve to it.
Handles [[name]], [[name|alias]], [[name#heading]], ![[embeds]], path links
([[folder/name]], partial paths, optional .md), frontmatter links, and links in
the moved note itself. A bare-name link is only rewritten when the old name was
unique in the vault; the new link is bare unless the new name is ambiguous.
Markdown-style [text](path.md) links are not rewritten."""
vault_name, vault_path = self._get_vault_and_path(vault)
current = vault_path / current_path
@@ -472,22 +811,57 @@ class OnyxCLI:
if not current.exists():
raise click.UsageError(f"Note {current_path} does not exist")
if new.exists() and not new.samefile(current):
raise click.UsageError(f"Note {new_path} already exists")
old_rel = current.relative_to(vault_path).with_suffix('').as_posix()
new_rel = new.relative_to(vault_path).with_suffix('').as_posix()
new.parent.mkdir(parents=True, exist_ok=True)
current.rename(new)
# Update internal links in all vault files
old_link_pattern = f"\\[\\[{current.stem}\\]\\]"
new_link = f"[[{new.stem}]]"
notes = list(_iter_notes(vault_path))
stems = Counter(p.stem.casefold() for p in notes)
old_stem, new_stem = current.stem.casefold(), new.stem.casefold()
stem_changed = old_stem != new_stem
old_bare_unique = stems[old_stem] == (0 if stem_changed else 1)
new_target_bare = new.stem if stems[new_stem] == 1 else new_rel
old_rel_cf = old_rel.casefold()
links = ambiguous = 0
for md_file in vault_path.rglob("*.md"):
if md_file != new: # Skip the moved file
def rewrite(match: re.Match) -> str:
nonlocal links, ambiguous
target = match.group(1).strip().replace('\\', '/')
has_md = target.lower().endswith('.md')
key = (target[:-3] if has_md else target).casefold()
if '/' in key:
if key.lstrip('/') != old_rel_cf and not old_rel_cf.endswith('/' + key):
return match.group(0)
replacement = new_rel
else:
if key != old_stem:
return match.group(0)
if not old_bare_unique:
ambiguous += stem_changed
return match.group(0)
replacement = new_target_bare
replacement += '.md' if has_md else ''
if replacement == target:
return match.group(0)
links += 1
return f"[[{replacement}{match.group(2) or ''}{match.group(3) or ''}]]"
files = 0
for md_file in notes:
content = md_file.read_text(encoding='utf-8-sig')
updated_content = re.sub(old_link_pattern, new_link, content)
if updated_content != content:
md_file.write_text(updated_content, encoding='utf-8-sig')
updated = _WIKILINK.sub(rewrite, content)
if updated != content:
_write_text(md_file, updated)
files += 1
click.echo(f"Moved {current_path} to {new_path}")
click.echo(f"Moved {current_path} to {new_path} ({links} link(s) updated in {files} note(s))")
if ambiguous:
click.echo(f"Warning: {ambiguous} [[{current.stem}]] link(s) left unchanged: "
f"several notes are named '{current.stem}'")
if open_after:
self.open_note(new_path, vault_name)
@@ -518,32 +892,47 @@ class OnyxCLI:
except yaml.YAMLError:
return {}, content, 0
def _update_frontmatter(self, note_path: Path, updates: dict):
content = note_path.read_text(encoding='utf-8-sig')
frontmatter, rest_content, fm_length = self._parse_frontmatter(content)
frontmatter.update(updates)
new_content = '---\n'
new_content += yaml.dump(frontmatter, default_flow_style=False)
new_content += '---\n'
new_content += rest_content if fm_length else content
note_path.write_text(new_content, encoding='utf-8-sig')
def frontmatter_edit(self, note_name: str, keys: tuple, values: tuple = None, vault: str = None):
vault_name, vault_path = self._get_vault_and_path(vault)
def _find_note(self, vault_path: Path, note_name: str) -> Path:
"""Exact vault-relative path first, then the first note with that name anywhere."""
note_path = vault_path / note_name
if not note_path.suffix:
note_path = note_path.with_suffix('.md')
if not note_path.exists():
matches = list(vault_path.rglob(f"{note_name}.md"))
if matches:
note_path = matches[0]
if not note_path.exists():
raise click.UsageError(f"Note {note_name} does not exist")
return note_path
@staticmethod
def _split_keys(keys: tuple) -> List[str]:
"""--key values, also accepting comma/bracket-separated keys (legacy)."""
return [k.strip() for key in keys for k in re.split(r',|\[|\]', key) if k.strip()]
def _rewrite_frontmatter(self, note_path: Path, updates: dict, deletions=()) -> bool:
content = note_path.read_text(encoding='utf-8-sig')
new_content = _splice_frontmatter(content, updates, deletions)
if new_content == content:
return False
_write_text(note_path, new_content)
return True
@staticmethod
def _split_list(value: Optional[str]) -> List[str]:
return [item.strip() for item in value.split(',') if item.strip()] if value else []
@staticmethod
def _list_item_id(item: str) -> str:
"""Identity of a list item: a wikilink's target (alias and heading ignored), else its text."""
match = _WIKILINK.fullmatch(item.strip())
return (match.group(1).strip() if match else item.strip()).casefold()
def frontmatter_edit(self, note_name: str, keys: tuple, values: tuple = None, vault: str = None):
"""Set keys. `tags` and keys that are already lists in the note take a comma-separated list."""
vault_name, vault_path = self._get_vault_and_path(vault)
note_path = self._find_note(vault_path, note_name)
existing, _, _ = self._parse_frontmatter(note_path.read_text(encoding='utf-8-sig'))
updates = {}
@@ -552,36 +941,63 @@ class OnyxCLI:
values = values * len(keys)
for i, key in enumerate(keys):
# Handle comma-separated keys in a single string (legacy support)
sub_keys = [k.strip() for k in re.split(r',|\[|\]', key) if k.strip()]
value = values[i] if values and i < len(values) else None
for k in sub_keys:
if k == 'tags':
if value:
updates[k] = [t.strip() for t in value.split(',')] if ',' in value else [value.strip()]
else:
updates[k] = []
for k in self._split_keys((key,)):
if k == 'tags' or isinstance(existing.get(k), list):
updates[k] = self._split_list(value)
else:
updates[k] = value if value is not None else ""
self._update_frontmatter(note_path, updates)
self._rewrite_frontmatter(note_path, updates)
click.echo(f"Updated frontmatter for {note_name}")
def frontmatter_list_items(self, note_name: str, key: str, items: tuple, remove: bool = False,
vault: str = None):
"""Add items to, or remove them from, a list property; other items and keys stay as they are.
Items are matched by wikilink target, so "[[Name|Alias]]" and "[[name]]" are the same
item. Adding skips duplicates; a missing key becomes a list, a text value becomes its
first item. Each item is taken whole (no comma splitting)."""
vault_name, vault_path = self._get_vault_and_path(vault)
note_path = self._find_note(vault_path, note_name)
match = _FRONTMATTER.match(note_path.read_text(encoding='utf-8-sig'))
try:
existing = (yaml.load(match.group(1), Loader=_YAML_LOADER) or {}) if match else {}
except yaml.YAMLError as error:
# An unreadable frontmatter would look like "key missing" and the splice would
# replace the existing (unparsed) items: refuse instead of losing them.
raise click.UsageError(f"Frontmatter of {note_name} is not valid YAML; fix it first:\n{error}")
if not isinstance(existing, dict):
raise click.UsageError(f"Frontmatter of {note_name} is not a key: value mapping; fix it first")
value = existing.get(key)
current = value if isinstance(value, list) else ([] if value in (None, '') else [value])
if any(isinstance(item, (list, dict)) for item in current):
raise click.UsageError(f"'{key}' in {note_name} has nested values (unquoted [[link]]?); "
"fix it by hand: Obsidian needs \"[[link]]\" quoted")
current = [item if isinstance(item, str) else str(item) for item in current if item is not None]
if remove:
dropped = {self._list_item_id(item) for item in items}
new = [item for item in current if self._list_item_id(item) not in dropped]
else:
new, seen = list(current), {self._list_item_id(item) for item in current}
for item in (i.strip() for i in items):
if item and self._list_item_id(item) not in seen:
new.append(item)
seen.add(self._list_item_id(item))
changed = len(new) - len(current)
if not changed:
click.echo(f"No change to '{key}' in {note_name}")
return
self._rewrite_frontmatter(note_path, {key: new})
click.echo(f"{'Removed' if remove else 'Added'} {abs(changed)} item(s) "
f"{'from' if remove else 'to'} '{key}' in {note_name}")
def frontmatter_view(self, note_name: str, keys: tuple, value: str = None, vault: str = None, open_file: bool = False, output_path: Union[bool, str, None] = None):
vault_name, vault_path = self._get_vault_and_path(vault)
note_path = vault_path / note_name
if not note_path.suffix:
note_path = note_path.with_suffix('.md')
if not note_path.exists():
matches = list(vault_path.rglob(f"{note_name}.md"))
if matches:
note_path = matches[0]
if not note_path.exists():
raise click.UsageError(f"Note {note_name} does not exist")
note_path = self._find_note(vault_path, note_name)
content = note_path.read_text(encoding='utf-8-sig')
frontmatter, _, _ = self._parse_frontmatter(content)
@@ -630,7 +1046,7 @@ class OnyxCLI:
click.echo(val)
else:
click.echo(yaml.dump({k: frontmatter[k]}, default_flow_style=False).strip())
click.echo(yaml.dump({k: frontmatter[k]}, default_flow_style=False, allow_unicode=True).strip())
def _handle_open_file(self, current_value, vault):
val_str = str(current_value)
@@ -650,68 +1066,15 @@ class OnyxCLI:
def frontmatter_clear(self, note_name: str, keys: tuple, vault: str = None):
vault_name, vault_path = self._get_vault_and_path(vault)
note_path = vault_path / note_name
if not note_path.suffix:
note_path = note_path.with_suffix('.md')
if not note_path.exists():
matches = list(vault_path.rglob(f"{note_name}.md"))
if matches:
note_path = matches[0]
if not note_path.exists():
raise click.UsageError(f"Note {note_name} does not exist")
all_keys = []
for key in keys:
all_keys.extend([k.strip() for k in re.split(r',|\[|\]', key) if k.strip()])
updates = {}
for k in all_keys:
updates[k] = [] if k == 'tags' else ""
self._update_frontmatter(note_path, updates)
note_path = self._find_note(vault_path, note_name)
updates = {k: [] if k == 'tags' else "" for k in self._split_keys(keys)}
self._rewrite_frontmatter(note_path, updates)
click.echo(f"Cleared frontmatter values for {note_name}")
def frontmatter_delete(self, note_name: str, keys: tuple, vault: str = None):
vault_name, vault_path = self._get_vault_and_path(vault)
note_path = vault_path / note_name
if not note_path.suffix:
note_path = note_path.with_suffix('.md')
if not note_path.exists():
matches = list(vault_path.rglob(f"{note_name}.md"))
if matches:
note_path = matches[0]
if not note_path.exists():
raise click.UsageError(f"Note {note_name} does not exist")
content = note_path.read_text(encoding='utf-8-sig')
frontmatter, rest_content, _ = self._parse_frontmatter(content)
all_keys = []
for key in keys:
all_keys.extend([k.strip() for k in re.split(r',|\[|\]', key) if k.strip()])
modified = False
for k in all_keys:
if k in frontmatter:
del frontmatter[k]
modified = True
if modified:
if frontmatter:
new_content = '---\n'
new_content += yaml.dump(frontmatter, default_flow_style=False)
new_content += '---\n'
new_content += rest_content
else:
new_content = rest_content
note_path.write_text(new_content, encoding='utf-8-sig')
note_path = self._find_note(vault_path, note_name)
if self._rewrite_frontmatter(note_path, {}, set(self._split_keys(keys))):
click.echo(f"Deleted frontmatter keys from {note_name}")
else:
click.echo("No matching keys found in frontmatter")
@@ -837,7 +1200,7 @@ class OnyxCLI:
vaults[vault_name] = vault_entry
config['vaults'] = vaults
self.config_file.write_text(yaml.dump(config, default_flow_style=False), encoding='utf-8-sig')
_write_text(self.config_file, yaml.dump(config, default_flow_style=False))
click.echo(f"Updated {note_type} note settings for vault '{vault_name}'")
def print_periodic_config(self, note_type: str, vault: str = None):
@@ -851,7 +1214,15 @@ class OnyxCLI:
# CLI Commands
@click.version_option(version='0.1.0', prog_name='onyx')
def _periodic_output_options(command):
"""--no-open / --print-path, shared by the periodic note commands."""
command = click.option('--print-path', is_flag=True,
help="Print only the note's absolute path (for scripts)")(command)
return click.option('--no-open', is_flag=True,
help="Don't open Obsidian; a newly created note keeps raw Templater syntax "
"until Obsidian processes it")(command)
@click.version_option(version='0.4.2', prog_name='onyx')
@click.group(name='onyx')
def cli():
"""Onyx - Interact with Obsidian vaults from the command line
@@ -908,57 +1279,62 @@ def open(note_name, vault):
@click.option('--date', help="Target date (YYYY-MM-DD) for the note")
@click.option('--next', 'next_', is_flag=True, help="Open the next period's note")
@click.option('--previous', is_flag=True, help="Open the previous period's note")
def daily(vault, date, next_, previous):
@_periodic_output_options
def daily(vault, date, next_, previous, no_open, print_path):
"""Create or open a daily note for a specific date, next, or previous day
Folder, filename format, and template are resolved from onyx's own config
(see `onyx periodic --type daily`), falling back to Obsidian's Daily Notes
or Periodic Notes plugin settings, then to built-in defaults."""
OnyxCLI().daily_note(vault, date=date, next_=next_, previous=previous)
OnyxCLI().daily_note(vault, date, next_, previous, not no_open, print_path)
@cli.command()
@click.option('--vault', help="Vault name (if not using default)")
@click.option('--date', help="Target date (YYYY-MM-DD) for the note")
@click.option('--next', 'next_', is_flag=True, help="Open the next period's note")
@click.option('--previous', is_flag=True, help="Open the previous period's note")
def weekly(vault, date, next_, previous):
@_periodic_output_options
def weekly(vault, date, next_, previous, no_open, print_path):
"""Create or open a weekly note for a specific date, next, or previous week
Folder, filename format, and template are configurable via `onyx periodic --type weekly`."""
OnyxCLI().weekly_note(vault, date=date, next_=next_, previous=previous)
OnyxCLI().weekly_note(vault, date, next_, previous, not no_open, print_path)
@cli.command()
@click.option('--vault', help="Vault name (if not using default)")
@click.option('--date', help="Target date (YYYY-MM-DD) for the note")
@click.option('--next', 'next_', is_flag=True, help="Open the next period's note")
@click.option('--previous', is_flag=True, help="Open the previous period's note")
def monthly(vault, date, next_, previous):
@_periodic_output_options
def monthly(vault, date, next_, previous, no_open, print_path):
"""Create or open a monthly note for a specific date, next, or previous month
Folder, filename format, and template are configurable via `onyx periodic --type monthly`."""
OnyxCLI().monthly_note(vault, date=date, next_=next_, previous=previous)
OnyxCLI().monthly_note(vault, date, next_, previous, not no_open, print_path)
@cli.command()
@click.option('--vault', help="Vault name (if not using default)")
@click.option('--date', help="Target date (YYYY-MM-DD) for the note")
@click.option('--next', 'next_', is_flag=True, help="Open the next period's note")
@click.option('--previous', is_flag=True, help="Open the previous period's note")
def quarterly(vault, date, next_, previous):
@_periodic_output_options
def quarterly(vault, date, next_, previous, no_open, print_path):
"""Create or open a quarterly note for a specific date, next, or previous quarter
Folder, filename format, and template are configurable via `onyx periodic --type quarterly`."""
OnyxCLI().quarterly_note(vault, date=date, next_=next_, previous=previous)
OnyxCLI().quarterly_note(vault, date, next_, previous, not no_open, print_path)
@cli.command()
@click.option('--vault', help="Vault name (if not using default)")
@click.option('--date', help="Target date (YYYY-MM-DD) for the note")
@click.option('--next', 'next_', is_flag=True, help="Open the next period's note")
@click.option('--previous', is_flag=True, help="Open the previous period's note")
def yearly(vault, date, next_, previous):
@_periodic_output_options
def yearly(vault, date, next_, previous, no_open, print_path):
"""Create or open a yearly note for a specific date, next, or previous year
Folder, filename format, and template are configurable via `onyx periodic --type yearly`."""
OnyxCLI().yearly_note(vault, date=date, next_=next_, previous=previous)
OnyxCLI().yearly_note(vault, date, next_, previous, not no_open, print_path)
@cli.command()
@click.option('--type', 'note_type', required=True,
@@ -1014,6 +1390,95 @@ def create(note_name, content, vault, overwrite, append, open_after, template):
content = content.replace('\\n', '\n')
OnyxCLI().create_note(note_name, content, vault, overwrite, append, open_after, template)
@cli.command()
@click.argument('note_name')
@click.option('--heading', required=True,
help="Section title: '## Journal' (that level) or 'Journal' (any level); case-insensitive")
@click.option('--content', required=True, help="Text to insert; '-' reads stdin; literal \\n becomes a newline")
@click.option('--replace', is_flag=True, help="Replace the section's body instead of appending to it")
@click.option('--create', is_flag=True, help="Add the section at the end of the note if it is missing")
@click.option('--vault', help="Vault name (if not using default)")
def section(note_name, heading, content, replace, create, vault):
"""Append to, or replace, one section of a note
Appends at the end of the section (before the next heading of the same or a
higher level), after a blank line, except between two list items. Headings in
code blocks are ignored. Everything outside the section stays byte-for-byte.
USAGE:
onyx section "periodic/daily/2026-10-19" --heading "## Journal" --content "### 10:00 - Titre\\n- note"
onyx section "periodic/daily/2026-10-19" --heading "### Matin" --content "- [ ] Appeler Haro"
onyx section "periodic/weekly/2026-W43" --heading "## Weekly review" --replace --content - < review.md
"""
content = sys.stdin.read() if content == '-' else content.replace('\\n', '\n')
OnyxCLI().write_section(note_name, heading, content, replace, create, vault)
@cli.command()
@click.argument('name')
@click.option('--folder', help="Only notes under this vault-relative folder (e.g. entities)")
@click.option('--link', is_flag=True, help="Print a ready-to-paste wikilink instead of the path")
@click.option('--vault', help="Vault name (if not using default)")
def resolve(name, folder, link, vault):
"""Find notes by filename or alias
Exact match on the filename or any `aliases` entry, ignoring case and accents
(no fuzzy matching). Prints one vault-relative path per match; exits 1 when
nothing matches. --link prints [[Note]] or [[Note|Alias]] (a path link when the
filename is shared by several notes).
USAGE:
onyx resolve "Stéphane" --folder entities --link
onyx resolve "METC"
"""
if not OnyxCLI().resolve_note(name, folder, link, vault):
click.echo(f"No note named or aliased '{name}'", err=True)
sys.exit(1)
@cli.command(name='list')
@click.option('--folder', help="Only notes under this vault-relative folder")
@click.option('--where', multiple=True,
help="Frontmatter filter, repeatable (AND): key=value, key!=value, key, !key")
@click.option('--json', 'as_json', is_flag=True, help="Print paths and frontmatter as JSON")
@click.option('--vault', help="Vault name (if not using default)")
def list_command(folder, where, as_json, vault):
"""List notes, filtered by frontmatter
Values compare without case or accents; a [[link]] compares by its target, and a
list matches when any item does. Prints vault-relative paths, or JSON with --json.
USAGE:
onyx list --folder projects --where type=project --where status=active
onyx list --where client=Symbolitech --where "status!=done" --json
"""
OnyxCLI().list_notes(folder, where, as_json, vault)
@cli.command()
@click.option('--folder', help="Only notes under this vault-relative folder (e.g. periodic/daily)")
@click.option('--from', 'date_from', help="First note date (YYYY-MM-DD), inclusive")
@click.option('--to', 'date_to', help="Last note date (YYYY-MM-DD), inclusive")
@click.option('--status', help="Comma-separated: open, doing, done, cancelled (default: all)")
@click.option('--ignore-from', help="Skip tasks whose text also appears in this note (e.g. the daily template)")
@click.option('--json', 'as_json', is_flag=True, help="JSON with path, line, date, status, heading, text")
@click.option('--vault', help="Vault name (if not using default)")
def tasks(folder, date_from, date_to, status, ignore_from, as_json, vault):
"""List checkbox tasks: - [ ] open, - [/] doing, - [x] done, - [-] cancelled
With --from/--to, a note's date is the YYYY-MM-DD at the start of its filename,
else its `date` property; undated notes are skipped. Tasks in code blocks are ignored.
USAGE:
onyx tasks --folder periodic/daily --from 2026-10-12 --to 2026-10-18 --status open \\
--ignore-from resources/template/daily-note
"""
for value in (date_from, date_to):
if value and not re.fullmatch(r'\d{4}-\d{2}-\d{2}', value):
raise click.UsageError(f"Invalid date '{value}': use YYYY-MM-DD")
statuses = {s.strip() for s in status.split(',')} if status else None
unknown = (statuses or set()) - set(_TASK_STATUS.values())
if unknown:
raise click.UsageError(f"Unknown status {', '.join(sorted(unknown))}: use open, doing, done, cancelled")
OnyxCLI().list_tasks(folder, date_from, date_to, statuses, ignore_from, as_json, vault)
@cli.command()
@click.argument('note_name')
@click.option('--vault', help="Vault name (if not using default)")
@@ -1029,7 +1494,9 @@ def print_note(note_name, vault):
def move(current_path, new_path, vault, open_after):
"""Move or rename a note and auto-update internal links
Automatically updates all [[links]] pointing to this note in other vault files.
Rewrites wikilinks that resolve to this note anywhere in the vault: [[name]],
[[name|alias]], [[name#heading]], embeds, path links and frontmatter links.
Refuses to overwrite an existing note.
USAGE:
onyx move "old-name" "new-name"
@@ -1050,24 +1517,33 @@ def delete(note_path, vault):
@click.option('--view', is_flag=True, help="View frontmatter")
@click.option('--clear', is_flag=True, help="Clear frontmatter values")
@click.option('--delete', is_flag=True, help="Delete frontmatter keys")
@click.option('--add', is_flag=True, help="Add each --value as an item of the list --key (no duplicates)")
@click.option('--remove', is_flag=True, help="Remove each --value from the list --key")
@click.option('--key', required=True, multiple=True, help="Frontmatter key to manage")
@click.option('--value', multiple=True, help="Value to set or check for")
@click.option('--open', 'open_file', is_flag=True, help="Open the file referenced in the value")
@click.option('--vault', help="Vault name (if not using default)")
@click.option('--output-path', is_flag=False, flag_value=True, default=None, help="Resolve relative paths. Can be a flag (uses config prefix) or a string (custom prefix).")
def frontmatter_command(note_name, edit, view, clear, delete, key, value, open_file, vault, output_path):
def frontmatter_command(note_name, edit, view, clear, delete, add, remove, key, value, open_file, vault, output_path):
"""Manage YAML frontmatter in notes
Must specify exactly one action: --edit, --view, --clear, or --delete
Must specify exactly one action: --edit, --view, --clear, --delete, --add or --remove
--edit replaces a value; `tags` and keys already holding a list take a
comma-separated list. --add/--remove change single items of a list property;
repeat --value for several items. Items match by wikilink target, so
"[[Name|Alias]]" and "[[name]]" are the same item.
USAGE:
onyx frontmatter "my-note" --view --key "tags"
onyx frontmatter "my-note" --edit --key "tags" --value "project,work"
onyx frontmatter "my-note" --add --key participants --value "[[Haro]]" --value "[[Jeffrey]]"
onyx frontmatter "my-note" --remove --key participants --value "[[Haro]]"
onyx frontmatter "my-note" --delete --key "date"
"""
cli = OnyxCLI()
if sum([edit, view, clear, delete]) != 1:
raise click.UsageError("Exactly one of --edit, --view, --clear, or --delete must be specified")
if sum([edit, view, clear, delete, add, remove]) != 1:
raise click.UsageError("Exactly one of --edit, --view, --clear, --delete, --add or --remove must be specified")
if edit:
if value and len(value) > 1 and len(key) != len(value):
@@ -1085,6 +1561,16 @@ def frontmatter_command(note_name, edit, view, clear, delete, key, value, open_f
cli.frontmatter_clear(note_name, key, vault)
elif delete:
cli.frontmatter_delete(note_name, key, vault)
else:
if len(key) != 1 or not value:
raise click.UsageError("--add/--remove need exactly one --key and at least one --value")
cli.frontmatter_list_items(note_name, key[0], value, remove=remove, vault=vault)
if __name__ == '__main__':
# Scripts talk to onyx through pipes (`--content -`, `--print-path`): UTF-8 with LF on
# every OS. Without this, Windows pipes use the ANSI code page (é -> 0xE9 out, UTF-8 in
# read as "é"; PyInstaller ignores PYTHONUTF8) and CRLF, whose \r survives bash's $(...).
sys.stdin.reconfigure(encoding='utf-8')
for stream in (sys.stdout, sys.stderr):
stream.reconfigure(encoding='utf-8', newline='\n')
cli()
+318 -65
View File
@@ -1,4 +1,7 @@
import json
import os
import subprocess
import sys
import unittest
import yaml
import shutil
@@ -9,6 +12,8 @@ import click
from click.testing import CliRunner
from datetime import datetime
SRC = Path(__file__).resolve().parent.parent / 'src' / 'onyx.py'
class TestOnyxCLI(unittest.TestCase):
def setUp(self):
# Create temporary test directory and files
@@ -35,75 +40,12 @@ class TestOnyxCLI(unittest.TestCase):
shutil.rmtree(self.test_dir, ignore_errors=True)
shutil.rmtree(self.temp_config_dir, ignore_errors=True)
def test_set_default_vault(self):
"""Test setting default vault"""
self.cli.set_default_vault('new_vault', str(self.test_dir))
self.assertEqual(self.cli.default_vault, 'new_vault')
config = yaml.safe_load(self.cli.config_file.read_text(encoding='utf-8-sig'))
self.assertEqual(config['default_vault'], 'new_vault')
self.assertEqual(Path(config['vaults']['new_vault']), self.test_dir.resolve())
def test_create_note(self):
"""Test note creation"""
# Test basic note creation
self.cli.create_note('test.md', 'Test content')
note_path = self.test_dir / 'test.md'
self.assertTrue(note_path.exists())
@unittest.skip("Method export_to_csv not implemented")
def test_export_to_csv(self):
"""Test exporting note frontmatter to CSV"""
# Create a test note with frontmatter
note_content = """---
title: Test Note
tags: [tag1, tag2]
status: in-progress
date: 2025-10-28
custom: value
---
Note content"""
self.cli.create_note('test_note.md', note_content)
# Test export with specific columns included
output_file = self.test_dir / 'output1.csv'
self.cli.export_to_csv('test_note', str(output_file), include_columns=['title', 'status', 'date'])
with open(output_file, 'r', encoding='utf-8') as f:
content = f.read().strip()
self.assertEqual(content, 'title,status,date\nTest Note,in-progress,2025-10-28')
# Test export excluding specific columns
output_file = self.test_dir / 'output2.csv'
self.cli.export_to_csv('test_note', str(output_file), exclude_columns=['tags', 'custom'])
with open(output_file, 'r', encoding='utf-8') as f:
content = f.read().strip()
self.assertEqual(content, 'title,status,date\nTest Note,in-progress,2025-10-28')
# Test export with both include and exclude (exclude takes precedence)
output_file = self.test_dir / 'output3.csv'
self.cli.export_to_csv('test_note', str(output_file),
include_columns=['title', 'status', 'date', 'custom'],
exclude_columns=['status'])
with open(output_file, 'r', encoding='utf-8') as f:
content = f.read().strip()
self.assertEqual(content, 'title,date,custom\nTest Note,2025-10-28,value')
# Test with non-existent note
with self.assertRaises(click.UsageError) as context:
self.cli.export_to_csv('non_existent', 'output.csv')
self.assertTrue("not found in vault" in str(context.exception))
# Test with note without frontmatter
self.cli.create_note('no_frontmatter.md', 'Just content')
with self.assertRaises(click.UsageError) as context:
self.cli.export_to_csv('no_frontmatter', 'output.csv')
self.assertTrue("No frontmatter found" in str(context.exception))
# Test with empty frontmatter
self.cli.create_note('empty_frontmatter.md', '---\n---\nContent')
with self.assertRaises(click.UsageError) as context:
self.cli.export_to_csv('empty_frontmatter', 'output.csv')
self.assertTrue("Empty frontmatter" in str(context.exception))
self.assertEqual(note_path.read_text(encoding='utf-8-sig'), 'Test content')
# Test note creation with auto-extension
@@ -123,6 +65,21 @@ Note content"""
'New content\nAdditional content'
)
def test_writes_keep_lf_without_bom(self):
"""Append and frontmatter edits write LF, no BOM, even on Windows"""
note_path = self.test_dir / 'lf.md'
note_path.write_bytes(b'---\nstatus: draft\n---\n# Journal\n')
self.cli.create_note('lf.md', '### 10:00 - Entry\nline', append=True)
self.cli.frontmatter_edit('lf', ('status',), ('done',))
data = note_path.read_bytes()
self.assertNotIn(b'\r', data)
self.assertFalse(data.startswith(b'\xef\xbb\xbf'))
self.assertIn(b'status: done', data)
self.assertTrue(data.endswith(b'# Journal\n\n### 10:00 - Entry\nline'))
def test_frontmatter_operations(self):
"""Test frontmatter operations"""
# Create a test note with frontmatter
@@ -172,8 +129,8 @@ Test content"""
self.cli.frontmatter_clear(test_note, ('status,title',))
content = (self.test_dir / test_note).read_text(encoding='utf-8-sig')
frontmatter = yaml.safe_load(content.split('---')[1])
self.assertEqual(frontmatter['status'], '')
self.assertEqual(frontmatter['title'], '')
self.assertIsNone(frontmatter['status'])
self.assertIsNone(frontmatter['title'])
# Test frontmatter --delete
self.cli.frontmatter_delete(test_note, ('tags',))
@@ -204,6 +161,287 @@ Test content"""
link_content = (self.test_dir / 'link.md').read_text(encoding='utf-8-sig')
self.assertIn('[[dest]]', link_content)
def test_frontmatter_edit_only_touches_target_keys(self):
"""Editing keys keeps order, quoting, accents and long lines of the rest"""
note = self.test_dir / 'meeting.md'
note.write_text(
'---\n'
'client: "[[Symbolitech]]"\n'
'participants:\n'
' - "[[Stéphane Charbonneau|Stéphane]]"\n'
'type: meeting\n'
'summary: "Une longue phrase de résumé qui dépasse largement quatre-vingts caractères sans être repliée."\n'
'date: 2026-10-19\n'
'---\n'
'# Réunion\n', encoding='utf-8')
self.cli.frontmatter_edit('meeting', ('type', 'status', 'parent', 'reviewed'),
('réunion', 'done', '[[Haro]]', '2026-10-20'))
self.cli.frontmatter_delete('meeting', ('client',))
self.assertEqual(note.read_text(encoding='utf-8'),
'---\n'
'participants:\n'
' - "[[Stéphane Charbonneau|Stéphane]]"\n'
'type: réunion\n'
'summary: "Une longue phrase de résumé qui dépasse largement quatre-vingts caractères sans être repliée."\n'
'date: 2026-10-19\n'
'status: done\n'
'parent: "[[Haro]]"\n'
'reviewed: 2026-10-20\n'
'---\n'
'# Réunion\n')
def test_frontmatter_edit_adds_frontmatter_to_plain_note(self):
note = self.test_dir / 'plain.md'
note.write_text('Corps\n---\nPas un frontmatter\n', encoding='utf-8')
self.cli.frontmatter_edit('plain', ('tags',), ('a, b',))
self.assertEqual(note.read_text(encoding='utf-8'),
'---\ntags:\n - a\n - b\n---\nCorps\n---\nPas un frontmatter\n')
MEETING = (
'---\n'
'type: meeting\n'
'participants:\n'
' - "[[Stéphane Charbonneau|Stéphane]]"\n'
' - "[[Haro]]"\n'
'summary: "Résumé : décisions."\n'
'---\n'
'Corps\n')
def test_frontmatter_edit_keeps_existing_list_type(self):
"""--edit on a key that is already a list writes a list, not one quoted string"""
note = self.test_dir / 'm.md'
note.write_text(self.MEETING, encoding='utf-8')
self.cli.frontmatter_edit('m', ('participants', 'type'), ('[[Jeffrey]], [[Haro]]', 'réunion, interne'))
self.assertEqual(note.read_text(encoding='utf-8'), self.MEETING
.replace(' - "[[Stéphane Charbonneau|Stéphane]]"\n - "[[Haro]]"\n',
' - "[[Jeffrey]]"\n - "[[Haro]]"\n')
.replace('type: meeting', 'type: réunion, interne'))
def test_frontmatter_add_and_remove_list_items(self):
"""Items match by link target (alias/case ignored); other items and keys untouched"""
note = self.test_dir / 'm.md'
note.write_text(self.MEETING, encoding='utf-8')
self.cli.frontmatter_list_items('m', 'participants', ('[[stéphane charbonneau]]', '[[Jeffrey]]', '[[Jeffrey|Jeff]]'))
self.cli.frontmatter_list_items('m', 'participants', ('[[Haro|H]]',), remove=True)
self.cli.frontmatter_list_items('m', 'mentioned', ('[[Livre, tome 2]]',))
self.cli.frontmatter_list_items('m', 'type', ('log',))
self.assertEqual(note.read_text(encoding='utf-8'),
'---\n'
'type:\n'
' - meeting\n'
' - log\n'
'participants:\n'
' - "[[Stéphane Charbonneau|Stéphane]]"\n'
' - "[[Jeffrey]]"\n'
'summary: "Résumé : décisions."\n'
'mentioned:\n'
' - "[[Livre, tome 2]]"\n'
'---\n'
'Corps\n')
with patch('click.echo') as echo:
self.cli.frontmatter_list_items('m', 'participants', ('[[Jeffrey|J]]',))
self.cli.frontmatter_list_items('m', 'absent', ('x',), remove=True)
self.assertEqual([c.args[0] for c in echo.call_args_list],
["No change to 'participants' in m", "No change to 'absent' in m"])
def test_frontmatter_add_refuses_invalid_yaml(self):
"""Unparseable frontmatter must not look like 'key missing' and lose existing items"""
note = self.test_dir / 'person.md'
content = '---\nchildren: "[[Julia]]", "[[Jacob]]"\ntype: person\n---\nCorps\n'
note.write_text(content, encoding='utf-8')
for remove in (False, True):
with self.assertRaises(click.UsageError):
self.cli.frontmatter_list_items('person', 'children', ('[[Nouveau]]',), remove=remove)
self.assertEqual(note.read_text(encoding='utf-8'), content)
def test_frontmatter_add_refuses_unquoted_links(self):
note = self.test_dir / 'bad.md'
content = '---\nrelated:\n - [[Haro]]\n---\n'
note.write_text(content, encoding='utf-8')
with self.assertRaises(click.UsageError):
self.cli.frontmatter_list_items('bad', 'related', ('[[Plane]]',))
self.assertEqual(note.read_text(encoding='utf-8'), content)
DAILY = (
'---\n'
'type: daily-note\n'
'# Journal: not a heading inside frontmatter\n'
'---\n'
'\n'
'# 2026-10-29\n'
'\n'
'## To do\n'
'### Matin\n'
'- [ ] Regarder ma Weekly note\n'
'\n'
'### Après-midi\n'
'\n'
'### Soir\n'
'- [ ] Prendre calcium\n'
'\n'
'```markdown\n'
'## Journal\n'
'```\n'
'\n'
'## Journal\n'
'Aujourd’hui, Mark présente Align.')
def _daily(self):
note = self.test_dir / 'd.md'
note.write_text(self.DAILY, encoding='utf-8')
return note
def test_section_append(self):
"""Blank line before new blocks, none between list items or after an empty heading;
headings in frontmatter or code fences are ignored; nothing else moves"""
note = self._daily()
self.cli.write_section('d', '## Journal', '### 10:00 - Ménage\n- fait')
self.cli.write_section('d', 'matin', '- [ ] Appeler Haro')
self.cli.write_section('d', '### Après-midi', '- [ ] Courses\n')
self.cli.write_section('d', '## Journal', '### 11:00')
self.assertEqual(note.read_text(encoding='utf-8'), self.DAILY
.replace('- [ ] Regarder ma Weekly note\n', '- [ ] Regarder ma Weekly note\n- [ ] Appeler Haro\n')
.replace('### Après-midi\n', '### Après-midi\n- [ ] Courses\n')
.replace('Align.', 'Align.\n\n### 10:00 - Ménage\n- fait\n\n### 11:00\n'))
def test_section_replace_keeps_neighbours(self):
"""Replacing '## To do' drops its subsections and the code block inside it, but stops
at '## Journal' (the fake one in the fence doesn't end the section)"""
note = self._daily()
self.cli.write_section('d', '## To do', '- [ ] Une seule tâche', replace=True)
self.cli.write_section('d', '## Journal', '', replace=True)
self.assertEqual(note.read_text(encoding='utf-8'),
'---\ntype: daily-note\n# Journal: not a heading inside frontmatter\n---\n\n# 2026-10-29\n\n'
'## To do\n- [ ] Une seule tâche\n\n## Journal\n')
def test_section_missing_heading(self):
note = self._daily()
with self.assertRaises(click.UsageError):
self.cli.write_section('d', '## Review', 'x')
self.assertEqual(note.read_text(encoding='utf-8'), self.DAILY)
self.cli.write_section('d', 'Review', 'Bilan', create=True)
self.assertEqual(note.read_text(encoding='utf-8'), self.DAILY + '\n\n## Review\nBilan\n')
def _write_notes(self, notes: dict):
for rel, text in notes.items():
path = self.test_dir / rel
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding='utf-8')
def _echoed(self, call, *args, **kwargs):
with patch('click.echo') as echo:
result = call(*args, **kwargs)
return [c.args[0] for c in echo.call_args_list], result
def test_resolve_by_name_or_alias(self):
"""Case/accent-insensitive exact match; alias gives [[Note|Alias]]; shared filename
gives a path link; no fuzzy match"""
self._write_notes({
'entities/people/Stéphane Charbonneau.md': '---\naliases:\n - Stéphane\n---\n',
'entities/people/Stéphane Bigham.md': '---\naliases: [stephane]\n---\n',
'entities/businesses/Securitech.md': '---\naliases:\n - Sécuritech\n---\n',
'entities/businesses/Haro.md': '',
'archives/Haro.md': '',
'.trash/Securitech.md': '',
})
self.assertEqual(self._echoed(self.cli.resolve_note, 'STEPHANE', link=True),
(['[[Stéphane Bigham|stephane]]', '[[Stéphane Charbonneau|Stéphane]]'], 2))
self.assertEqual(self._echoed(self.cli.resolve_note, 'sécuritech'),
(['entities/businesses/Securitech'], 1))
self.assertEqual(self._echoed(self.cli.resolve_note, 'haro', folder='entities', link=True),
(['[[entities/businesses/Haro]]'], 1))
self.assertEqual(self._echoed(self.cli.resolve_note, 'Charbonneau'), ([], 0))
def test_list_where(self):
self._write_notes({
'projects/A.md': '---\ntype: project\nstatus: active\nclient: "[[Symbolitech]]"\n---\n',
'projects/B.md': '---\ntype: project\nstatus: done\nclient: "[[Diakonos]]"\n---\n',
'projects/C.md': '---\ntype: project\ntags:\n - Écriture\n---\n',
'notes/D.md': '---\ntype: project\nstatus: active\n---\n',
})
list_notes = self.cli.list_notes
self.assertEqual(self._echoed(list_notes, 'projects', ('status=ACTIVE', 'client=symbolitech'))[0],
['projects/A'])
self.assertEqual(self._echoed(list_notes, 'projects', ('status!=done', 'type'))[0],
['projects/A', 'projects/C'])
self.assertEqual(self._echoed(list_notes, None, ('!status',))[0], ['projects/C'])
self.assertEqual(self._echoed(list_notes, 'projects', ('tags=ecriture',))[0], ['projects/C'])
def test_tasks_dates_statuses_and_template(self):
self._write_notes({
'template.md': '- [ ] Prendre calcium\n',
'daily/2026-10-12.md': '## To do\n### Matin\n- [ ] Appeler Haro\n- [x] Fait\n- [/] En cours\n'
'```\n- [ ] dans du code\n```\n## Soir\n - [ ] prendre CALCIUM\n- [-] Annulé\n',
'daily/2026-10-20.md': '- [ ] Trop tard\n',
'daily/Sans date.md': '---\ndate: 2026-10-13\n---\n- [ ] Daté par la propriété\n',
})
lines, _ = self._echoed(self.cli.list_tasks, 'daily', '2026-10-12', '2026-10-18',
{'open', 'doing'}, 'template')
self.assertEqual(lines, ['daily/2026-10-12.md:3: - [ ] Appeler Haro',
'daily/2026-10-12.md:5: - [/] En cours',
'daily/Sans date.md:4: - [ ] Daté par la propriété'])
output, _ = self._echoed(self.cli.list_tasks, 'daily', statuses={'cancelled'}, as_json=True)
self.assertEqual(json.loads(output[0]), [{'path': 'daily/2026-10-12.md', 'line': 11, 'date': '2026-10-12',
'status': 'cancelled', 'heading': 'Soir', 'text': 'Annulé'}])
def test_move_rewrites_every_link_form(self):
"""Aliases, headings, embeds, path links, frontmatter links, regex characters"""
(self.test_dir / 'films').mkdir()
(self.test_dir / 'films' / 'Dune (film).md').write_text('Self: [[Dune (film)#Notes]]\n', encoding='utf-8')
linker = self.test_dir / 'index.md'
linker.write_text(
'---\nrelated: "[[Dune (film)|Dune]]"\n---\n'
'[[Dune (film)]] [[dune (film)#Cast|vu]] ![[Dune (film)#^b1]] '
'[[films/Dune (film).md]] [[Dune]] [[Other (film)]]\n', encoding='utf-8')
with patch('click.echo') as echo:
self.cli.move_note('films/Dune (film)', 'archives/Dune 2021')
echo.assert_any_call('Moved films/Dune (film) to archives/Dune 2021 (6 link(s) updated in 2 note(s))')
self.assertEqual(linker.read_text(encoding='utf-8'),
'---\nrelated: "[[Dune 2021|Dune]]"\n---\n'
'[[Dune 2021]] [[Dune 2021#Cast|vu]] ![[Dune 2021#^b1]] '
'[[archives/Dune 2021.md]] [[Dune]] [[Other (film)]]\n')
self.assertEqual((self.test_dir / 'archives' / 'Dune 2021.md').read_text(encoding='utf-8'),
'Self: [[Dune 2021#Notes]]\n')
def test_move_ambiguous_names(self):
"""Bare links to a non-unique old name stay; a non-unique new name gets a path link"""
for rel in ('a/Note.md', 'b/Note.md', 'c/Target.md', 'd/Old.md'):
(self.test_dir / rel).parent.mkdir(exist_ok=True)
(self.test_dir / rel).write_text('', encoding='utf-8')
linker = self.test_dir / 'links.md'
linker.write_text('[[Note]] [[a/Note]] [[Old]]\n', encoding='utf-8')
self.cli.move_note('a/Note', 'e/Renamed')
self.cli.move_note('d/Old', 'f/Target')
self.assertEqual(linker.read_text(encoding='utf-8'), '[[Note]] [[e/Renamed]] [[f/Target]]\n')
def test_move_refuses_to_overwrite(self):
self.cli.create_note('one.md', '1')
self.cli.create_note('two.md', '2')
with self.assertRaises(click.UsageError):
self.cli.move_note('one', 'two')
self.assertEqual((self.test_dir / 'two.md').read_text(encoding='utf-8'), '2')
self.assertTrue((self.test_dir / 'one.md').exists())
def test_periodic_no_open_print_path(self):
"""--no-open creates without opening Obsidian; --print-path prints only the path"""
expected = self.test_dir.resolve() / '2025-10-24.md'
with patch.object(OnyxCLI, 'open_note') as open_note, patch('click.echo') as echo:
self.cli.daily_note(date='2025-10-24', open_after=False, print_path=True)
self.cli.daily_note(date='2025-10-24', open_after=False, print_path=True)
open_note.assert_not_called()
self.assertEqual([c.args for c in echo.call_args_list], [(str(expected),), (str(expected),)])
self.assertTrue(expected.exists())
def test_daily_note(self):
"""Test daily note creation"""
# Mock datetime.now() to return a specific date
@@ -370,5 +608,20 @@ Test content"""
self.assertIn(str(self.test_dir.resolve()), printed)
self.assertNotIn('path_prefix', printed)
def test_pipes_are_utf8_without_utf8_mode(self):
"""stdin/stdout are UTF-8 even when Python isn't in UTF-8 mode (frozen exe on Windows)"""
note = self.test_dir / 'd.md'
note.write_text('## Journal\n', encoding='utf-8')
env = {k: v for k, v in os.environ.items() if k not in ('PYTHONUTF8', 'PYTHONIOENCODING')}
home = str(self.temp_config_dir.resolve())
env.update(PYTHONUTF8='0', USERPROFILE=home, HOME=home)
result = subprocess.run([sys.executable, str(SRC), 'section', 'd', '--heading', 'Journal',
'--content', '-'], input='Réunion « test »'.encode('utf-8'),
capture_output=True, env=env)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(note.read_text(encoding='utf-8'), '## Journal\nRéunion « test »\n')
printed = subprocess.run([sys.executable, str(SRC), 'print-note', 'd'], capture_output=True, env=env)
self.assertEqual(printed.stdout, '## Journal\nRéunion « test »\n\n'.encode('utf-8'))
if __name__ == '__main__':
unittest.main()