@@ -57,7 +57,9 @@ def __init__(
5757 # Added for efficiency (no need to traverse unless necessary). Updated
5858 # from the main thread always, so no need for locking.
5959 self .obj_list_changed : bool = True
60- # This field presents the idx of the currently selected object in the
60+ # This field is a list with two elements. The first is the index of
61+ # the currently selected object (0-indexed). The second is the
62+ # index of the currently selected object in the
6163 # list of objects where each object is counted with length of characters
6264 # in name + 1 multiplicity. So for example a list of objects
6365 # A
@@ -68,7 +70,9 @@ def __init__(
6870 # This is used to track the cursor position in the sidebar document
6971 # It is recorded here, rather than elsewhere because we can track it
7072 # here far more efficiently (select_next, and select_previous).
71- self ._selected_obj_idx = 0
73+ # It is important that all methods of this class that manipulate the
74+ # currently selected object also update this index.
75+ self ._selected_obj_idx = [0 , 0 ]
7276 dsns = list (datasources ().keys ())
7377 if len (dsns ) < 1 :
7478 sys .exit ("No datasources found ... exiting." )
@@ -103,16 +107,35 @@ def selected_object(self) -> myDBObject:
103107 def selected_object (self , obj ) -> None :
104108 """ Avoid using / computationally expensive.
105109 Instead try using select_next / select_previous if possible.
110+ Will update _selected_obj_idx appropriately.
106111 """
112+ cursor = 0
107113 idx = 0
108114 o = self .obj_list [0 ]
109115 self ._selected_object = obj
110116 while o is not self ._selected_object :
111117 if not o .next_object :
112118 raise IndexError
113- idx += len (o .name ) + 1
119+ cursor += len (o .name ) + 1
120+ idx += 1
114121 o = o .next_object
115- self ._selected_obj_idx = idx
122+ self ._selected_obj_idx = [idx , cursor ]
123+
124+ def select (self , idx ) -> None :
125+ """ Select the [i]-th object in the list. Will also update
126+ _selected_obj_idx appropriately.
127+ """
128+ counter = 0
129+ cursor = 0
130+ o = self .obj_list [0 ]
131+ while counter < idx :
132+ if not o .next_object :
133+ raise IndexError
134+ counter += 1
135+ cursor += len (o .name ) + 1
136+ o = o .next_object
137+ self ._selected_object = o
138+ self ._selected_obj_idx = [idx , cursor ]
116139
117140 @property
118141 def selected_object_idx (self ):
@@ -272,14 +295,17 @@ def _(event):
272295 inc = len (self .selected_object .name ) + 1 # newline character
273296 if obj is self .obj_list [0 ]:
274297 idx = 0
298+ cursor = 0
275299 while obj is not self ._selected_object :
276300 if not obj .next_object :
277301 raise IndexError
278- idx += len (obj .name ) + 1
302+ cursor += len (obj .name ) + 1
303+ idx += 1
279304 obj = obj .next_object
280- self ._selected_obj_idx = idx
305+ self ._selected_obj_idx = [ idx , cursor ]
281306 else :
282- self ._selected_obj_idx -= inc
307+ self ._selected_obj_idx [0 ] -= 1
308+ self ._selected_obj_idx [1 ] -= inc
283309
284310 @kb .add ("down" , filter = sidebar_visible )
285311 @kb .add ("c-n" , filter = sidebar_visible )
@@ -289,9 +315,10 @@ def _(event):
289315 inc = len (self .selected_object .name ) + 1 # newline character
290316 self .select_next ()
291317 if self .selected_object is self .obj_list [0 ]:
292- self ._selected_obj_idx = 0
318+ self ._selected_obj_idx = [ 0 , 0 ]
293319 else :
294- self ._selected_obj_idx += inc
320+ self ._selected_obj_idx [0 ] += 1
321+ self ._selected_obj_idx [1 ] += inc
295322
296323 @kb .add ("enter" , filter = sidebar_visible )
297324 def _ (event ):
0 commit comments