11import argparse
2+
3+ from db_connection import (connect , getKey , getMultipleKeys , listKeys ,
4+ listVersions )
5+ from rich .text import Text
26from textual import on
37from textual .app import App , ComposeResult
4- from textual .widgets import (
5- Button ,
6- Header ,
7- Footer ,
8- Input ,
9- DataTable ,
10- RichLog ,
11- )
8+ from textual .binding import Binding
129from textual .containers import Horizontal , Vertical
1310from textual .widget import Widget
14- from rich .text import Text
15- from db_connection import connect , listKeys , getKey , listVersions , getMultipleKeys
11+ from textual .widgets import Button , DataTable , Footer , Header , Input , RichLog
1612
1713# import logging
1814
@@ -53,7 +49,6 @@ def update_key(self, key):
5349 self .key = key
5450
5551 self .query_one ("#write-button" ).styles .visibility = "visible"
56- self .query_one ("#delete-button" ).styles .visibility = "visible"
5752
5853 log = self .query_one ("#key-info-label" )
5954
@@ -80,35 +75,48 @@ def write_key(self):
8075 def on_button_pressed (self , event ):
8176 if event .button .id == "write-button" :
8277 self .write_key ()
83- if event .button .id == "delete-button" :
84- self .delete_key ()
8578
8679 def compose (self ) -> ComposeResult :
8780 with Vertical ():
8881 yield RichLog (id = "key-info-label" , wrap = True )
8982 writeButton = Button (label = "Write data to out.bin" , id = "write-button" )
9083 writeButton .styles .visibility = "hidden"
9184 yield writeButton
92- deleteButton = Button (label = "Delete key" , id = "delete-button" )
93- deleteButton .styles .visibility = "hidden"
94- yield deleteButton
9585
9686
9787class FossilDBClient (App ):
88+
9889 """A Textual app to manage FossilDB databases."""
9990
10091 BINDINGS = [
10192 ("d" , "toggle_dark" , "Toggle dark mode" ),
10293 ("q" , "quit" , "Quit the client" ),
10394 ("r" , "refresh" , "Refresh the data" ),
104- ("k" , "show_next" , f"Show next { KEY_LIST_LIMIT } keys" ),
95+ Binding (
96+ "pagedown,j" ,
97+ "show_next" ,
98+ f"Show next { KEY_LIST_LIMIT } keys" ,
99+ priority = True ,
100+ show = True ,
101+ ),
102+ Binding (
103+ "pageup,k" ,
104+ "show_prev" ,
105+ f"Show previous { KEY_LIST_LIMIT } keys" ,
106+ priority = True ,
107+ show = True ,
108+ ),
109+ Binding ("down" , "next_key" , "Select the next key" , priority = True , show = False ),
110+ Binding ("up" , "prev_key" , "Select the previous key" , priority = True , show = False ),
105111 ]
106112
107113 after_key = ""
108114 prefix = ""
109115 collection = "volumeData"
110116 CSS_PATH = "client.tcss"
111117
118+ last_keys = ["" ]
119+
112120 def __init__ (self , stub , collection ):
113121 super ().__init__ ()
114122 self .stub = stub
@@ -158,7 +166,7 @@ def refresh_data(self) -> None:
158166 for i , key in enumerate (keys ):
159167 label = Text (str (i ), style = "#B0FC38 italic" )
160168 table .add_row (key , label = label )
161- self .last_key = keys [- 1 ]
169+ self .last_keys . append ( keys [- 1 ])
162170 table .focus ()
163171 except Exception as e :
164172 table .add_row ("Could not load keys: " + str (e ))
@@ -177,9 +185,39 @@ def action_refresh(self) -> None:
177185
178186 def action_show_next (self ) -> None :
179187 """An action to show the next KEY_LIST_LIMIT keys."""
180- self .after_key = self .last_key
188+ self .after_key = self .last_keys [ - 1 ]
181189 self .refresh_data ()
182190
191+ def action_show_prev (self ) -> None :
192+ """An action to show the previous KEY_LIST_LIMIT keys."""
193+ if len (self .last_keys ) > 2 :
194+ self .last_keys .pop ()
195+ self .last_keys .pop ()
196+ self .after_key = self .last_keys [- 1 ]
197+ self .refresh_data ()
198+
199+ def action_next_key (self ) -> None :
200+ """An action to select the next key."""
201+ table = self .query_one (DataTable )
202+ current_row = table .cursor_coordinate .row
203+ if current_row < len (table .rows ) - 1 :
204+ table .cursor_coordinate = (current_row + 1 , table .cursor_coordinate .column )
205+ else :
206+ self .action_show_next ()
207+
208+ def action_prev_key (self ) -> None :
209+ """An action to select the previous key."""
210+ table = self .query_one (DataTable )
211+ current_row = table .cursor_coordinate .row
212+ if current_row > 0 :
213+ table .cursor_coordinate = (current_row - 1 , table .cursor_coordinate .column )
214+ else :
215+ self .action_show_prev ()
216+ table .cursor_coordinate = (
217+ len (table .rows ) - 1 ,
218+ table .cursor_coordinate .column ,
219+ )
220+
183221
184222def init_argument_parser ():
185223 parser = argparse .ArgumentParser ()
0 commit comments