@rse/ink-editor 0.0.0-stage → 0.9.1

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.
Files changed (63) hide show
  1. package/.claude/CLAUDE.md +1 -0
  2. package/AGENTS.md +99 -0
  3. package/CHANGELOG.md +9 -0
  4. package/README.md +116 -2
  5. package/dst-stage1/ink-editor-cli.d.ts +2 -0
  6. package/dst-stage1/ink-editor-cli.js +174 -0
  7. package/dst-stage1/ink-editor-engine-emacs.d.ts +103 -0
  8. package/dst-stage1/ink-editor-engine-emacs.js +1530 -0
  9. package/dst-stage1/ink-editor-engine-nano.d.ts +78 -0
  10. package/dst-stage1/ink-editor-engine-nano.js +884 -0
  11. package/dst-stage1/ink-editor-engine-vim.d.ts +124 -0
  12. package/dst-stage1/ink-editor-engine-vim.js +2220 -0
  13. package/dst-stage1/ink-editor-engine-vscode.d.ts +79 -0
  14. package/dst-stage1/ink-editor-engine-vscode.js +850 -0
  15. package/dst-stage1/ink-editor-mode-emacs.d.ts +12 -0
  16. package/dst-stage1/ink-editor-mode-emacs.js +143 -0
  17. package/dst-stage1/ink-editor-mode-nano.d.ts +12 -0
  18. package/dst-stage1/ink-editor-mode-nano.js +140 -0
  19. package/dst-stage1/ink-editor-mode-vim.d.ts +12 -0
  20. package/dst-stage1/ink-editor-mode-vim.js +230 -0
  21. package/dst-stage1/ink-editor-mode-vscode.d.ts +12 -0
  22. package/dst-stage1/ink-editor-mode-vscode.js +133 -0
  23. package/dst-stage1/ink-editor-mode.d.ts +72 -0
  24. package/dst-stage1/ink-editor-mode.js +24 -0
  25. package/dst-stage1/ink-editor-syntax.d.ts +14 -0
  26. package/dst-stage1/ink-editor-syntax.js +149 -0
  27. package/dst-stage1/ink-editor-text.d.ts +38 -0
  28. package/dst-stage1/ink-editor-text.js +177 -0
  29. package/dst-stage1/ink-editor-widget.d.ts +15 -0
  30. package/dst-stage1/ink-editor-widget.js +132 -0
  31. package/dst-stage1/ink-editor.d.ts +13 -0
  32. package/dst-stage1/ink-editor.js +37 -0
  33. package/dst-stage1/tsc.tsbuildinfo +1 -0
  34. package/dst-stage2/ink-editor-cli.esm.js +177 -0
  35. package/dst-stage2/ink-editor.esm.js +5972 -0
  36. package/etc/c8.json +17 -0
  37. package/etc/eslint.mts +84 -0
  38. package/etc/knip.jsonc +51 -0
  39. package/etc/stx.conf +103 -0
  40. package/etc/tsc.json +29 -0
  41. package/etc/vite.mts +79 -0
  42. package/package.d/@typescript-eslint+typescript-estree+8.74.0.patch +12 -0
  43. package/package.d/vite+8.4.1.patch +12 -0
  44. package/package.json +79 -5
  45. package/src/ink-editor-cli.ts +200 -0
  46. package/src/ink-editor-engine-emacs.ts +1648 -0
  47. package/src/ink-editor-engine-nano.ts +978 -0
  48. package/src/ink-editor-engine-vim.ts +2203 -0
  49. package/src/ink-editor-engine-vscode.ts +938 -0
  50. package/src/ink-editor-mode-emacs.ts +137 -0
  51. package/src/ink-editor-mode-nano.ts +135 -0
  52. package/src/ink-editor-mode-vim.ts +223 -0
  53. package/src/ink-editor-mode-vscode.ts +129 -0
  54. package/src/ink-editor-mode.ts +137 -0
  55. package/src/ink-editor-syntax.ts +176 -0
  56. package/src/ink-editor-text.ts +205 -0
  57. package/src/ink-editor-widget.ts +182 -0
  58. package/src/ink-editor.ts +48 -0
  59. package/tst/ink-editor-1-vim.spec.ts +177 -0
  60. package/tst/ink-editor-2-emacs.spec.ts +211 -0
  61. package/tst/ink-editor-3-nano.spec.ts +187 -0
  62. package/tst/ink-editor-4-vscode.spec.ts +187 -0
  63. package/tst/tsc.json +27 -0
