@@ -477,54 +477,66 @@ def create_window():
477477 max_display = 0
478478
479479 create_window ()
480-
480+
481481 current_pos = 0
482482 start_idx = 0
483483
484484 while True :
485- history_window .clear ()
486- history_window .border ()
487- history_window .addstr (0 , 2 , " Command History - Use Up/Down arrows, Enter to select, Esc to cancel " , curses .A_BOLD )
488-
489- for i in range (max_display ):
490- idx = start_idx + i
491- if idx >= len (self .history ):
492- break
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 ] + "..."
493502
494- entry = self . history [ - ( idx + 1 )]
495- display_text = f" { idx + 1 } : { entry } "
496- if len ( display_text ) > window_width - 6 :
497- display_text = display_text [: window_width - 9 ] + "..."
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 )
498507
499- if i == current_pos :
500- history_window .addstr (i + 2 , 2 , display_text , curses .A_REVERSE )
501- else :
502- history_window .addstr (i + 2 , 2 , display_text )
503-
504- history_window .refresh ()
505-
506- key = history_window .getch ()
507-
508- if key == 27 :
509- return None
510- elif key == curses .KEY_UP :
511- if current_pos > 0 :
512- current_pos -= 1
513- elif start_idx > 0 :
514- start_idx -= 1
515- elif key == curses .KEY_DOWN :
516- if current_pos < max_display - 1 and start_idx + current_pos < len (self .history ) - 1 :
517- current_pos += 1
518- elif start_idx + max_display < len (self .history ):
519- start_idx += 1
520- elif key == curses .KEY_NPAGE :
521- start_idx = min (start_idx + max_display , len (self .history ) - max_display )
522- elif key == curses .KEY_PPAGE :
523- start_idx = max (0 , start_idx - max_display )
524- elif key == 10 or key == 13 :
525- idx = start_idx + current_pos
526- if idx < len (self .history ):
527- return self .history [- (idx + 1 )]
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 ()
528540
529541 return None
530542
0 commit comments