Skip to content

Commit

Permalink
Implement -ed command like in r2 to edit r2ai.rc
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed May 10, 2024
1 parent bda670f commit 7d3cb0d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions r2ai/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}"
Expand Down
1 change: 1 addition & 0 deletions r2ai/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __main__():
except:
traceback.print_exc()

# TODO: see repl.run_script as replacement
def run_rcfile():
global ai
try:
Expand Down
21 changes: 21 additions & 0 deletions r2ai/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 7d3cb0d

Please sign in to comment.