@@ -0,0 +1,200 @@
1
+ #!/usr/bin/env node
2
+ /*
3
+ ** Ink-Editor -- Text Editor with Exchangeable Editing Modes for Ink
4
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
5
+ **
6
+ ** Permission is hereby granted, free of charge, to any person obtaining
7
+ ** a copy of this software and associated documentation files (the
8
+ ** "Software"), to deal in the Software without restriction, including
9
+ ** without limitation the rights to use, copy, modify, merge, publish,
10
+ ** distribute, sublicense, and/or sell copies of the Software, and to
11
+ ** permit persons to whom the Software is furnished to do so, subject to
12
+ ** the following conditions:
13
+ **
14
+ ** The above copyright notice and this permission notice shall be included
15
+ ** in all copies or substantial portions of the Software.
16
+ **
17
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+
26
+ /* text editor application, on top of the editor widget of the library
27
+ Usage: ink-editor [--mode=vim|emacs|nano|vscode] [--no-syntax] [<file>] */
28
+
29
+ /* external dependencies */
30
+ import fs from "node:fs"
31
+ import React from "react"
32
+ import { useApp, useWindowSize } from "ink"
33
+ import { render } from "better-ink-ui"
34
+
35
+ /* internal dependencies */
36
+ import { Editor, editorModes as modes,
37
+ syntaxLanguage, syntaxLoad } from "./ink-editor"
38
+ import type { Syntax, EditorModeFactory,
39
+ EditorHost, EditorResult } from "./ink-editor"
40
+
41
+ const h = React.createElement
42
+
43
+ /* a loaded file (without its final newline and with its line endings normalized), whether
44
+ it was a single empty line, and the outcome of loading it (null without a file name) */
45
+ type Loaded = { text: string, crlf: boolean, blank: boolean, result: EditorResult | null }
46
+
47
+ /* statistics of the data of a file on disk */
48
+ const stats = (data: string) => {
49
+ const lines = data === "" ? 0 : data.split("\n").length - (data.endsWith("\n") ? 1 : 0)
50
+ return { lines, bytes: Buffer.byteLength(data) }
51
+ }
52
+
53
+ /* load a file, if it exists */
54
+ const load = (name: string): Loaded => {
55
+ if (name === "")
56
+ return { text: "", crlf: false, blank: false, result: null }
57
+ if (!fs.existsSync(name))
58
+ return { text: "", crlf: false, blank: false, result: { name, isNew: true, crlf: false, lines: 0, bytes: 0 } }
59
+ let text: string
60
+ try {
61
+ text = fs.readFileSync(name, "utf8")
62
+ }
63
+ catch {
64
+ return { text: "", crlf: false, blank: false, result: { error: "read", detail: name } }
65
+ }
66
+ const info = stats(text)
67
+ const crlf = text.includes("\r\n")
68
+ if (crlf)
69
+ text = text.replaceAll("\r\n", "\n")
70
+ const blank = text === "\n"
71
+ if (text.endsWith("\n"))
72
+ text = text.slice(0, -1)
73
+ return { text, crlf, blank, result: { name, isNew: false, crlf, ...info } }
74
+ }
75
+
76
+ /* the editor application */
77
+ function App (props: { name: string, file: Loaded, mode: EditorModeFactory, syntax: Syntax | null }) {
78
+ const app = useApp()
79
+ const { rows } = useWindowSize()
80
+
81
+ /* the text */
82
+ const [ value, setValue ] = React.useState(props.file.text)
83
+
84
+ /* the current and saved text, the file name, and the line endings */
85
+ const valueRef = React.useRef(props.file.text)
86
+ const savedRef = React.useRef(props.file.text)
87
+ const nameRef = React.useRef(props.name)
88
+ const crlfRef = React.useRef(props.file.crlf)
89
+ const blankRef = React.useRef(props.file.blank)
90
+
91
+ /* the values are read through references, as the commands of the mode run before a re-render */
92
+ const onChange = (v: string) => {
93
+ valueRef.current = v
94
+ setValue(v)
95
+ }
96
+ const modified = () => valueRef.current !== savedRef.current
97
+
98
+ /* write the text to a file */
99
+ const write = (file: string, force: boolean, rename = false): EditorResult => {
100
+ const target = file || nameRef.current
101
+ if (target === "")
102
+ return { error: "noname", detail: "" }
103
+ const isNew = !fs.existsSync(target)
104
+ if (target !== nameRef.current && !isNew && !force)
105
+ return { error: "exists", detail: target }
106
+ const text = valueRef.current
107
+ let data = text === "" && !blankRef.current ? "" : text + "\n"
108
+ if (crlfRef.current)
109
+ data = data.replaceAll("\n", "\r\n")
110
+ try {
111
+ fs.writeFileSync(target, data, "utf8")
112
+ }
113
+ catch (err) {
114
+ return { error: "write", detail: err instanceof Error ? err.message : String(err) }
115
+ }
116
+ if (nameRef.current === "" || rename)
117
+ nameRef.current = target
118
+ if (target === nameRef.current)
119
+ savedRef.current = text
120
+ return { name: target, isNew, crlf: crlfRef.current, ...stats(data) }
121
+ }
122
+
123
+ /* reload the text of the edited file */
124
+ const reload = (): EditorResult => {
125
+ const file = load(nameRef.current)
126
+ if (file.result === null)
127
+ return { error: "noname", detail: "" }
128
+ if ("error" in file.result)
129
+ return file.result
130
+ valueRef.current = savedRef.current = file.text
131
+ crlfRef.current = file.crlf
132
+ blankRef.current = file.blank
133
+ setValue(file.text)
134
+ return file.result
135
+ }
136
+
137
+ /* the file operations and the quitting for the commands of the mode */
138
+ const host: EditorHost = {
139
+ name: () => nameRef.current,
140
+ modified,
141
+ write,
142
+ reload,
143
+ quit: (failed = false) => {
144
+ if (failed)
145
+ process.exitCode = 1
146
+ app.exit()
147
+ }
148
+ }
149
+
150
+ /* the text area takes all rows except the one of the status line */
151
+ const height = Math.max(1, rows - 1)
152
+ return h(Editor, { value, onChange, height, mode: props.mode, host, file: props.file.result, syntax: props.syntax })
153
+ }
154
+
155
+ /* parse the command line */
156
+ let mode = "vim"
157
+ let highlight = true
158
+ let name = ""
159
+ for (const arg of process.argv.slice(2)) {
160
+ if (arg.startsWith("--mode="))
161
+ mode = arg.slice(7)
162
+ else if (arg === "--no-syntax")
163
+ highlight = false
164
+ else if (arg === "--syntax")
165
+ highlight = true
166
+ else if (arg.startsWith("-") && arg !== "-") {
167
+ process.stderr.write(`ink-editor: ERROR: unknown option "${arg}"\nUsage: ink-editor [--mode=vim|emacs|nano|vscode] [--no-syntax] [<file>]\n`)
168
+ process.exit(1)
169
+ }
170
+ else if (name !== "") {
171
+ process.stderr.write("ink-editor: ERROR: only a single file can be edited\n")
172
+ process.exit(1)
173
+ }
174
+ else
175
+ name = arg
176
+ }
177
+ if (!Object.hasOwn(modes, mode)) {
178
+ process.stderr.write(`ink-editor: ERROR: unknown mode "${mode}" (known: ${Object.keys(modes).join(", ")})\n`)
179
+ process.exit(1)
180
+ }
181
+
182
+ /* main entry point */
183
+ if (!process.stdin.isTTY || !process.stdout.isTTY) {
184
+ process.stderr.write("ink-editor: ERROR: standard input and output have to be a terminal\n")
185
+ process.exit(1)
186
+ }
187
+ const file = load(name)
188
+
189
+ /* load the syntax highlighting of the file's language, and tokenize the text once
190
+ before the first rendering, so editing afterwards re-tokenizes only changed lines */
191
+ const syntax = highlight && name !== "" ? await syntaxLoad(syntaxLanguage(name)) : null
192
+ syntax?.(file.text)
193
+
194
+ const instance = render(h(App, { name, file, mode: modes[mode], syntax }), {
195
+ exitOnCtrlC: false,
196
+ alternateScreen: true
197
+ })
198
+ await instance.waitUntilExit()
199
+ process.exit()
200
+