Created
November 12, 2021 09:00
-
-
Save dmitmel/ec3ff9845e4a1d142a627b3d2d3408c4 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# type: ignore | |
# Some code was taken from Localize-Me-Tools. Thank you, Satcher! | |
# <https://github.com/L-Sherry/Localize-Me-Tools/blob/d3d124af392da85422d303242e43673a817b360f/readliner.py>. | |
import os | |
import readline | |
from typing import List | |
import colorama | |
import pynvim | |
def main() -> None: | |
nvim: pynvim.Nvim = pynvim.attach("socket", path=os.environ["NVIM_LISTEN_ADDRESS"]) | |
try: | |
readline.read_init_file() | |
except FileNotFoundError: | |
pass | |
readline.parse_and_bind("tab: complete") | |
readline.clear_history() | |
for item in nvim.eval("""map(range(1, histnr('cmd')), "histget('cmd', v:val)")"""): | |
readline.add_history(item) | |
readline.set_auto_history(False) | |
def completer(text: str, state: int) -> List[str]: | |
result = nvim.funcs.getcompletion(readline.get_line_buffer(), "cmdline") | |
try: | |
return result[state] | |
except IndexError: | |
return None | |
readline.set_completer(completer) | |
print( | |
"Hi! Welcome to RPC REPL, attached to Neovim v{0[major]}.{0[minor]}.{0[patch]}".format( | |
nvim.metadata["version"] | |
) | |
) | |
interrupted = False | |
while True: | |
print(colorama.Style.RESET_ALL, end="") | |
try: | |
line = input(":") | |
except EOFError: | |
break | |
except KeyboardInterrupt: | |
print() | |
if not interrupted: | |
print("Press Ctrl-C again or Ctrl-D to exit") | |
interrupted = True | |
continue | |
else: | |
break | |
else: | |
interrupted = False | |
if len(line) == 0: | |
continue | |
nvim.funcs.histadd("cmd", line) | |
# Save exactly what Vim has recorded on the other side | |
readline.add_history(nvim.funcs.histget("cmd", -1)) | |
try: | |
output: str = nvim.command_output(line) | |
except pynvim.NvimError as error: | |
print(colorama.Fore.RED + str(error) + colorama.Fore.RESET) | |
continue | |
if len(output) > 0: | |
print(output) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment