aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-02-29 16:38:35 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2024-02-29 17:00:46 +0100
commit055cbe24255be0b376d840a92948c830c46e3adc (patch)
tree84c044003f8e49fdd45d451f3537537e3fcf5ed2
parentfa90804d713d68831e850d960c211bd0d656a736 (diff)
downloaddotfiles-055cbe24255be0b376d840a92948c830c46e3adc.tar.gz
dotfiles-055cbe24255be0b376d840a92948c830c46e3adc.zip
[vis] add simple plugin to asynchronously run build commands
-rw-r--r--config/vis/plugins/vis-build/.editorconfig11
-rw-r--r--config/vis/plugins/vis-build/Makefile19
-rw-r--r--config/vis/plugins/vis-build/README.md24
-rw-r--r--config/vis/plugins/vis-build/init.lua52
-rwxr-xr-xconfig/vis/plugins/vis-build/tools/check-format8
-rw-r--r--config/vis/visrc.lua11
6 files changed, 123 insertions, 2 deletions
diff --git a/config/vis/plugins/vis-build/.editorconfig b/config/vis/plugins/vis-build/.editorconfig
new file mode 100644
index 0000000..1635025
--- /dev/null
+++ b/config/vis/plugins/vis-build/.editorconfig
@@ -0,0 +1,11 @@
+# http://editorconfig.org
+
+root = true
+
+[*.lua]
+indent_style = space
+indent_size = 2
+
+[.gitlab-ci.yml]
+indent_style = space
+indent_size = 2
diff --git a/config/vis/plugins/vis-build/Makefile b/config/vis/plugins/vis-build/Makefile
new file mode 100644
index 0000000..144e229
--- /dev/null
+++ b/config/vis/plugins/vis-build/Makefile
@@ -0,0 +1,19 @@
+.PHONY: check format check-luacheck check-format
+
+LUA_FILES := $(shell find -name "*.lua" -not -path "./json.lua")
+
+TEST_FILES := $(shell find -name "*_test.lua")
+
+check: check-luacheck check-format
+
+check-luacheck:
+ luacheck --globals=vis -- $(LUA_FILES)
+
+check-format:
+ set -e; for lf in $(LUA_FILES); do tools/check-format "$${lf}"; done
+
+format:
+ lua-format -i $(LUA_FILES)
+
+test:
+ set -e; for tf in $(TEST_FILES); do "$$tf"; done
diff --git a/config/vis/plugins/vis-build/README.md b/config/vis/plugins/vis-build/README.md
new file mode 100644
index 0000000..23c4de0
--- /dev/null
+++ b/config/vis/plugins/vis-build/README.md
@@ -0,0 +1,24 @@
+# vis-build - asynchronously run (build) commands
+
+This is a simple `vis` plugin to asynchronously run any shell command using
+the `vis:communicate` API.
+
+I use the plugin to run build commands while keeping my editor usable.
+
+## Configuration
+
+By default, the `build` will execute `make(1)` in the current directory.
+The used build command can either be passed as first argument to `build` or
+is retrieved by calling the function `build.get_default_build_cmd`.
+
+So in order to change the default build command to ninja add this snippet to
+you `visrc.lua`:
+
+```lua
+local build = require('plugins/vis-build')
+build.get_default_build_cmd = function() return 'ninja' end
+```
+
+## Bindings
+
+`<M-b>` is bound to the `build` command.
diff --git a/config/vis/plugins/vis-build/init.lua b/config/vis/plugins/vis-build/init.lua
new file mode 100644
index 0000000..0558c39
--- /dev/null
+++ b/config/vis/plugins/vis-build/init.lua
@@ -0,0 +1,52 @@
+local M = {}
+M.get_default_build_cmd = function() return 'make' end
+
+local build_id = 0
+local builds = {}
+
+M.new_build = function(cmd)
+ build_id = build_id + 1
+ local build_name = 'build' .. build_id
+ local build_fd = vis:communicate(build_name, cmd)
+ local build = {fd = build_fd, out = '', err = '', cmd = cmd}
+ builds[build_name] = build
+end
+
+vis.events.subscribe(vis.events.PROCESS_RESPONSE,
+ function(name, event, code, msg)
+ local build = builds[name]
+ if not build then return end
+
+ if event == 'EXIT' or event == 'SIGNAL' then
+ if code ~= 0 then
+ vis:message('build: ' .. name .. ' cmd: ' .. build.cmd)
+ if event == 'EXIT' then
+ vis:message('failed with: ' .. code)
+ else
+ vis:message('got signal: ' .. code)
+ end
+ vis:message('stdout:\n' .. build.out)
+ vis:message('stderr:\n' .. build.err)
+ else
+ vis:info(name .. ' done')
+ end
+ builds[name] = nil
+ end
+
+ if event == 'STDOUT' then
+ build.out = build.out .. msg
+ elseif event == 'STDERR' then
+ build.err = build.err .. msg
+ end
+end)
+
+vis:command_register('build', function(argv)
+ M.new_build(argv[1] or M.get_default_build_cmd())
+end, 'Asynchronously build the current file or project')
+
+vis:map(vis.modes.NORMAL, '<M-b>', function()
+ vis:command('build')
+ return 0
+end, 'Asynchronously build the current file or project')
+
+return M
diff --git a/config/vis/plugins/vis-build/tools/check-format b/config/vis/plugins/vis-build/tools/check-format
new file mode 100755
index 0000000..392d023
--- /dev/null
+++ b/config/vis/plugins/vis-build/tools/check-format
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+LUA_FILE=$1
+lua-format "${LUA_FILE}" > "${LUA_FILE}.fmt"
+diff "${LUA_FILE}" "${LUA_FILE}.fmt"
+RET=$?
+rm "${LUA_FILE}.fmt"
+exit ${RET}
diff --git a/config/vis/visrc.lua b/config/vis/visrc.lua
index 462ccf9..e9c4160 100644
--- a/config/vis/visrc.lua
+++ b/config/vis/visrc.lua
@@ -21,12 +21,19 @@ if next(lspc) then
end
-local fzf_open = require('plugins/vis-fzf-open')
+local build = require('plugins/vis-build')
+build.get_default_build_cmd = function()
+ if os.getenv('PWD'):match('Unterricht') then
+ return "lesson-make"
+ end
+ return "make"
+end
+local fzf_open = require('plugins/vis-fzf-open')
fzf_open.fzf_path = "FZF_DEFAULT_COMMAND='rg --hidden -g !.git -l \"\"' fzf"
vis:map(vis.modes.NORMAL, ";o", function()
- vis:command('fzf')
+ vis:command('fzf')
end)
require('plugins/suw')