From f6626aa9b55a5a16f342ebb24419015514bdc7d6 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 2 Oct 2024 02:01:23 +0200 Subject: [PATCH] Implement ! and !# for listing and re-running history lines --- r2ai/repl.py | 23 +++++++++++++++++++++-- r2ai/tab.py | 13 ++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/r2ai/repl.py b/r2ai/repl.py index b94ccaf..1cde381 100644 --- a/r2ai/repl.py +++ b/r2ai/repl.py @@ -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 @@ -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 @@ -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()) diff --git a/r2ai/tab.py b/r2ai/tab.py index 3c06c4d..0bea582 100644 --- a/r2ai/tab.py +++ b/r2ai/tab.py @@ -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 @@ -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)