Skip to content

Commit

Permalink
Implement ! and !# for listing and re-running history lines
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Oct 2, 2024
1 parent 9fc93cd commit f6626aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
23 changes: 21 additions & 2 deletions r2ai/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
import os

from .tab import tab_init, tab_hist, tab_write, tab_evals
from .tab import tab_init, tab_hist, tab_write, tab_evals, tab_list
from .interpreter import Interpreter
from .pipe import have_rlang, r2lang, r2singleton
from r2ai import bubble, LOGGER
Expand Down Expand Up @@ -53,6 +53,7 @@ def r2_cmd(x):
r2ai ?e [msg] echo a message
r2ai ?t [query] run an query and show it's timing
r2ai !ls run a system command
r2ai ! show prompt history
r2ai -a query with audio voice
r2ai -A enter the voice chat loop
r2ai -k clear the screen
Expand Down Expand Up @@ -426,7 +427,25 @@ def runline(ai, usertext):
tag = "```\n" # TEXT, INPUT ..
ai.chat(f"{que}:\n{tag}\n{res}\n{tag}\n")
elif usertext[0] == "!":
os.system(usertext[1:])
import shutil
columns, rows = shutil.get_terminal_size()
if len(usertext) == 1:
count = 0
for item in tab_list():
print(item, file=sys.stderr)
count = count + 1
if count == rows - 1:
print("-- More -- (q)")
count = 0
if input() == 'q':
break
else:
n = int(usertext[1:])
if n > 0:
items = tab_list()
runline(ai, items[n - 1])
else:
os.system(usertext[1:])
elif usertext[0] == ".":
#if len(usertext) > 1 and usertext[1] == ".": # ".." - run user plugins
# runplugin(ai, usertext[2:].strip())
Expand Down
13 changes: 12 additions & 1 deletion r2ai/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def display_matches(self, substitution, matches, longest_match_length):
have_readline = True
pass # readline not available

def tab_hist(text):
def tab_hist():
if not have_readline:
print("Cannot find readline", file=sys.stderr)
return False
Expand All @@ -119,6 +119,17 @@ def tab_write():
return False
readline.write_history_file(R2AI_HISTFILE)

def tab_list():
global readline
if not have_readline:
return []
amount = readline.get_current_history_length()
res = []
for i in range(1, amount):
item = readline.get_history_item(i)
res.append(f"{i} {item}")
return res

def tab_init():
if not have_readline:
print("Cannot find readline", file=sys.stderr)
Expand Down

0 comments on commit f6626aa

Please sign in to comment.