From 7d3cb0d23db26dfee2ffc94c2044955066abc1be Mon Sep 17 00:00:00 2001 From: pancake Date: Fri, 10 May 2024 12:36:05 +0200 Subject: [PATCH] Implement -ed command like in r2 to edit r2ai.rc --- r2ai/interpreter.py | 3 +++ r2ai/main.py | 1 + r2ai/repl.py | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/r2ai/interpreter.py b/r2ai/interpreter.py index e962c40e..88e83882 100644 --- a/r2ai/interpreter.py +++ b/r2ai/interpreter.py @@ -576,6 +576,7 @@ def __init__(self): self.env["user.os"] = "" self.env["user.arch"] = "" self.env["user.cwd"] = "" + self.env["user.editor"] = "" self.env["user.plugins"] = f"{R2AI_HOMEDIR}/plugins" self.env["voice.lang"] = "en" self.env["voice.model"] = "base" @@ -769,6 +770,8 @@ def environment(self): kvs += "OS: " + self.env["user.os"] + "\n" if self.env["user.cwd"] != "": kvs += "CWD: " + self.env["user.cwd"] + "\n" + if self.env["user.editor"] != "": + kvs += "EDITOR: " + self.env["user.editor"] + "\n" if self.env["user.arch"] != "": kvs += "ARCH: " + self.env["user.arch"] + "\n" # info += f"[User Info]\nName: {username}\nCWD: {current_working_directory}\nOS: {operating_system}" diff --git a/r2ai/main.py b/r2ai/main.py index 3d87d7ee..6da5301f 100755 --- a/r2ai/main.py +++ b/r2ai/main.py @@ -78,6 +78,7 @@ def __main__(): except: traceback.print_exc() +# TODO: see repl.run_script as replacement def run_rcfile(): global ai try: diff --git a/r2ai/repl.py b/r2ai/repl.py index e08a4881..fb6e0c66 100644 --- a/r2ai/repl.py +++ b/r2ai/repl.py @@ -49,6 +49,7 @@ def r2_cmd(x): r2ai -k clear the screen r2ai -c [cmd] [query] run the given r2 command with the given query r2ai -e [k[=v]] set environment variable + r2ai -ed launch user.editor with ~/.r2ai.rc r2ai -f [file] load file and paste the output r2ai -h show this help (same as ?) r2ai -H ([var]) show path variables like it's done in r2 -H @@ -130,6 +131,16 @@ def r2ai_vars(ai, arg): for k in vs.keys(): print(k) +# copypasta from main.run_rcfile . todo avoid dup code +def run_script(ai, script): + try: + lines = slurp(script) + for line in lines.split("\n"): + if line.strip() != "": + runline(ai, line) + except: + pass + def runline(ai, usertext): global print global autoai @@ -211,6 +222,16 @@ def runline(ai, usertext): ai.env["data.path"] = f"{R2AI_HOMEDIR}/doc/" ai.env["chat.bubble"] = "true" runline(ai, f"-rf {R2AI_HOMEDIR}/doc/role/r2clippy.txt") + elif usertext.startswith("-ed"): + editor = "vim" # Defaults to the only real editor + if ai.env["user.editor"] != "": + editor = ai.env["user.editor"] + elif "EDITOR" in os.environ: + editor = os.environ["EDITOR"] + os.system(f"{editor} {R2AI_RCFILE}") + print("Reload? (y/N)") + if input() == "y": + run_script(ai, R2AI_RCFILE) elif usertext.startswith("-e"): if len(usertext) == 2: for k in ai.env.keys():