pykernel-cli 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pykernel/__init__.py ADDED
File without changes
pykernel/__main__.py ADDED
@@ -0,0 +1,2 @@
1
+ from pykernel.kernel import main
2
+ main()
File without changes
@@ -0,0 +1,144 @@
1
+ """
2
+ Built-in commands — always available.
3
+
4
+ /help [command] — list all commands or describe one
5
+ /exit | /quit — exit the kernel
6
+ /clear — clear the screen
7
+ /history [n] — show last n inputs (default 20)
8
+ /run <file.py> — execute a Python file inside the kernel
9
+ /reset — clear the execution namespace
10
+ /alias <new> <target> — create an alias for an existing command
11
+ /commands — list all commands (alias for /help)
12
+ """
13
+
14
+ import os, pathlib, traceback
15
+ from pykernel.core.registry import CommandRegistry
16
+
17
+
18
+ def register(reg: CommandRegistry):
19
+
20
+ # ── /help ─────────────────────────────────────────────────────────────────
21
+
22
+ @reg.command("help", aliases=["?", "commands"],
23
+ help="List commands or show help for a specific command",
24
+ usage="/help [command]",
25
+ category="Core")
26
+ def cmd_help(ctx, *args):
27
+ p = ctx.paint
28
+ if args:
29
+ name = args[0].lstrip("/")
30
+ cmd = ctx.registry.resolve(name)
31
+ if cmd is None:
32
+ ctx.print(p.error(f"No command: /{name}"))
33
+ return
34
+ ctx.print(p.header(f" /{cmd.name}"))
35
+ if cmd.aliases:
36
+ ctx.print(p.dim(f" Aliases : " + ", ".join(f"/{a}" for a in cmd.aliases)))
37
+ ctx.print(p.dim(f" Usage : {cmd.usage}"))
38
+ ctx.print(f" {cmd.help}")
39
+ return
40
+
41
+ by_cat = ctx.registry.by_category()
42
+ ctx.print()
43
+ for cat, cmds in sorted(by_cat.items()):
44
+ ctx.print(p.header(f" {cat}"))
45
+ rows = [(f"/{c.name}", ", ".join(f"/{a}" for a in c.aliases), c.help)
46
+ for c in cmds]
47
+ ctx.print(p.table(rows, headers=["Command", "Aliases", "Description"]))
48
+ ctx.print()
49
+ ctx.print(p.dim(" Tip: type /help <command> for detailed usage."))
50
+ ctx.print()
51
+
52
+ # ── /exit ─────────────────────────────────────────────────────────────────
53
+
54
+ @reg.command("exit", aliases=["quit", "q"],
55
+ help="Exit the kernel",
56
+ usage="/exit",
57
+ category="Core")
58
+ def cmd_exit(ctx, *args):
59
+ ctx.print(ctx.paint.dim(" Goodbye."))
60
+ raise SystemExit(0)
61
+
62
+ # ── /clear ────────────────────────────────────────────────────────────────
63
+
64
+ @reg.command("clear", aliases=["cls"],
65
+ help="Clear the terminal screen",
66
+ usage="/clear",
67
+ category="Core")
68
+ def cmd_clear(ctx, *args):
69
+ os.system("clear" if os.name != "nt" else "cls")
70
+
71
+ # ── /history ──────────────────────────────────────────────────────────────
72
+
73
+ @reg.command("history", aliases=["hist"],
74
+ help="Show recent input history",
75
+ usage="/history [n]",
76
+ category="Core")
77
+ def cmd_history(ctx, *args):
78
+ p = ctx.paint
79
+ n = int(args[0]) if args and args[0].isdigit() else 20
80
+ items = ctx.history[-(n):]
81
+ if not items:
82
+ ctx.print(p.dim(" (no history yet)"))
83
+ return
84
+ ctx.print()
85
+ for i, entry in enumerate(items, start=max(1, len(ctx.history) - n + 1)):
86
+ prefix = p.dim(f" {i:>4} │ ")
87
+ # truncate long entries
88
+ snippet = entry.replace("\n", "⏎ ")[:80]
89
+ ctx.print(prefix + snippet)
90
+ ctx.print()
91
+
92
+ # ── /run ──────────────────────────────────────────────────────────────────
93
+
94
+ @reg.command("run",
95
+ help="Execute a Python file in the kernel namespace",
96
+ usage="/run <path/to/file.py>",
97
+ category="Core")
98
+ def cmd_run(ctx, *args):
99
+ p = ctx.paint
100
+ if not args:
101
+ ctx.print(p.error("Usage: /run <path/to/file.py>"))
102
+ return
103
+ fpath = pathlib.Path(args[0]).expanduser()
104
+ if not fpath.exists():
105
+ ctx.print(p.error(f"File not found: {fpath}"))
106
+ return
107
+ try:
108
+ source = fpath.read_text()
109
+ exec(compile(source, str(fpath), "exec"), ctx.env)
110
+ ctx.print(p.success(f" ✓ Ran {fpath.name}"))
111
+ except Exception:
112
+ ctx.print(p.error(traceback.format_exc()))
113
+
114
+ # ── /reset ────────────────────────────────────────────────────────────────
115
+
116
+ @reg.command("reset",
117
+ help="Clear all variables from the kernel namespace",
118
+ usage="/reset",
119
+ category="Core")
120
+ def cmd_reset(ctx, *args):
121
+ kept = {"__name__", "__builtins__"}
122
+ for k in list(ctx.env.keys()):
123
+ if k not in kept:
124
+ del ctx.env[k]
125
+ ctx.print(ctx.paint.success(" ✓ Namespace cleared."))
126
+
127
+ # ── /alias ────────────────────────────────────────────────────────────────
128
+
129
+ @reg.command("alias",
130
+ help="Create an alias for an existing command (/alias hi greet)",
131
+ usage="/alias <new_name> <existing_command>",
132
+ category="Core")
133
+ def cmd_alias(ctx, *args):
134
+ p = ctx.paint
135
+ if len(args) < 2:
136
+ ctx.print(p.error("Usage: /alias <new_name> <existing_command>"))
137
+ return
138
+ new_name, target = args[0].lstrip("/"), args[1].lstrip("/")
139
+ cmd = ctx.registry.resolve(target)
140
+ if cmd is None:
141
+ ctx.print(p.error(f"No command: /{target}"))
142
+ return
143
+ ctx.registry._alias[new_name] = cmd.name
144
+ ctx.print(p.success(f" ✓ /{new_name} → /{cmd.name}"))
@@ -0,0 +1,164 @@
1
+ """
2
+ Inspector commands — explore the live kernel namespace.
3
+
4
+ /vars — list all variables in the namespace
5
+ /who <name> — print type + repr of a variable
6
+ /doc <name> — show docstring
7
+ /source <name> — show source code (functions / classes)
8
+ /del <name> — delete a variable
9
+ /timeit <expr> — time an expression
10
+ """
11
+
12
+ import inspect, timeit as _timeit, textwrap
13
+ from pykernel.core.registry import CommandRegistry
14
+
15
+
16
+ def register(reg: CommandRegistry):
17
+
18
+ # ── /vars ─────────────────────────────────────────────────────────────────
19
+
20
+ @reg.command("vars", aliases=["ls", "env"],
21
+ help="List all variables in the kernel namespace",
22
+ usage="/vars",
23
+ category="Inspector")
24
+ def cmd_vars(ctx, *args):
25
+ p = ctx.paint
26
+ ns = ctx.namespace_vars()
27
+ if not ns:
28
+ ctx.print(p.dim(" (namespace is empty)"))
29
+ return
30
+ rows = []
31
+ for name, val in sorted(ns.items()):
32
+ typ = type(val).__name__
33
+ snippet = repr(val)[:60].replace("\n", " ")
34
+ rows.append((name, typ, snippet))
35
+ ctx.print()
36
+ ctx.print(p.table(rows, headers=["Name", "Type", "Value"]))
37
+ ctx.print()
38
+
39
+ # ── /who ──────────────────────────────────────────────────────────────────
40
+
41
+ @reg.command("who",
42
+ help="Show detailed info about a variable",
43
+ usage="/who <name>",
44
+ category="Inspector")
45
+ def cmd_who(ctx, *args):
46
+ p = ctx.paint
47
+ if not args:
48
+ ctx.print(p.error("Usage: /who <name>"))
49
+ return
50
+ name = args[0]
51
+ if not ctx.has_var(name):
52
+ ctx.print(p.error(f"'{name}' is not defined in the namespace"))
53
+ return
54
+ val = ctx.get_var(name)
55
+ typ = type(val)
56
+ ctx.print()
57
+ ctx.print(p.header(f" {name}"))
58
+ ctx.print(f" type : {p.code(typ.__module__ + '.' + typ.__name__)}")
59
+ ctx.print(f" repr : {repr(val)[:120]}")
60
+ if hasattr(val, "__len__"):
61
+ try: ctx.print(f" len : {len(val)}")
62
+ except: pass
63
+ if hasattr(val, "shape"):
64
+ ctx.print(f" shape : {val.shape}")
65
+ if hasattr(val, "dtype"):
66
+ ctx.print(f" dtype : {val.dtype}")
67
+ ctx.print(f" id : {id(val)}")
68
+ ctx.print()
69
+
70
+ # ── /doc ──────────────────────────────────────────────────────────────────
71
+
72
+ @reg.command("doc",
73
+ help="Show the docstring of a name",
74
+ usage="/doc <name>",
75
+ category="Inspector")
76
+ def cmd_doc(ctx, *args):
77
+ p = ctx.paint
78
+ if not args:
79
+ ctx.print(p.error("Usage: /doc <name>"))
80
+ return
81
+ name = args[0]
82
+ # try namespace first, then builtins
83
+ val = ctx.get_var(name, None) or __builtins__.__dict__.get(name) if isinstance(__builtins__, dict) else getattr(__builtins__, name, None)
84
+ if val is None:
85
+ try:
86
+ import builtins
87
+ val = getattr(builtins, name, None)
88
+ except: pass
89
+ if val is None:
90
+ ctx.print(p.error(f"'{name}' not found"))
91
+ return
92
+ doc = inspect.getdoc(val) or "(no docstring)"
93
+ ctx.print()
94
+ ctx.print(p.header(f" {name}"))
95
+ ctx.print(textwrap.indent(doc, " "))
96
+ ctx.print()
97
+
98
+ # ── /source ───────────────────────────────────────────────────────────────
99
+
100
+ @reg.command("source", aliases=["src"],
101
+ help="Show source code of a function or class",
102
+ usage="/source <name>",
103
+ category="Inspector")
104
+ def cmd_source(ctx, *args):
105
+ p = ctx.paint
106
+ if not args:
107
+ ctx.print(p.error("Usage: /source <name>"))
108
+ return
109
+ name = args[0]
110
+ val = ctx.get_var(name)
111
+ if val is None:
112
+ ctx.print(p.error(f"'{name}' not in namespace"))
113
+ return
114
+ try:
115
+ src = inspect.getsource(val)
116
+ ctx.print()
117
+ ctx.print(p.code(src))
118
+ ctx.print()
119
+ except (TypeError, OSError) as e:
120
+ ctx.print(p.error(str(e)))
121
+
122
+ # ── /del ──────────────────────────────────────────────────────────────────
123
+
124
+ @reg.command("del", aliases=["delete", "rm"],
125
+ help="Delete a variable from the namespace",
126
+ usage="/del <name>",
127
+ category="Inspector")
128
+ def cmd_del(ctx, *args):
129
+ p = ctx.paint
130
+ if not args:
131
+ ctx.print(p.error("Usage: /del <name>"))
132
+ return
133
+ for name in args:
134
+ if ctx.delete_var(name):
135
+ ctx.print(p.success(f" ✓ Deleted '{name}'"))
136
+ else:
137
+ ctx.print(p.warning(f" ⚠ '{name}' not in namespace"))
138
+
139
+ # ── /timeit ───────────────────────────────────────────────────────────────
140
+
141
+ @reg.command("timeit",
142
+ help="Time an expression (/timeit sum(range(1000000)))",
143
+ usage="/timeit <expression> [repeat=N]",
144
+ category="Inspector")
145
+ def cmd_timeit(ctx, *args):
146
+ p = ctx.paint
147
+ if not args:
148
+ ctx.print(p.error("Usage: /timeit <expression>"))
149
+ return
150
+ # allow trailing repeat=N
151
+ repeat = 3
152
+ expr_parts = list(args)
153
+ if expr_parts and expr_parts[-1].startswith("repeat="):
154
+ try: repeat = int(expr_parts.pop()[7:])
155
+ except: pass
156
+ expr = " ".join(expr_parts)
157
+ try:
158
+ times = _timeit.repeat(expr, globals=ctx.env, repeat=repeat, number=1000)
159
+ best = min(times) * 1_000_000 / 1000 # µs per loop
160
+ ctx.print()
161
+ ctx.print(p.info(f" {best:.3f} µs ± per loop (best of {repeat}×1000)"))
162
+ ctx.print()
163
+ except Exception as e:
164
+ ctx.print(p.error(str(e)))
@@ -0,0 +1,101 @@
1
+ """
2
+ Shell commands — run system commands, navigate dirs, etc.
3
+
4
+ /sh <cmd> — run a shell command (output captured into _)
5
+ /cd <dir> — change working directory
6
+ /pwd — print working directory
7
+ /ls [dir] — list directory contents
8
+ """
9
+
10
+ import os, subprocess, shlex, pathlib
11
+ from pykernel.core.registry import CommandRegistry
12
+
13
+
14
+ def register(reg: CommandRegistry):
15
+
16
+ # ── /sh ───────────────────────────────────────────────────────────────────
17
+
18
+ @reg.command("sh", aliases=["!"],
19
+ help="Run a shell command; output stored in `_out`",
20
+ usage="/sh <shell command>",
21
+ category="Shell")
22
+ def cmd_sh(ctx, *args):
23
+ p = ctx.paint
24
+ if not args:
25
+ ctx.print(p.error("Usage: /sh <command>"))
26
+ return
27
+ cmd = " ".join(args)
28
+ try:
29
+ result = subprocess.run(
30
+ cmd, shell=True, text=True,
31
+ capture_output=True
32
+ )
33
+ out = result.stdout
34
+ err = result.stderr
35
+ if out:
36
+ ctx.print(out, end="")
37
+ if err:
38
+ ctx.print(p.warning(err), end="")
39
+ if result.returncode != 0:
40
+ ctx.print(p.dim(f" exit code: {result.returncode}"))
41
+ # inject result into namespace
42
+ ctx.set_var("_out", out)
43
+ ctx.set_var("_err", err)
44
+ ctx.set_var("_retcode", result.returncode)
45
+ except Exception as e:
46
+ ctx.print(p.error(str(e)))
47
+
48
+ # ── /cd ───────────────────────────────────────────────────────────────────
49
+
50
+ @reg.command("cd",
51
+ help="Change the current working directory",
52
+ usage="/cd <path>",
53
+ category="Shell")
54
+ def cmd_cd(ctx, *args):
55
+ p = ctx.paint
56
+ dest = pathlib.Path(args[0]).expanduser() if args else pathlib.Path.home()
57
+ try:
58
+ os.chdir(dest)
59
+ ctx.print(p.info(f" → {os.getcwd()}"))
60
+ except FileNotFoundError:
61
+ ctx.print(p.error(f"Directory not found: {dest}"))
62
+ except PermissionError:
63
+ ctx.print(p.error(f"Permission denied: {dest}"))
64
+
65
+ # ── /pwd ──────────────────────────────────────────────────────────────────
66
+
67
+ @reg.command("pwd",
68
+ help="Print the current working directory",
69
+ usage="/pwd",
70
+ category="Shell")
71
+ def cmd_pwd(ctx, *args):
72
+ ctx.print(ctx.paint.info(f" {os.getcwd()}"))
73
+
74
+ # ── /ls ───────────────────────────────────────────────────────────────────
75
+
76
+ @reg.command("ls",
77
+ help="List directory contents",
78
+ usage="/ls [path]",
79
+ category="Shell")
80
+ def cmd_ls(ctx, *args):
81
+ p = ctx.paint
82
+ path = pathlib.Path(args[0]).expanduser() if args else pathlib.Path(".")
83
+ try:
84
+ entries = sorted(path.iterdir(), key=lambda e: (not e.is_dir(), e.name.lower()))
85
+ except (FileNotFoundError, PermissionError) as e:
86
+ ctx.print(p.error(str(e)))
87
+ return
88
+
89
+ ctx.print()
90
+ for entry in entries:
91
+ if entry.is_dir():
92
+ ctx.print(p.accent(f" 📁 {entry.name}/"))
93
+ else:
94
+ size = entry.stat().st_size
95
+ size_s = (
96
+ f"{size / 1_048_576:.1f} MB" if size > 1_048_576
97
+ else f"{size / 1_024:.1f} KB" if size > 1_024
98
+ else f"{size} B"
99
+ )
100
+ ctx.print(f" 📄 {entry.name:<40} {p.dim(size_s)}")
101
+ ctx.print()
@@ -0,0 +1,257 @@
1
+ """
2
+ Snippet commands — save and replay named code snippets.
3
+
4
+ Snippets are stored as .py files in ~/.pykernel/snippets/.
5
+
6
+ /snip save <name> — save the last history entry as a snippet
7
+ /snip write <name> — open $EDITOR to write a snippet from scratch
8
+ /snip list — list all saved snippets
9
+ /snip show <name> — print a snippet's source
10
+ /snip run <name> — execute a snippet in the kernel namespace
11
+ /snip edit <name> — open a saved snippet in $EDITOR
12
+ /snip del <name> — delete a snippet
13
+ """
14
+
15
+ import os, pathlib, subprocess, tempfile, traceback
16
+ from pykernel.core.registry import CommandRegistry
17
+
18
+ SNIPPETS_DIR = pathlib.Path.home() / ".pykernel" / "snippets"
19
+
20
+
21
+ def _ensure_dir():
22
+ SNIPPETS_DIR.mkdir(parents=True, exist_ok=True)
23
+
24
+
25
+ def _path(name: str) -> pathlib.Path:
26
+ # sanitise: allow only alphanumerics, dashes, underscores
27
+ safe = "".join(c if c.isalnum() or c in "-_" else "_" for c in name)
28
+ return SNIPPETS_DIR / f"{safe}.py"
29
+
30
+
31
+ def _editor() -> str:
32
+ return os.environ.get("VISUAL") or os.environ.get("EDITOR") or "vi"
33
+
34
+
35
+ def _open_in_editor(fpath: pathlib.Path, initial: str = "") -> bool:
36
+ """Write initial content, open editor, return True if saved non-empty."""
37
+ if not fpath.exists():
38
+ fpath.write_text(initial)
39
+ ret = subprocess.call([_editor(), str(fpath)])
40
+ return ret == 0 and fpath.exists() and fpath.stat().st_size > 0
41
+
42
+
43
+ # ─────────────────────────────────────────────────────────────────────────────
44
+
45
+ def register(reg: CommandRegistry):
46
+
47
+ # ── /snip ─────────────────────────────────────────────────────────────────
48
+
49
+ @reg.command("snip", aliases=["snippet"],
50
+ help="Manage code snippets (save / list / show / run / edit / del)",
51
+ usage="/snip <subcommand> [name]",
52
+ category="Snippets")
53
+ def cmd_snip(ctx, *args):
54
+ p = ctx.paint
55
+ if not args:
56
+ _print_usage(ctx)
57
+ return
58
+
59
+ sub = args[0].lower()
60
+ rest = args[1:]
61
+
62
+ dispatch = {
63
+ "save": _save,
64
+ "write": _write,
65
+ "list": _list,
66
+ "ls": _list,
67
+ "show": _show,
68
+ "cat": _show,
69
+ "run": _run,
70
+ "exec": _run,
71
+ "edit": _edit,
72
+ "del": _delete,
73
+ "rm": _delete,
74
+ "delete":_delete,
75
+ }
76
+
77
+ fn = dispatch.get(sub)
78
+ if fn is None:
79
+ ctx.print(p.error(f"Unknown subcommand: '{sub}'"))
80
+ _print_usage(ctx)
81
+ return
82
+
83
+ fn(ctx, *rest)
84
+
85
+ # ── subcommand helpers ────────────────────────────────────────────────────
86
+
87
+ def _print_usage(ctx):
88
+ p = ctx.paint
89
+ rows = [
90
+ ("save <name>", "Save last history entry as a snippet"),
91
+ ("write <name>", "Create a new snippet in $EDITOR"),
92
+ ("list", "List all saved snippets"),
93
+ ("show <name>", "Print a snippet's source"),
94
+ ("run <name>", "Execute a snippet in the kernel namespace"),
95
+ ("edit <name>", "Open a saved snippet in $EDITOR"),
96
+ ("del <name>", "Delete a snippet"),
97
+ ]
98
+ ctx.print()
99
+ ctx.print(p.header(" Snippet subcommands"))
100
+ ctx.print(p.table(rows, headers=["Subcommand", "Description"]))
101
+ ctx.print()
102
+
103
+ # ── save ──────────────────────────────────────────────────────────────────
104
+
105
+ def _save(ctx, *args):
106
+ p = ctx.paint
107
+ if not args:
108
+ ctx.print(p.error("Usage: /snip save <name>"))
109
+ return
110
+
111
+ name = args[0]
112
+ # find last non-/snip history entry
113
+ source = None
114
+ for entry in reversed(ctx.history):
115
+ if not entry.strip().startswith("/snip"):
116
+ source = entry
117
+ break
118
+
119
+ if not source:
120
+ ctx.print(p.warning(" ⚠ No suitable history entry found to save."))
121
+ return
122
+
123
+ _ensure_dir()
124
+ fpath = _path(name)
125
+ existed = fpath.exists()
126
+ fpath.write_text(source.rstrip() + "\n")
127
+ verb = "Updated" if existed else "Saved"
128
+ ctx.print(p.success(f" ✓ {verb} snippet '{name}' → {fpath}"))
129
+
130
+ # ── write ─────────────────────────────────────────────────────────────────
131
+
132
+ def _write(ctx, *args):
133
+ p = ctx.paint
134
+ if not args:
135
+ ctx.print(p.error("Usage: /snip write <name>"))
136
+ return
137
+
138
+ name = args[0]
139
+ _ensure_dir()
140
+ fpath = _path(name)
141
+
142
+ if fpath.exists():
143
+ ctx.print(p.warning(f" ⚠ '{name}' already exists — use /snip edit to modify it."))
144
+ return
145
+
146
+ ctx.print(p.dim(f" Opening {_editor()} …"))
147
+ ok = _open_in_editor(fpath, initial=f"# snippet: {name}\n")
148
+ if ok:
149
+ ctx.print(p.success(f" ✓ Snippet '{name}' saved → {fpath}"))
150
+ else:
151
+ if fpath.exists() and fpath.stat().st_size == 0:
152
+ fpath.unlink()
153
+ ctx.print(p.warning(" ⚠ Snippet discarded (empty or editor error)."))
154
+
155
+ # ── list ──────────────────────────────────────────────────────────────────
156
+
157
+ def _list(ctx, *args):
158
+ p = ctx.paint
159
+ _ensure_dir()
160
+ files = sorted(SNIPPETS_DIR.glob("*.py"))
161
+
162
+ if not files:
163
+ ctx.print(p.dim(" (no snippets saved yet — try /snip save <name>)"))
164
+ return
165
+
166
+ rows = []
167
+ for f in files:
168
+ name = f.stem
169
+ lines = f.read_text().splitlines()
170
+ size = f.stat().st_size
171
+ # first non-comment, non-blank line as preview
172
+ preview = next(
173
+ (l.strip() for l in lines if l.strip() and not l.strip().startswith("#")),
174
+ "(empty)"
175
+ )[:60]
176
+ rows.append((name, f"{len(lines)} lines", f"{size} B", preview))
177
+
178
+ ctx.print()
179
+ ctx.print(p.table(rows, headers=["Name", "Lines", "Size", "Preview"]))
180
+ ctx.print()
181
+
182
+ # ── show ──────────────────────────────────────────────────────────────────
183
+
184
+ def _show(ctx, *args):
185
+ p = ctx.paint
186
+ if not args:
187
+ ctx.print(p.error("Usage: /snip show <name>"))
188
+ return
189
+
190
+ fpath = _path(args[0])
191
+ if not fpath.exists():
192
+ ctx.print(p.error(f"Snippet '{args[0]}' not found."))
193
+ return
194
+
195
+ source = fpath.read_text()
196
+ ctx.print()
197
+ ctx.print(p.header(f" {args[0]} ({fpath.name})"))
198
+ ctx.print(p.code(source))
199
+
200
+ # ── run ───────────────────────────────────────────────────────────────────
201
+
202
+ def _run(ctx, *args):
203
+ p = ctx.paint
204
+ if not args:
205
+ ctx.print(p.error("Usage: /snip run <name>"))
206
+ return
207
+
208
+ fpath = _path(args[0])
209
+ if not fpath.exists():
210
+ ctx.print(p.error(f"Snippet '{args[0]}' not found."))
211
+ return
212
+
213
+ source = fpath.read_text()
214
+ ctx.print(p.dim(f" Running snippet '{args[0]}' …"))
215
+ try:
216
+ exec(compile(source, str(fpath), "exec"), ctx.env)
217
+ ctx.print(p.success(f" ✓ Done."))
218
+ except SystemExit:
219
+ raise
220
+ except Exception:
221
+ lines = traceback.format_exc().splitlines()
222
+ cleaned = [l for l in lines if str(fpath) in l or not l.strip().startswith('File "')]
223
+ ctx.print(p.error("\n".join(cleaned or lines)))
224
+
225
+ # ── edit ──────────────────────────────────────────────────────────────────
226
+
227
+ def _edit(ctx, *args):
228
+ p = ctx.paint
229
+ if not args:
230
+ ctx.print(p.error("Usage: /snip edit <name>"))
231
+ return
232
+
233
+ name = args[0]
234
+ fpath = _path(name)
235
+ if not fpath.exists():
236
+ ctx.print(p.warning(f" ⚠ '{name}' doesn't exist — use /snip write to create it."))
237
+ return
238
+
239
+ ctx.print(p.dim(f" Opening {_editor()} …"))
240
+ _open_in_editor(fpath)
241
+ ctx.print(p.success(f" ✓ Snippet '{name}' updated."))
242
+
243
+ # ── delete ────────────────────────────────────────────────────────────────
244
+
245
+ def _delete(ctx, *args):
246
+ p = ctx.paint
247
+ if not args:
248
+ ctx.print(p.error("Usage: /snip del <name>"))
249
+ return
250
+
251
+ for name in args:
252
+ fpath = _path(name)
253
+ if fpath.exists():
254
+ fpath.unlink()
255
+ ctx.print(p.success(f" ✓ Deleted snippet '{name}'"))
256
+ else:
257
+ ctx.print(p.warning(f" ⚠ Snippet '{name}' not found."))
File without changes