Gdbmtool VLANG Wrapper, FZF, and VIM
Consider this vim9script for Windows OS shown in the tweet below, it displays the files in the working directory, then the user can select and open them with the fzf fuzzy finder.
Tweet of Seve_py/1635577279014133762
This note shows an extended version of the previous VIM script written in VLANG (V programming language). While not strictly necessary, the file list passed to fzf will be saved in a database for demonstrating VLANG ability to wrap command line executables.
Gdbmtool VLANG WRAPPER
The gdbmtool utility allows you to view and modify an existing GDBM database or to create a new one. On Windows OS it can be installed from a MSYS2/MINGW-W64 terminal with:
pacman -S gdbm
The following program creates a new database in the %AppData% directory (i.e. C:\Users\user\AppData\Roaming on Windows OS) which stores the working directory filenames.
//gdbmtool wrapper
//Copyright © 2023 Seve Tessarin
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os
import cli
fn main(){
mut app := cli.Command{
name: 'gdbmtool_wrapper'
description: 'gdbmtool wrapper'
execute: fn (cmd cli.Command) ! {
println('Usage: gdbmtool_wrapper.exe [command]')
println('Commands:')
println('store\t save working directory filenames')
println('fetch\t fetch working directory filenames')
return
}
commands: [
cli.Command{
name: 'fetch'
execute: fn (cmd cli.Command) ! {
fetch()
return
}
},
cli.Command{
name: 'store'
execute: fn (cmd cli.Command) ! {
store()
return
}
},
]
}
app.setup()
app.parse(os.args)
}
fn fetch() {
appdata := os.config_dir() or {panic(err)}
mut flist := os.new_process("gdbmtool.exe $appdata\\vim.db fetch files")
flist.set_redirect_stdio()
flist.wait()
mut line := flist.stdout_read()
line = line.replace("[","")
line = line.replace("]","")
line = line.replace(', ' ,'\r\n')
println('$line')
}
fn store() {
files := os.ls('.') or {
println(err)
return
}
appdata := os.config_dir() or {panic(err)}
mut cmd := os.new_process("gdbmtool.exe $appdata\\vim.db store files '$files'")
cmd.run()
println("Directory filenames saved to db")
}
It can be compiled for a specific OS with:
v.exe gdbmtool_wrapper.v
and it produces the executable gdbmtool_wrapper.exe, which can be moved to a directory included in the PATH.
Note that the config_dir function returns the path to the user's configuration directory (depending on the platform).
VIM Integration
The previous vim9script can then be changed as follows:
vim9script
system('gdbmtool_wrapper.exe store')
var fn = system('gdbmtool_wrapper.exe fetch |fzf --preview "more < {}"')
execute 'vsplit ' .. fn
execute 'norm \<esc>'