diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-10-14 09:53:33 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-10-21 13:39:45 +0530 |
| commit | 94bb19748d3bb8636cf252539b8f0be18f46515a (patch) | |
| tree | 8029e727bd4a8d2a1282cd9b08b9cf8d6c3e72ce | |
| parent | 2fa61e7942d899c7d883d3f5a82e7623740ebb6b (diff) | |
| download | kakoune-config-94bb19748d3bb8636cf252539b8f0be18f46515a.tar.gz kakoune-config-94bb19748d3bb8636cf252539b8f0be18f46515a.zip | |
Switch grep update script to fnl
| -rw-r--r-- | autoload/grep.kak | 4 | ||||
| -rwxr-xr-x | scripts/apply_vimgrep_updates.fnl | 39 | ||||
| -rwxr-xr-x | scripts/apply_vimgrep_updates.js | 29 |
3 files changed, 41 insertions, 31 deletions
diff --git a/autoload/grep.kak b/autoload/grep.kak index cef77ee..13a44bd 100644 --- a/autoload/grep.kak +++ b/autoload/grep.kak @@ -1,11 +1,11 @@ set-option global grepcmd "rg -S --vimgrep --hidden -g '!**/.git/**'" -map global file g ':grep ' -docstring 'Grep' +map global file g ":grep ''<left>" -docstring 'Grep' define-command grep-write %{ execute-keys '%' # Consider selecting manually evaluate-commands %sh{ - echo "$kak_selections" | "$kak_config/scripts/apply_vimgrep_updates.js" | xargs -i echo "info '{}'" + echo "$kak_selections" | "$kak_config/scripts/apply_vimgrep_updates.fnl" | xargs -i echo "info '{}'" } } diff --git a/scripts/apply_vimgrep_updates.fnl b/scripts/apply_vimgrep_updates.fnl new file mode 100755 index 0000000..b70e93e --- /dev/null +++ b/scripts/apply_vimgrep_updates.fnl @@ -0,0 +1,39 @@ +#!/usr/bin/env -S fennel --lua luajit + +(local M {}) + +(fn M.main [] + (local write-count (M.apply-vimgrep-changes)) + (print (.. "Applied " write-count " changes"))) + +(fn M.apply-vimgrep-changes [] + (var count 0) + (each [line (io.lines)] + (local (filepath linenr _ text) (M.parse-vimgrep line)) + (when filepath + (local applied? (M.update-line filepath linenr text)) + (when applied? + (set count (+ count 1))))) + count) + +(fn M.parse-vimgrep [line] + (local (filepath linenr col text) (string.match line "(.*):(%d+):(%d+):(.*)")) + (values filepath (tonumber linenr) col text)) + +(fn M.update-line [filepath linenr text] + (local lines (M.read-lines filepath)) + (var written? false) + (when (and (> (length lines) linenr) (not= (. lines linenr) text)) + (set (. lines linenr) text) + (local file (io.open filepath :w)) + (file:write (table.concat lines "\n")) + (file:close) + (set written? true)) + written?) + +(fn M.read-lines [filepath] + (local file (io.open filepath :r)) + (local lines []) + (each [line (file:lines)] (table.insert lines line)) + (file:close) + lines) diff --git a/scripts/apply_vimgrep_updates.js b/scripts/apply_vimgrep_updates.js deleted file mode 100755 index 9707aab..0000000 --- a/scripts/apply_vimgrep_updates.js +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env bun - -const main = async () => { - let count = 0 - for await (const line of console) { - const [_, filepath, row, _col, text] = line.match(/(.*):(\d+):(\d+):(.*)/) ?? [] - if (filepath) { - const isApplied = await apply_edit(filepath, Number(row), text); - if (isApplied) count++ - } - } - return count -} - -const apply_edit = async (filepath, row, text) => { - if (!Number.isFinite(row) || row <= 0) return false; - const file = Bun.file(filepath) - const lines = (await file.text()).split('\n') - if (lines.length > row) { - lines[row - 1] = text - await Bun.write(filepath, lines.join('\n')) - return true - } - return false -} - -const changesCount = await main() -console.log(`Applied ${changesCount} changes`) - |
