@@ -3452,6 +3452,7 @@ def __init__(
3452
3452
logger .error (f'{ keys_file = } ' )
3453
3453
with open (keys_file , 'r' , encoding = 'utf-8' ) as f :
3454
3454
self ._classes = json .load (f )
3455
+ self ._needs_update = False
3455
3456
3456
3457
3457
3458
def item (self , an_item_id = None ):
@@ -3532,12 +3533,12 @@ def _focus_previous(self):
3532
3533
def _go_top (self ):
3533
3534
self ._start = 0
3534
3535
self ._selection = 1
3535
- self .show ()
3536
+ self ._needs_update = True
3536
3537
3537
3538
def _go_bottom (self ):
3538
3539
self ._selection = len (self ._list ) - 1
3539
3540
self ._start = self ._selection - self ._number_of_lines + 1
3540
- self .show ()
3541
+ self ._needs_update = True
3541
3542
3542
3543
def _go_down (self , step = 1 ):
3543
3544
logger .error (f'go_down: { step = } ' )
@@ -3555,7 +3556,7 @@ def _go_down(self, step=1):
3555
3556
if next_selection >= len (self ._list ):
3556
3557
self ._selection = 1
3557
3558
self ._start = 0
3558
- self .show ()
3559
+ self ._needs_update = True
3559
3560
return
3560
3561
line = next_selection - self ._start + 2
3561
3562
logger .error (f'{ line = } , { self ._number_of_lines = } ' )
@@ -3564,7 +3565,7 @@ def _go_down(self, step=1):
3564
3565
while line > self ._number_of_lines + 1 :
3565
3566
self ._start += (next_selection - self ._selection )
3566
3567
self ._selection = next_selection
3567
- self .show ()
3568
+ self ._needs_update = True
3568
3569
return
3569
3570
if 2 < line <= self ._number_of_lines + 1 :
3570
3571
logger .error ('=== between' )
@@ -3591,7 +3592,7 @@ def _go_up(self, step=1):
3591
3592
if next_selection < 0 :
3592
3593
self ._selection = len (self ._list ) - 1
3593
3594
self ._start = len (self ._list ) - self ._number_of_lines
3594
- self .show ()
3595
+ self ._needs_update = True
3595
3596
return
3596
3597
line = next_selection - self ._start - 2
3597
3598
line = next_selection - self ._start + 1
@@ -3601,7 +3602,7 @@ def _go_up(self, step=1):
3601
3602
if self ._selection in self ._headers :
3602
3603
self ._selection = - 1
3603
3604
self ._start = self ._selection
3604
- self .show ()
3605
+ self ._needs_update = True
3605
3606
return
3606
3607
if 1 <= line <= self ._number_of_lines :
3607
3608
logger .error ('=== between' )
@@ -3648,28 +3649,40 @@ def _init_win(self):
3648
3649
self ._make_selection_visible ()
3649
3650
3650
3651
def _make_selection_visible (self ):
3651
- chk = self ._start - self ._selection
3652
- logger .error (f'{ self ._start = } ' )
3653
- logger .error (f'{ self ._selection = } ' )
3654
- logger .error (f'{ chk = } ' )
3652
+ # Check if the current selection is already visible
3653
+ if self ._start <= self ._selection < self ._start + self ._number_of_lines :
3654
+ # Ensure that we use the expanded window space effectively
3655
+ if self ._start + self ._number_of_lines > len (self ._list ):
3656
+ # Adjust `self._start` to fill the screen if the end of the list is reached
3657
+ self ._start = max (0 , len (self ._list ) - self ._number_of_lines )
3658
+ return
3659
+
3660
+ # Selection is not visible, adjust `self._start` as per existing behavior
3661
+ chk = self ._start - self ._selection
3662
+
3663
+ # Case 1: Selection is near the bottom of the list
3655
3664
if self ._selection > len (self ._list ) - self ._number_of_lines :
3656
- self ._start = len (self ._list ) - self ._number_of_lines
3657
- else :
3658
- if chk < 0 :
3659
- # start is after selection
3660
- self ._start = self ._selection
3661
- if (self ._start - 1 ) in self ._headers :
3662
- self ._start -= 1
3663
- else :
3664
- # start is before selection
3665
- # is it too far?
3666
- if chk > self ._number_of_lines :
3667
- self ._start = int ((self ._selection - self ._number_of_lines ) / 2 )
3665
+ self ._start = max (0 , len (self ._list ) - self ._number_of_lines )
3666
+
3667
+ # Case 2: Selection is below the visible range
3668
+ elif chk < 0 :
3669
+ # Try to center the selection in the visible area
3670
+ proposed_start = self ._selection - self ._number_of_lines // 2
3671
+
3672
+ # Ensure no empty lines at the bottom
3673
+ self ._start = min (proposed_start , len (self ._list ) - self ._number_of_lines )
3674
+
3675
+ # Ensure `self._start` is not negative
3676
+ self ._start = max (0 , self ._start )
3677
+
3678
+ # Case 3: Selection is above the visible range
3679
+ elif chk > self ._number_of_lines :
3680
+ self ._start = max (0 , int ((self ._selection - self ._number_of_lines ) / 2 ))
3668
3681
3669
3682
def _go_to_line (self , a_line ):
3670
3683
self ._selection = a_line
3671
3684
self ._make_selection_visible ()
3672
- self .show ()
3685
+ self ._needs_update = True
3673
3686
3674
3687
def _select_line (self , a_line ):
3675
3688
self ._win .chgat (a_line , 2 , self .maxX - 4 , curses .color_pair (6 ))
@@ -3679,6 +3692,7 @@ def _unselect_line(self, a_line):
3679
3692
self ._win .chgat (a_line , self ._max_length , self .maxX - self ._max_length - 2 , curses .color_pair (4 ))
3680
3693
3681
3694
def show (self , parent = None ):
3695
+ logger .error ('def show()' )
3682
3696
if parent is not None :
3683
3697
self ._parent = parent
3684
3698
self ._init_win ()
@@ -3942,6 +3956,7 @@ def keypress(self, char):
3942
3956
1: Continue
3943
3957
2: Display help
3944
3958
'''
3959
+ self ._needs_update = False
3945
3960
if char == ord ('0' ):
3946
3961
if self .existing_conflict :
3947
3962
self ._editing = False
@@ -4006,15 +4021,15 @@ def keypress(self, char):
4006
4021
elif char == ord ('x' ):
4007
4022
self ._list [self ._selection ][3 ] = self ._list [self ._selection ][2 ]
4008
4023
self ._list [self ._selection ][6 ] = self ._list [self ._selection ][5 ]
4009
- self .show ()
4024
+ self ._needs_update = True
4010
4025
elif char == ord (']' ):
4011
4026
self ._get_after_header ()
4012
4027
self ._make_selection_visible ()
4013
- self .show ()
4028
+ self ._needs_update = True
4014
4029
elif char == ord ('[' ):
4015
4030
self ._get_after_header (next = False )
4016
4031
self ._make_selection_visible ()
4017
- self .show ()
4032
+ self ._needs_update = True
4018
4033
elif char in (curses .KEY_HOME , kbkey ['g' ]):
4019
4034
if self ._focus == 0 :
4020
4035
self ._go_top ()
@@ -4071,6 +4086,10 @@ def keypress(self, char):
4071
4086
logger .error (f'{ self ._start = } ' )
4072
4087
logger .error (f'{ self ._selection = } ' )
4073
4088
logger .error ('line = {}' .format (self ._selection - self ._start + 2 ))
4089
+
4090
+ # Centralized UI update
4091
+ if self ._needs_update :
4092
+ self .show ()
4074
4093
return 1
4075
4094
4076
4095
# pymode:lint_ignore=W901
0 commit comments