Last active
August 16, 2023 06:04
-
-
Save Shamaoke/4f0f7675fa5fd70dbcb5c405982dec04 to your computer and use it in GitHub Desktop.
Display a list of numbers to choose from.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vim9script | |
## ## | |
# ::: Fzf Numbers ::: # | |
## ## | |
# | |
# Display a list of numbers to choose from. | |
# | |
# | |
# How to use | |
# ---------- | |
# 1. Create the `data.txt` file with the command: `seq 1 100000 > data.txt`; | |
# 2. Run Vim with the command: `vim -S fzf-numbers.vim`; | |
# 3. In the Vim command line run the command: `FzfNumbers`; | |
# 4. In the pop-up window, observe the error message: `executing job failed: Argument list too long`; | |
# (5). Close the pop-up window with the `ZZ` hotkey. | |
# | |
echomsg 'Fzf Numbers has been loaded!' | |
var tmp_file: string = tempname() | |
var width: number = (&columns * 0.8)->ceil()->float2nr() | |
var height: number = (&lines * 0.8)->ceil()->float2nr() | |
var command = [ | |
'fzf', | |
'--expect=esc,enter' | |
] | |
var popup_options = { | |
minwidth: width, | |
minheight: height, | |
title: '─{{{ Fzf Numbers }}}─', | |
border: [1, 1, 1, 1], | |
borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└'], | |
} | |
def ExitCallback(job: job, status: number): void | |
execute('quit') | |
enddef | |
def CloseCallback(channel: channel): void | |
var [key, value] = readfile(tmp_file) | |
delete(tmp_file) | |
if key == 'enter' | |
echomsg value | |
else | |
echomsg '∅' | |
endif | |
enddef | |
var term_options = { | |
'hidden': true, | |
'out_io': 'file', | |
'out_name': tmp_file, | |
# 'exit_cb': ExitCallback, | |
'close_cb': CloseCallback | |
} | |
def FzfNumbers(): void | |
var fzf_default_command = $FZF_DEFAULT_COMMAND | |
var data = readfile('data.txt')->filter((_, v) => v->str2nr() % 2 == 0)->join('\n') | |
$FZF_DEFAULT_COMMAND = $"echo '{data}'" | |
try | |
term_start(command, term_options)->popup_create(popup_options) | |
finally | |
$FZF_DEFAULT_COMMAND = fzf_default_command | |
endtry | |
enddef | |
command FzfNumbers FzfNumbers() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment