Skip to content
This repository was archived by the owner on Dec 29, 2025. It is now read-only.

Commit 5e20318

Browse files
authored
Merge pull request #36 from tahakcbg/feature/add-last-command
Add 'last' command
2 parents a21ab33 + 1492b50 commit 5e20318

File tree

1 file changed

+115
-1
lines changed

1 file changed

+115
-1
lines changed

poly.py

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,100 @@ def show_history(self):
446446
for i, entry in enumerate(self.history):
447447
self.add(f"{i + 1}: {entry}")
448448

449+
def show_last_commands(self, stdscr):
450+
if not self.history:
451+
self.add("No history available.")
452+
return None
453+
454+
def create_window():
455+
nonlocal history_window, window_height, window_width, start_y, start_x, max_display
456+
height, width = stdscr.getmaxyx()
457+
458+
window_height = min(len(self.history) + 4, height - 4)
459+
window_width = min(width - 10, 80)
460+
461+
window_height = max(window_height, 6)
462+
window_width = max(window_width, 30)
463+
464+
start_y = max(0, (height - window_height) // 2)
465+
start_x = max(0, (width - window_width) // 2)
466+
467+
history_window = curses.newwin(window_height, window_width, start_y, start_x)
468+
history_window.keypad(True)
469+
470+
max_display = window_height - 4
471+
472+
history_window = None
473+
window_height = 0
474+
window_width = 0
475+
start_y = 0
476+
start_x = 0
477+
max_display = 0
478+
479+
create_window()
480+
481+
current_pos = 0
482+
start_idx = 0
483+
484+
while True:
485+
try:
486+
history_window.clear()
487+
history_window.border()
488+
title = " Command History - Use Up/Down arrows, Enter to select, Esc to cancel "
489+
if len(title) > window_width - 4:
490+
title = " History - Enter:Select Esc:Cancel "
491+
history_window.addstr(0, max(1, (window_width - len(title)) // 2), title, curses.A_BOLD)
492+
493+
for i in range(max_display):
494+
idx = start_idx + i
495+
if idx >= len(self.history):
496+
break
497+
498+
entry = self.history[-(idx+1)]
499+
display_text = f"{idx+1}: {entry}"
500+
if len(display_text) > window_width - 6:
501+
display_text = display_text[:window_width - 9] + "..."
502+
503+
if i == current_pos:
504+
history_window.addstr(i + 2, 2, display_text, curses.A_REVERSE)
505+
else:
506+
history_window.addstr(i + 2, 2, display_text)
507+
508+
history_window.refresh()
509+
510+
key = history_window.getch()
511+
512+
if key == 27:
513+
return None
514+
elif key == curses.KEY_RESIZE:
515+
curses.update_lines_cols()
516+
create_window()
517+
max_display = max(1, max_display)
518+
current_pos = min(current_pos, max_display - 1)
519+
elif key == curses.KEY_UP:
520+
if current_pos > 0:
521+
current_pos -= 1
522+
elif start_idx > 0:
523+
start_idx -= 1
524+
elif key == curses.KEY_DOWN:
525+
if current_pos < max_display - 1 and start_idx + current_pos < len(self.history) - 1:
526+
current_pos += 1
527+
elif start_idx + max_display < len(self.history):
528+
start_idx += 1
529+
elif key == curses.KEY_NPAGE:
530+
start_idx = min(start_idx + max_display, len(self.history) - max_display)
531+
start_idx = max(0, start_idx)
532+
elif key == curses.KEY_PPAGE:
533+
start_idx = max(0, start_idx - max_display)
534+
elif key == 10 or key == 13:
535+
idx = start_idx + current_pos
536+
if idx < len(self.history):
537+
return self.history[-(idx+1)]
538+
except curses.error:
539+
create_window()
540+
541+
return None
542+
449543
def set_mode(self, mode):
450544
m = mode.lower()
451545
if m not in ('poly', 'win', 'pws', 'lnx'):
@@ -638,7 +732,7 @@ def get_completions(inp, tabs, idx):
638732
else:
639733
base, token = inp[:i+1], inp[i+1:]
640734
cmd = inp.strip().split(' ', 1)[0].lower()
641-
commands = ["tab", "run", "cd", "cwd", "files", "makedir", "deldir", "remove", "echo", "make", "download", "alias", "tree", "history", "color", "clear", "read", "move", "copy", "kill", "variable", "shutdown", "restart"]
735+
commands = ["tab", "run", "cd", "cwd", "files", "makedir", "deldir", "remove", "echo", "make", "download", "alias", "tree", "history", "color", "clear", "read", "move", "copy", "kill", "variable", "shutdown", "restart", "last"]
642736
for command in CUSTOM_COMMANDS.keys():
643737
if not command.startswith("__"):
644738
commands.append(command)
@@ -803,6 +897,26 @@ def handle_single_command(cmd_line, tabs, current):
803897
if lc == "history":
804898
tabs[current].show_history()
805899
return current, False
900+
if lc == "last":
901+
curses.def_prog_mode()
902+
curses.endwin()
903+
904+
stdscr = curses.initscr()
905+
curses.start_color()
906+
curses.cbreak()
907+
curses.noecho()
908+
stdscr.keypad(True)
909+
910+
try:
911+
selected_cmd = tabs[current].show_last_commands(stdscr)
912+
finally:
913+
curses.endwin()
914+
curses.reset_prog_mode()
915+
stdscr.refresh()
916+
917+
if selected_cmd:
918+
tabs[current].add(f"> {selected_cmd}")
919+
return handle_command(selected_cmd, tabs, current)
806920
if lc == "files":
807921
if rest:
808922
tabs[current].files(rest)

0 commit comments

Comments
 (0)