commit 8440af67b2c025920bdc6d7effa0da89122613b7
parent fa79b0831885aa7678210a030ffd1a786f90fba4
Author: William Casarin <jb55@jb55.com>
Date: Fri, 7 May 2021 08:50:14 -0700
fmtsafe
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
3 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/bin/fmtsafe b/bin/fmtsafe
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+cmd="$1"
+shift
+
+orig=$(mktemp)
+fmt=$(mktemp)
+
+function cleanup() {
+ rm -f "$orig" "$fmt"
+}
+
+trap cleanup exit
+
+cat > "$orig"
+
+<"$orig" $cmd "$@" > "$fmt" 2>/dev/null
+
+if [ $? -eq 0 ]; then
+ cat "$fmt"
+else
+ cat "$orig"
+fi
+
diff --git a/bin/gofmt-safe b/bin/gofmt-safe
@@ -1,16 +0,0 @@
-#!/usr/bin/env bash
-
-orig=$(mktemp)
-fmt=$(mktemp)
-
-cat > "$orig"
-
-<"$orig" gofmt "$@" > "$fmt" 2>/dev/null
-
-if [ $? -eq 0 ]; then
- cat "$fmt"
-else
- cat "$orig"
-fi
-
-rm -f "$orig" "$fmt"
diff --git a/dotfiles/.config/nvim/init.vim b/dotfiles/.config/nvim/init.vim
@@ -19,5 +19,7 @@ let mapleader = "\\"
let maplocalleader = "\\"
nmap <Leader>xda ma:%s/\s\+$//g<CR>`a
-autocmd FileType go autocmd BufWritePre <buffer> execute "normal! mz:mkview\<esc>:%!gofmt-safe\<esc>:loadview\<esc>`z"
+autocmd FileType go autocmd BufWritePre <buffer> execute "normal! mz:mkview\<esc>:%!fmtsafe gofmt\<esc>:loadview\<esc>`z"
+autocmd FileType rust autocmd BufWritePre <buffer> execute "normal! mz:mkview\<esc>:%!fmtsafe rustfmt\<esc>:loadview\<esc>`z"
+
autocmd BufEnter,BufNew *.gmi set wrap linebreak