listpick 0.1.13.49__py3-none-any.whl → 0.1.13.51__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
listpick/lpapp2.py ADDED
@@ -0,0 +1,3152 @@
1
+ #!/bin/python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ listpick_app.py
5
+ Set up environment to parse command-line arguments and run a Picker.
6
+
7
+ Author: GrimAndGreedy
8
+ License: MIT
9
+ """
10
+
11
+ import curses
12
+ import re
13
+ import os
14
+ import subprocess
15
+ import argparse
16
+ import time
17
+ from wcwidth import wcswidth
18
+ from typing import Callable, Optional, Tuple, Dict
19
+ import json
20
+ import threading
21
+ import string
22
+ import logging
23
+
24
+ from listpick.ui.picker_colours import get_colours, get_help_colours, get_notification_colours, get_theme_count, get_fallback_colours
25
+ from listpick.utils.options_selectors import default_option_input, output_file_option_selector, default_option_selector
26
+ from listpick.utils.table_to_list_of_lists import *
27
+ from listpick.utils.utils import *
28
+ from listpick.utils.sorting import *
29
+ from listpick.utils.filtering import *
30
+ from listpick.ui.input_field import *
31
+ from listpick.utils.clipboard_operations import *
32
+ from listpick.utils.paste_operations import *
33
+ from listpick.utils.searching import search
34
+ from listpick.ui.help_screen import help_lines
35
+ from listpick.ui.keys import picker_keys, notification_keys, options_keys, help_keys
36
+ from listpick.utils.generate_data import generate_picker_data
37
+ from listpick.utils.dump import dump_state, load_state, dump_data
38
+ from listpick.ui.build_help import build_help_rows
39
+ from listpick.ui.footer import StandardFooter, CompactFooter, NoFooter
40
+ from listpick.utils.picker_log import setup_logger
41
+
42
+
43
+ try:
44
+ from tmp.data_stuff import test_items, test_highlights, test_header
45
+ except:
46
+ test_items, test_highlights, test_header = [], [], []
47
+
48
+ COLOURS_SET = False
49
+ help_colours, notification_colours = {}, {}
50
+
51
+ class Command:
52
+ def __init__(self, command_type, command_value):
53
+ self.command_type = command_type
54
+ self.command_value = command_value
55
+
56
+ class Picker:
57
+ def __init__(self,
58
+ stdscr: curses.window,
59
+ items: list[list[str]] = [],
60
+ cursor_pos: int = 0,
61
+ colours: dict = get_colours(0),
62
+ colour_theme_number: int = 0,
63
+ max_selected: int = -1,
64
+ top_gap: int =0,
65
+ title: str ="Picker",
66
+ header: list =[],
67
+ max_column_width: int =70,
68
+ clear_on_start: bool = False,
69
+
70
+ auto_refresh: bool =False,
71
+ timer: float = 5,
72
+
73
+ get_new_data: bool =False, # Whether we can get new data
74
+ refresh_function: Optional[Callable] = lambda: [], # The function with which we get new data
75
+ get_data_startup: bool =False, # Whether we should get data at statrup
76
+ track_entries_upon_refresh: bool = True,
77
+ pin_cursor: bool = False,
78
+ id_column: int = 0,
79
+
80
+ unselectable_indices: list =[],
81
+ highlights: list =[],
82
+ highlights_hide: bool =False,
83
+ number_columns: bool =True,
84
+ column_widths: list = [],
85
+ column_indices: list = [],
86
+
87
+
88
+ current_row : int = 0,
89
+ current_page : int = 0,
90
+ is_selecting : bool = False,
91
+ is_deselecting : int = False,
92
+ start_selection: int = -1,
93
+ start_selection_col: int = -1,
94
+ end_selection: int = -1,
95
+ user_opts : str = "",
96
+ options_list: list[str] = [],
97
+ user_settings : str = "",
98
+ separator : str = " ",
99
+ search_query : str = "",
100
+ search_count : int = 0,
101
+ search_index : int = 0,
102
+ filter_query : str = "",
103
+ hidden_columns: list = [],
104
+ indexed_items: list[Tuple[int, list[str]]] = [],
105
+ scroll_bar : int = True,
106
+
107
+ selections: dict = {},
108
+ cell_selections: dict[tuple[int,int], bool] = {},
109
+ highlight_full_row: bool =False,
110
+ cell_cursor: bool = False,
111
+
112
+ items_per_page : int = -1,
113
+ sort_method : int = 0,
114
+ SORT_METHODS: list[str] = ['Orig', 'lex', 'LEX', 'alnum', 'ALNUM', 'time', 'num', 'size'],
115
+ sort_reverse: list[bool] = [False],
116
+ selected_column: int = 0,
117
+ sort_column : int = 0,
118
+
119
+ columns_sort_method: list[int] = [0],
120
+ key_chain: str = "",
121
+ last_key: Optional[str] = None,
122
+
123
+ paginate: bool =False,
124
+ cancel_is_back: bool = False,
125
+ mode_index: int =0,
126
+ modes: list[dict] = [],
127
+ display_modes: bool =False,
128
+ require_option: list=[],
129
+ require_option_default: list=[],
130
+ option_functions: list[Callable[..., Tuple[bool, str]]] = [],
131
+ default_option_function: Callable[..., Tuple[bool, str]] = default_option_input,
132
+ disabled_keys: list=[],
133
+
134
+ show_header: bool = True,
135
+ show_row_header: bool = False,
136
+ show_footer: bool =True,
137
+ footer_style: int = 0,
138
+ footer_string: str="",
139
+ footer_string_auto_refresh: bool=False,
140
+ footer_string_refresh_function: Optional[Callable] = None,
141
+ footer_timer: float=1,
142
+ get_footer_string_startup=False,
143
+ unicode_char_width: bool = True,
144
+
145
+ colours_start: int =0,
146
+ colours_end: int =-1,
147
+ reset_colours: bool = True,
148
+ key_remappings: dict = {},
149
+ keys_dict:dict = picker_keys,
150
+ display_infobox : bool = False,
151
+ infobox_items: list[list[str]] = [],
152
+ infobox_title: str = "",
153
+ display_only: bool = False,
154
+
155
+ editable_columns: list[int] = [],
156
+ editable_by_default: bool = True,
157
+
158
+ centre_in_terminal: bool = False,
159
+ centre_in_terminal_vertical: bool = False,
160
+ centre_in_cols: bool = False,
161
+
162
+ startup_notification:str = "",
163
+
164
+ leftmost_column: int = 0,
165
+ leftmost_char: int = 0,
166
+
167
+
168
+ history_filter_and_search: list[str] = [],
169
+ history_opts: list[str] = [],
170
+ history_settings: list[str] = [],
171
+ history_edits: list[str] = [],
172
+ history_pipes: list[str] = [],
173
+ debug: bool = False,
174
+ debug_level: int = 1,
175
+
176
+ ):
177
+ self.stdscr = stdscr
178
+ self.items = items
179
+ self.cursor_pos = cursor_pos
180
+ self.colours = colours
181
+ self.colour_theme_number = colour_theme_number
182
+ self.max_selected = max_selected
183
+ self.top_gap = top_gap
184
+ self.title = title
185
+ self.header = header
186
+ self.max_column_width = max_column_width
187
+ self.clear_on_start = clear_on_start
188
+
189
+ self.auto_refresh = auto_refresh
190
+ self.timer = timer
191
+
192
+ self.get_new_data = get_new_data
193
+ self.refresh_function = refresh_function
194
+ self.get_data_startup = get_data_startup
195
+ self.track_entries_upon_refresh = track_entries_upon_refresh
196
+ self.pin_cursor = pin_cursor
197
+ self.id_column = id_column
198
+
199
+ self.unselectable_indices = unselectable_indices
200
+ self.highlights = highlights
201
+ self.highlights_hide = highlights_hide
202
+ self.number_columns = number_columns
203
+ self.column_widths, = [],
204
+ self.column_indices, = [],
205
+
206
+
207
+ self.current_row = current_row
208
+ self.current_page = current_page
209
+ self.is_selecting = is_selecting
210
+ self.is_deselecting = is_deselecting
211
+ self.start_selection = start_selection
212
+ self.start_selection_col = start_selection_col
213
+ self.end_selection = end_selection
214
+ self.user_opts = user_opts
215
+ self.options_list = options_list
216
+ self.user_settings = user_settings
217
+ self.separator = separator
218
+ self.search_query = search_query
219
+ self.search_count = search_count
220
+ self.search_index = search_index
221
+ self.filter_query = filter_query
222
+ self.hidden_columns = hidden_columns
223
+ self.indexed_items = indexed_items
224
+ self.scroll_bar = scroll_bar
225
+
226
+ self.selections = selections
227
+ self.cell_selections = cell_selections
228
+ self.highlight_full_row = highlight_full_row
229
+ self.cell_cursor = cell_cursor
230
+
231
+ self.items_per_page = items_per_page
232
+ self.sort_method = sort_method
233
+ self.sort_reverse = sort_reverse
234
+ self.selected_column = selected_column
235
+ self.sort_column = sort_column
236
+ self.columns_sort_method = columns_sort_method
237
+ self.key_chain = key_chain
238
+ self.last_key = last_key
239
+
240
+ self.paginate = paginate
241
+ self.cancel_is_back = cancel_is_back
242
+ self.mode_index = mode_index
243
+ self.modes = modes
244
+ self.display_modes = display_modes
245
+ self.require_option = require_option
246
+ self.require_option_default = require_option_default
247
+ self.option_functions = option_functions
248
+ self.default_option_function = default_option_function
249
+ self.disabled_keys = disabled_keys
250
+
251
+ self.show_header = show_header
252
+ self.show_row_header = show_row_header
253
+ self.show_footer = show_footer
254
+ self.footer_style = footer_style
255
+ self.footer_string = footer_string
256
+ self.footer_string_auto_refresh = footer_string_auto_refresh
257
+ self.footer_string_refresh_function = footer_string_refresh_function
258
+ self.footer_timer = footer_timer
259
+ self.get_footer_string_startup = get_footer_string_startup,
260
+ self.unicode_char_width = unicode_char_width
261
+
262
+
263
+ self.colours_start = colours_start
264
+ self.colours_end = colours_end
265
+ self.reset_colours = reset_colours
266
+ self.key_remappings = key_remappings
267
+ self.keys_dict = keys_dict
268
+ self.display_infobox = display_infobox
269
+ self.infobox_items = infobox_items
270
+ self.infobox_title = infobox_title
271
+ self.display_only = display_only
272
+
273
+ self.editable_columns = editable_columns
274
+ self.editable_by_default = editable_by_default
275
+
276
+ self.centre_in_terminal = centre_in_terminal
277
+ self.centre_in_terminal_vertical = centre_in_terminal_vertical
278
+ self.centre_in_cols = centre_in_cols
279
+
280
+ self.startup_notification = startup_notification
281
+
282
+
283
+ self.registers = {}
284
+
285
+ self.SORT_METHODS = SORT_METHODS
286
+ self.command_stack = []
287
+ self.leftmost_column = leftmost_column
288
+ self.leftmost_char = leftmost_char
289
+
290
+
291
+ # Refresh function variables
292
+ self.data_refreshed = False
293
+ self.refreshing_data = False
294
+ self.data_lock = threading.Lock()
295
+ self.data_ready = False
296
+ self.cursor_pos_id = 0
297
+ self.cursor_pos_prev = 0
298
+ self.ids = []
299
+ self.ids_tuples = []
300
+ self.selected_cells_by_row = {}
301
+
302
+ # History variables
303
+ self.history_filter_and_search = history_filter_and_search
304
+ self.history_pipes = history_pipes
305
+ self.history_opts = history_opts
306
+ self.history_settings = history_settings
307
+ self.history_edits = history_edits
308
+
309
+
310
+
311
+
312
+ self.debug = debug
313
+ self.debug_level = debug_level
314
+
315
+
316
+ self.initialise_picker_state(reset_colours=self.reset_colours)
317
+
318
+
319
+ # Note: We have to set the footer after initialising the picker state so that the footer can use the get_function_data method
320
+ self.footer_options = [StandardFooter(self.stdscr, colours_start, self.get_function_data), CompactFooter(self.stdscr, colours_start, self.get_function_data), NoFooter(self.stdscr, colours_start, self.get_function_data)]
321
+ self.footer = self.footer_options[self.footer_style]
322
+
323
+ # self.footer = CompactFooter(self.stdscr, colours_start, self.get_function_data)
324
+
325
+
326
+
327
+ def calculate_section_sizes(self):
328
+ """
329
+ Calculte the following for the Picker:
330
+ self.items_per_page: the number of entry rows displayed
331
+ self.bottom_space: the size of the footer + the bottom buffer space
332
+ self.top_space: the size of the space at the top of the picker: title + modes + header + top_gap
333
+ """
334
+
335
+ self.logger.debug(f"function: calculate_section_sizes()")
336
+
337
+ # self.bottom_space
338
+ self.bottom_space = self.footer.height if self.show_footer else 0
339
+
340
+ ## self.top_space
341
+ h, w = self.stdscr.getmaxyx()
342
+ self.top_space = self.top_gap
343
+ if self.title: self.top_space+=1
344
+ if self.modes and self.display_modes: self.top_space+=1
345
+ if self.header and self.show_header: self.top_space += 1
346
+
347
+ # self.items_per_page
348
+ self.items_per_page = h - self.top_space - self.bottom_space
349
+ if not self.show_footer and self.footer_string: self.items_per_page-=1
350
+ self.items_per_page = min(h-self.top_space-1, self.items_per_page)
351
+
352
+
353
+ # Adjust top space if centring vertically and we have fewer rows than terminal lines
354
+ if self.centre_in_terminal_vertical and len(self.indexed_items) < self.items_per_page:
355
+ self.top_space += ((h-(self.top_space+self.bottom_space))-len(self.indexed_items))//2
356
+
357
+ # self.column_widths
358
+ visible_column_widths = [c for i,c in enumerate(self.column_widths) if i not in self.hidden_columns]
359
+ visible_columns_total_width = sum(visible_column_widths) + len(self.separator)*(len(visible_column_widths)-1)
360
+
361
+ # self.startx
362
+ self.startx = 1 if self.highlight_full_row else 2
363
+ if self.show_row_header: self.startx += len(str(len(self.items))) + 2
364
+ if visible_columns_total_width < w and self.centre_in_terminal:
365
+ self.startx += (w - visible_columns_total_width) // 2
366
+
367
+ def get_visible_rows(self) -> list[list[str]]:
368
+
369
+ self.logger.debug(f"function: get_visible_rows()")
370
+ ## Scroll with column select
371
+ if self.paginate:
372
+ start_index = (self.cursor_pos//self.items_per_page) * self.items_per_page
373
+ end_index = min(start_index + self.items_per_page, len(self.indexed_items))
374
+ ## Scroll
375
+ else:
376
+ scrolloff = self.items_per_page//2
377
+ start_index = max(0, min(self.cursor_pos - (self.items_per_page-scrolloff), len(self.indexed_items)-self.items_per_page))
378
+ end_index = min(start_index + self.items_per_page, len(self.indexed_items))
379
+ if len(self.indexed_items) == 0: start_index, end_index = 0, 0
380
+
381
+ rows = [v[1] for v in self.indexed_items[start_index:end_index]] if len(self.indexed_items) else self.items
382
+ return rows
383
+
384
+ def initialise_picker_state(self, reset_colours=False) -> None:
385
+ """ Initialise state variables for the picker. These are: debugging and colours. """
386
+
387
+ if curses.has_colors() and self.colours != None:
388
+ # raise Exception("Terminal does not support color")
389
+ curses.start_color()
390
+ if reset_colours:
391
+ global COLOURS_SET
392
+ COLOURS_SET = False
393
+ colours_end = set_colours(pick=self.colour_theme_number, start=self.colours_start)
394
+ if curses.COLORS >= 255 and curses.COLOR_PAIRS >= 150:
395
+ self.colours_start = self.colours_start
396
+ self.notification_colours_start = self.colours_start+50
397
+ self.help_colours_start = self.colours_start+100
398
+ else:
399
+ self.colours_start = 0
400
+ self.notification_colours_start = 0
401
+ self.help_colours_start = 0
402
+ else:
403
+ self.colours_start = 0
404
+ self.notification_colours_start = 0
405
+ self.help_colours_start = 0
406
+
407
+
408
+ debug_levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]
409
+ dbglvl = debug_levels[self.debug_level]
410
+ self.logger = setup_logger(name="picker_log", log_file="picker.log", log_enabled=self.debug, level =dbglvl)
411
+ self.logger.info(f"Initialiasing Picker.")
412
+ # self.notification(self.stdscr, message=repr(self.logger))
413
+
414
+
415
+ # 1 2 3 4 5
416
+ # logger = logging.getLogger(__file__)
417
+ # if self.debug_level == 0:
418
+ # logger = logging.getLogger()
419
+ # logger.disabled = True
420
+ # else:
421
+ #
422
+ # file_handler = logging.FileHandler(f"{self.title}.log", mode='w')
423
+ # formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', '%m-%d-%Y %H:%M:%S')
424
+ # file_handler.setFormatter(formatter)
425
+ # logger.addHandler(file_handler)
426
+ # logger.setLevel(debug_levels[self.debug_level-1])
427
+
428
+ # logging.basicConfig(
429
+ # level=debug_levels[self.debug_level-1],
430
+ # format='%(asctime)s - %(levelname)s - %(message)s',
431
+ # datefmt='%m-%d-%Y %H:%M:%S',
432
+ # filename=f"{self.title}.log",
433
+ # filemode="w",
434
+ # )
435
+ #
436
+ # self.logger.info(f"Starging log. Log level {logger.getEffectiveLevel()}")
437
+ # self.logger.info(f"Starging log. Log level {repr(debug_levels)}, {self.debug_level}, {debug_levels[self.debug_level-1]}")
438
+ # self.notification(self.stdscr, f"Starging log. Log level {repr(debug_levels)}, {self.debug_level}, {debug_levels[self.debug_level-1]}")
439
+ # self.notification(self.stdscr, f"{__file__}")
440
+
441
+ ## Logging level plan
442
+ # DEBUG: loop functions, draw screen, etc.
443
+ # INFO: main functions
444
+ # WARNING: any try-except fails
445
+
446
+ # No set_escdelay function on windows.
447
+ try:
448
+ curses.set_escdelay(25)
449
+ except:
450
+ logging.warning("Error trying to set curses.set_escdelay")
451
+
452
+ # self.stdscr.clear()
453
+ # self.stdscr.refresh()
454
+ # self.draw_screen(self.indexed_items, self.highlights)
455
+
456
+
457
+
458
+ def initialise_variables(self, get_data: bool = False) -> None:
459
+ """ Initialise the variables that keep track of the data. """
460
+
461
+ self.logger.info(f"function: initialise_variables()")
462
+
463
+ tracking = False
464
+
465
+ ## Get data synchronously
466
+ if get_data and self.refresh_function != None:
467
+ if self.track_entries_upon_refresh and len(self.items) > 0:
468
+ tracking = True
469
+ selected_indices = get_selected_indices(self.selections)
470
+ self.selected_cells_by_row = get_selected_cells_by_row(self.cell_selections)
471
+ self.ids = [item[self.id_column] for i, item in enumerate(self.items) if i in selected_indices]
472
+ self.ids_tuples = [(i, item[self.id_column]) for i, item in enumerate(self.items) if i in selected_indices]
473
+
474
+ if len(self.indexed_items) > 0 and len(self.indexed_items) >= self.cursor_pos and len(self.indexed_items[0][1]) >= self.id_column:
475
+ self.cursor_pos_id = self.indexed_items[self.cursor_pos][1][self.id_column]
476
+
477
+ self.items, self.header = self.refresh_function()
478
+
479
+
480
+ if self.items == []: self.items = [[]]
481
+ ## Ensure that items is a List[List[Str]] object
482
+ if len(self.items) > 0 and not isinstance(self.items[0], list):
483
+ self.items = [[item] for item in self.items]
484
+ self.items = [[str(cell) for cell in row] for row in self.items]
485
+
486
+
487
+ # Ensure that header is of the same length as the rows
488
+ if self.header and len(self.items) > 0 and len(self.header) != len(self.items[0]):
489
+ self.header = [str(self.header[i]) if i < len(self.header) else "" for i in range(len(self.items[0]))]
490
+
491
+ # Constants
492
+ # DEFAULT_ITEMS_PER_PAGE = os.get_terminal_size().lines - top_gap*2-2-int(bool(header))
493
+
494
+ self.calculate_section_sizes()
495
+
496
+ # Initial states
497
+ if len(self.selections) != len(self.items):
498
+ self.selections = {i : False if i not in self.selections else bool(self.selections[i]) for i in range(len(self.items))}
499
+
500
+ if len(self.items) and len(self.cell_selections) != len(self.items)*len(self.items[0]):
501
+ self.cell_selections = {(i, j) : False if (i, j) not in self.cell_selections else self.cell_selections[(i, j)] for i in range(len(self.items)) for j in range(len(self.items[0]))}
502
+ elif len(self.items) == 0:
503
+ self.cell_selections = {}
504
+
505
+ if len(self.require_option) < len(self.items):
506
+ self.require_option += [self.require_option_default for i in range(len(self.items)-len(self.require_option))]
507
+ if len(self.option_functions) < len(self.items):
508
+ self.option_functions += [self.default_option_function for i in range(len(self.items)-len(self.option_functions))]
509
+ if len(self.items)>0 and len(self.columns_sort_method) < len(self.items[0]):
510
+ self.columns_sort_method = self.columns_sort_method + [0 for i in range(len(self.items[0])-len(self.columns_sort_method))]
511
+ if len(self.items)>0 and len(self.sort_reverse) < len(self.items[0]):
512
+ self.sort_reverse = self.sort_reverse + [False for i in range(len(self.items[0])-len(self.sort_reverse))]
513
+ if len(self.items)>0 and len(self.editable_columns) < len(self.items[0]):
514
+ self.editable_columns = self.editable_columns + [self.editable_by_default for i in range(len(self.items[0])-len(self.editable_columns))]
515
+ if len(self.items)>0 and len(self.column_indices) < len(self.items[0]):
516
+ self.column_indices = self.column_indices + [i for i in range(len(self.column_indices), len(self.items[0]))]
517
+
518
+
519
+
520
+ # items2 = [[row[self.column_indices[i]] for i in range(len(row))] for row in self.items]
521
+ # self.indexed_items = list(enumerate(items2))
522
+ if self.items == [[]]: self.indexed_items = []
523
+ else: self.indexed_items = list(enumerate(self.items))
524
+
525
+ # If a filter is passed then refilter
526
+ if self.filter_query:
527
+ # prev_index = self.indexed_items[cursor_pos][0] if len(self.indexed_items)>0 else 0
528
+ # prev_index = self.indexed_items[cursor_pos][0] if len(self.indexed_items)>0 else 0
529
+ self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
530
+ if self.cursor_pos in [x[0] for x in self.indexed_items]: self.cursor_pos = [x[0] for x in self.indexed_items].index(self.cursor_pos)
531
+ else: self.cursor_pos = 0
532
+ if self.search_query:
533
+ return_val, tmp_cursor, tmp_index, tmp_count, tmp_highlights = search(
534
+ query=self.search_query,
535
+ indexed_items=self.indexed_items,
536
+ highlights=self.highlights,
537
+ cursor_pos=self.cursor_pos,
538
+ unselectable_indices=self.unselectable_indices,
539
+ continue_search=True,
540
+ )
541
+ if return_val:
542
+ self.cursor_pos, self.search_index, self.search_count, self.highlights = tmp_cursor, tmp_index, tmp_count, tmp_highlights
543
+ # If a sort is passed
544
+ if len(self.indexed_items) > 0:
545
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
546
+ # if len(self.items[0]) == 1:
547
+ # self.number_columns = False
548
+
549
+
550
+
551
+ h, w = self.stdscr.getmaxyx()
552
+
553
+ # Adjust variables to ensure correctness if errors
554
+ ## Move to a selectable row (if applicable)
555
+ if len(self.items) <= len(self.unselectable_indices): self.unselectable_indices = []
556
+ new_pos = (self.cursor_pos)%len(self.items)
557
+ while new_pos in self.unselectable_indices and new_pos != self.cursor_pos:
558
+ new_pos = (new_pos + 1) % len(self.items)
559
+
560
+ assert new_pos < len(self.items)
561
+ self.cursor_pos = new_pos
562
+
563
+
564
+ # if tracking and len(self.items) > 1:
565
+ # Ensure that selected indices are tracked upon data refresh
566
+ if self.track_entries_upon_refresh and (self.data_ready or tracking) and len(self.items) > 1:
567
+ selected_indices = []
568
+ all_ids = [item[self.id_column] for item in self.items]
569
+ self.selections = {i: False for i in range(len(self.items))}
570
+ if len(self.items) > 0:
571
+ self.cell_selections = {(i, j): False for i in range(len(self.items)) for j in range(len(self.items[0]))}
572
+ else:
573
+ self.cell_selections = {}
574
+
575
+ for id in self.ids:
576
+ if id in all_ids:
577
+ selected_indices.append(all_ids.index(id))
578
+ self.selections[all_ids.index(id)] = True
579
+
580
+ for i, id in self.ids_tuples:
581
+ if id in all_ids:
582
+ # rows_with_selected_cells
583
+ for j in self.selected_cells_by_row[i]:
584
+ self.cell_selections[(all_ids.index(id), j)] = True
585
+
586
+
587
+
588
+ if len(self.indexed_items):
589
+ if self.pin_cursor:
590
+ self.cursor_pos = min(self.cursor_pos_prev, len(self.indexed_items)-1)
591
+ else:
592
+ if self.cursor_pos_id in all_ids:
593
+ cursor_pos_x = all_ids.index(self.cursor_pos_id)
594
+ if cursor_pos_x in [i[0] for i in self.indexed_items]:
595
+ self.cursor_pos = [i[0] for i in self.indexed_items].index(cursor_pos_x)
596
+ else:
597
+ self.cursor_pos = 0
598
+
599
+
600
+
601
+ def move_column(self, direction: int) -> None:
602
+ """
603
+ Cycles the column $direction places.
604
+ E.g., If $direction == -1 and the sort column is 3, then column 3 will swap with column 2
605
+ in each of the rows in $items and 2 will become the new sort column.
606
+
607
+ sort_column = 3, direction = -1
608
+ [[0,1,2,*3*,4],
609
+ [5,6,7,*8*,9]]
610
+ -->
611
+ [[0,1,*3*,2,4],
612
+ [5,6,*8*,7,9]]
613
+
614
+ returns:
615
+ adjusted items, header, sort_column and column_widths
616
+ """
617
+ self.logger.info(f"function: move_column(direction={direction})")
618
+ if len(self.items) < 1: return None
619
+ if (self.selected_column+direction) < 0 or (self.selected_column+direction) >= len(self.items[0]): return None
620
+
621
+ new_index = self.selected_column + direction
622
+
623
+ # Swap columns in each row
624
+ for row in self.items:
625
+ row[self.selected_column], row[new_index] = row[new_index], row[self.selected_column]
626
+ if self.header:
627
+ self.header[self.selected_column], self.header[new_index] = self.header[new_index], self.header[self.selected_column]
628
+
629
+ # Swap column widths
630
+ self.column_widths[self.selected_column], self.column_widths[new_index] = self.column_widths[new_index], self.column_widths[self.selected_column]
631
+
632
+ # Update current column index
633
+ self.selected_column = new_index
634
+
635
+ def test_screen_size(self):
636
+ self.logger.debug("function: test_screen_size()")
637
+ h, w = self.stdscr.getmaxyx()
638
+ ## Terminal too small to display Picker
639
+ if h<3 or w<len("Terminal"): return False
640
+ if (self.show_footer or self.footer_string) and (h<12 or w<35) or (h<12 and w<10):
641
+ self.stdscr.addstr(h//2-1, (w-len("Terminal"))//2, "Terminal")
642
+ self.stdscr.addstr(h//2, (w-len("Too"))//2, "Too")
643
+ self.stdscr.addstr(h//2+1, (w-len("Small"))//2, "Small")
644
+ return False
645
+ return True
646
+
647
+ def splash_screen(self, message=""):
648
+ self.logger.info(f"function: splash_screen({message})")
649
+ """ Display a splash screen with a message. Useful when loading a large data set. """
650
+ h, w =self.stdscr.getmaxyx()
651
+ self.stdscr.bkgd(' ', curses.color_pair(2))
652
+ try:
653
+ self.stdscr.addstr(h//2, (w-len(message))//2, message, curses.color_pair(2))
654
+ except:
655
+ pass
656
+ self.stdscr.refresh()
657
+
658
+ def draw_screen(self, indexed_items: list[Tuple[int, list[str]]], highlights: list[dict] = [{}], clear: bool = True) -> None:
659
+ """ Draw Picker screen. """
660
+ self.logger.debug("Draw screen.")
661
+
662
+ if clear:
663
+ self.stdscr.erase()
664
+
665
+ h, w = self.stdscr.getmaxyx()
666
+
667
+ # Test if the terminal is of a sufficient size to display the picker
668
+ if not self.test_screen_size(): return None
669
+
670
+ # Determine which rows are to be displayed on the current screen
671
+ ## Paginate
672
+ if self.paginate:
673
+ start_index = (self.cursor_pos//self.items_per_page) * self.items_per_page
674
+ end_index = min(start_index + self.items_per_page, len(self.indexed_items))
675
+ ## Scroll
676
+ else:
677
+ scrolloff = self.items_per_page//2
678
+ start_index = max(0, min(self.cursor_pos - (self.items_per_page-scrolloff), len(self.indexed_items)-self.items_per_page))
679
+ end_index = min(start_index + self.items_per_page, len(self.indexed_items))
680
+ if len(self.indexed_items) == 0: start_index, end_index = 0, 0
681
+
682
+ # self.column_widths = get_column_widths(self.items, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
683
+ # Determine widths based only on the currently indexed rows
684
+ # rows = [v[1] for v in self.indexed_items] if len(self.indexed_items) else self.items
685
+ # Determine widths based only on the currently displayed indexed rows
686
+ rows = [v[1] for v in self.indexed_items[start_index:end_index]] if len(self.indexed_items) else self.items
687
+ self.column_widths = get_column_widths(rows, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
688
+ visible_column_widths = [c for i,c in enumerate(self.column_widths) if i not in self.hidden_columns]
689
+ visible_columns_total_width = sum(visible_column_widths) + len(self.separator)*(len(visible_column_widths)-1)
690
+
691
+ # Determine the number of items_per_page, top_size and bottom_size
692
+ self.calculate_section_sizes()
693
+
694
+ # top_space = self.top_gap
695
+
696
+ ## Display title (if applicable)
697
+ if self.title:
698
+ padded_title = f" {self.title.strip()} "
699
+ self.stdscr.addstr(self.top_gap, 0, f"{' ':^{w}}", curses.color_pair(self.colours_start+16))
700
+ title_x = (w-wcswidth(padded_title))//2
701
+ # title = f"{title:^{w}}"
702
+ self.stdscr.addstr(self.top_gap, title_x, padded_title, curses.color_pair(self.colours_start+16) | curses.A_BOLD)
703
+ # top_space += 1
704
+
705
+ ## Display modes
706
+ if self.display_modes and self.modes not in [[{}], []]:
707
+ self.stdscr.addstr(self.top_gap+1, 0, ' '*w, curses.A_REVERSE)
708
+ modes_list = [f"{mode['name']}" if 'name' in mode else f"{i}. " for i, mode in enumerate(self.modes)]
709
+ # mode_colours = [mode["colour"] for mode ]
710
+ mode_widths = get_mode_widths(modes_list)
711
+ split_space = (w-sum(mode_widths))//len(self.modes)
712
+ xmode = 0
713
+ for i, mode in enumerate(modes_list):
714
+ if i == len(modes_list)-1:
715
+ mode_str = f"{mode:^{mode_widths[i]+split_space+(w-sum(mode_widths))%len(self.modes)}}"
716
+ else:
717
+ mode_str = f"{mode:^{mode_widths[i]+split_space}}"
718
+ # current mode
719
+ if i == self.mode_index:
720
+ self.stdscr.addstr(self.top_gap+1, xmode, mode_str, curses.color_pair(self.colours_start+14) | curses.A_BOLD)
721
+ # other modes
722
+ else:
723
+ self.stdscr.addstr(self.top_gap+1, xmode, mode_str, curses.color_pair(self.colours_start+15) | curses.A_UNDERLINE)
724
+ xmode += split_space+mode_widths[i]
725
+ # top_space += 1
726
+
727
+ ## Display header
728
+ if self.header and self.show_header:
729
+ header_str = ""
730
+ up_to_selected_col = ""
731
+ selected_col_str = ""
732
+ for i in range(len(self.header)):
733
+ if i == self.selected_column: up_to_selected_col = header_str
734
+ if i in self.hidden_columns: continue
735
+ number = f"{i}. " if self.number_columns else ""
736
+ # number = f"{intStringToExponentString(str(i))}. " if self.number_columns else ""
737
+ header_str += number
738
+ header_str += f"{self.header[i]:^{self.column_widths[i]-len(number)}}"
739
+ header_str += self.separator
740
+
741
+ header_str = header_str[self.leftmost_char:]
742
+ header_ypos = self.top_gap + bool(self.title) + bool(self.display_modes and self.modes)
743
+ self.stdscr.addstr(header_ypos, 0, ' '*w, curses.color_pair(self.colours_start+4) | curses.A_BOLD)
744
+ self.stdscr.addstr(header_ypos, self.startx, header_str[:min(w-self.startx, visible_columns_total_width+1)], curses.color_pair(self.colours_start+4) | curses.A_BOLD)
745
+
746
+ # Highlight sort column
747
+ try:
748
+ if self.selected_column != None and self.selected_column not in self.hidden_columns:
749
+ if len(self.header) > 1 and (len(up_to_selected_col)-self.leftmost_char) < w:
750
+ # if len(up_to_selected_col) + 1 < w or True:
751
+ # if self.startx + len(up_to_selected_col) - self.leftmost_char > 0 or True:
752
+ number = f"{self.selected_column}. " if self.number_columns else ""
753
+ # number = f"{intStringToExponentString(self.selected_column)}. " if self.number_columns else ""
754
+ # self.startx + len(up_to_selected_col) - self.leftmost_char
755
+ highlighed_col_startx = max(self.startx, self.startx + len(up_to_selected_col) - self.leftmost_char)
756
+ highlighted_col_str = (number+f"{self.header[self.selected_column]:^{self.column_widths[self.selected_column]-len(number)}}") + self.separator
757
+ end_of_highlighted_col_str = w-(highlighed_col_startx+len(highlighted_col_str)) if (highlighed_col_startx+len(highlighted_col_str)) > w else len(highlighted_col_str)
758
+ start_of_highlighted_col_str = max(self.leftmost_char - len(up_to_selected_col), 0)
759
+ self.stdscr.addstr(header_ypos, highlighed_col_startx , highlighted_col_str[start_of_highlighted_col_str:end_of_highlighted_col_str], curses.color_pair(self.colours_start+19) | curses.A_BOLD)
760
+ except:
761
+ pass
762
+
763
+ # Display row header
764
+ if self.show_row_header:
765
+ for idx in range(start_index, end_index):
766
+ y = idx - start_index + self.top_space
767
+ if idx == self.cursor_pos:
768
+ self.stdscr.addstr(y, 0, f" {self.indexed_items[idx][0]} ", curses.color_pair(self.colours_start+19) | curses.A_BOLD)
769
+ else:
770
+ self.stdscr.addstr(y, 0, f" {self.indexed_items[idx][0]} ", curses.color_pair(self.colours_start+4) | curses.A_BOLD)
771
+
772
+ def highlight_cell(row: int, col:int, visible_column_widths, colour_pair_number: int = 5):
773
+ cell_pos = sum(visible_column_widths[:col])+col*len(self.separator)-self.leftmost_char + self.startx
774
+ # cell_width = self.column_widths[self.selected_column]
775
+ cell_width = visible_column_widths[col] + len(self.separator)
776
+ cell_max_width = w-cell_pos
777
+ try:
778
+ # Start of cell is on screen
779
+ if self.startx <= cell_pos <= w:
780
+ self.stdscr.addstr(y, cell_pos, (' '*cell_width)[:cell_max_width], curses.color_pair(self.colours_start+colour_pair_number))
781
+ if self.centre_in_cols:
782
+ cell_value = f"{self.indexed_items[row][1][col]:^{cell_width-len(self.separator)}}" + self.separator
783
+ else:
784
+ cell_value = self.indexed_items[row][1][col] + self.separator
785
+ # cell_value = cell_value[:min(cell_width, cell_max_width)-len(self.separator)]
786
+ cell_value = truncate_to_display_width(cell_value, min(cell_width, cell_max_width)-len(self.separator))
787
+ cell_value = cell_value + self.separator
788
+ # cell_value = cell_value
789
+ cell_value = truncate_to_display_width(cell_value, min(cell_width, cell_max_width))
790
+ # row_str = truncate_to_display_width(row_str_left_adj, min(w-self.startx, visible_columns_total_width))
791
+ self.stdscr.addstr(y, cell_pos, cell_value, curses.color_pair(self.colours_start+colour_pair_number) | curses.A_BOLD)
792
+ # Part of the cell is on screen
793
+ elif self.startx <= cell_pos+cell_width <= w:
794
+ cell_start = self.startx - cell_pos
795
+ self.stdscr.addstr(y, self.startx, ' '*(cell_width-cell_start), curses.color_pair(self.colours_start+colour_pair_number))
796
+ cell_value = self.indexed_items[row][1][col][cell_start:visible_column_widths[col]]
797
+ self.stdscr.addstr(y, self.startx, cell_value, curses.color_pair(self.colours_start+colour_pair_number) | curses.A_BOLD)
798
+ except:
799
+ pass
800
+
801
+ # Draw:
802
+ # 1. standard row
803
+ # 2. highlights l0
804
+ # 3. selected
805
+ # 4. above-selected highlights l1
806
+ # 5. cursor
807
+ # 6. top-level highlights l2
808
+ ## Display rows and highlights
809
+
810
+ def sort_highlights(highlights):
811
+ """
812
+ Sort highlights into lists based on their display level.
813
+ Highlights with no level defined will be displayed at level 0.
814
+ """
815
+ l0 = []
816
+ l1 = []
817
+ l2 = []
818
+ for highlight in highlights:
819
+ if "level" in highlight:
820
+ if highlight["level"] == 0: l0.append(highlight)
821
+ elif highlight["level"] == 1: l1.append(highlight)
822
+ elif highlight["level"] == 2: l2.append(highlight)
823
+ else: l0.append(highlight)
824
+ else:
825
+ l0.append(highlight)
826
+ return l0, l1, l2
827
+
828
+ def draw_highlights(highlights: list[dict], idx: int, y: int, item: tuple[int, list[str]]):
829
+ self.logger.debug(f"function: draw_highlights()")
830
+ if len(highlights) == 0: return None
831
+ full_row_str = format_row(item[1], self.hidden_columns, self.column_widths, self.separator, self.centre_in_cols)
832
+ row_str = full_row_str[self.leftmost_char:]
833
+ for highlight in highlights:
834
+ if "row" in highlight:
835
+ if highlight["row"] != self.indexed_items[idx][0]:
836
+ continue
837
+ try:
838
+ if highlight["field"] == "all":
839
+ match = re.search(highlight["match"], full_row_str, re.IGNORECASE)
840
+ if not match: continue
841
+ highlight_start = match.start()
842
+ highlight_end = match.end()
843
+ if highlight_end - self.leftmost_char < 0:
844
+ continue
845
+
846
+ elif type(highlight["field"]) == type(0) and highlight["field"] not in self.hidden_columns:
847
+ match = re.search(highlight["match"], truncate_to_display_width(item[1][highlight["field"]], self.column_widths[highlight["field"]], centre=False), re.IGNORECASE)
848
+ if not match: continue
849
+ field_start = sum([width for i, width in enumerate(self.column_widths[:highlight["field"]]) if i not in self.hidden_columns]) + sum([1 for i in range(highlight["field"]) if i not in self.hidden_columns])*wcswidth(self.separator)
850
+
851
+ ## We want to search the non-centred values but highlight the centred values.
852
+ if self.centre_in_cols:
853
+ tmp = truncate_to_display_width(item[1][highlight["field"]], self.column_widths[highlight["field"]], self.centre_in_cols)
854
+ field_start += (len(tmp) - len(tmp.lstrip()))
855
+
856
+ highlight_start = field_start + match.start()
857
+ highlight_end = match.end() + field_start
858
+ if highlight_end - self.leftmost_char < 0:
859
+ continue
860
+ else:
861
+ continue
862
+ highlight_start -= self.leftmost_char
863
+ highlight_end -= self.leftmost_char
864
+ self.stdscr.addstr(y, max(self.startx, self.startx+highlight_start), row_str[max(highlight_start,0):min(w-self.startx, highlight_end)], curses.color_pair(self.colours_start+highlight["color"]) | curses.A_BOLD)
865
+ except:
866
+ pass
867
+
868
+ l0_highlights, l1_highlights, l2_highlights = sort_highlights(self.highlights)
869
+
870
+
871
+ for idx in range(start_index, end_index):
872
+ item = self.indexed_items[idx]
873
+ y = idx - start_index + self.top_space
874
+
875
+ row_str = format_row(item[1], self.hidden_columns, self.column_widths, self.separator, self.centre_in_cols)[self.leftmost_char:]
876
+ # row_str = truncate_to_display_width(row_str, min(w-self.startx, visible_columns_total_width))
877
+ row_str_orig = format_row(item[1], self.hidden_columns, self.column_widths, self.separator, self.centre_in_cols)
878
+ row_str_left_adj = clip_left(row_str_orig, self.leftmost_char)
879
+ row_str = truncate_to_display_width(row_str_left_adj, min(w-self.startx, visible_columns_total_width))
880
+ # row_str = truncate_to_display_width(row_str, min(w-self.startx, visible_columns_total_width))[self.leftmost_char:]
881
+
882
+ ## Display the standard row
883
+ self.stdscr.addstr(y, self.startx, row_str[:min(w-self.startx, visible_columns_total_width)], curses.color_pair(self.colours_start+2))
884
+
885
+
886
+ # Draw the level 0 highlights
887
+ if not self.highlights_hide:
888
+ draw_highlights(l0_highlights, idx, y, item)
889
+
890
+ # Higlight cursor cell and selected cells
891
+ if self.cell_cursor:
892
+ self.selected_cells_by_row = get_selected_cells_by_row(self.cell_selections)
893
+ if item[0] in self.selected_cells_by_row:
894
+ for j in self.selected_cells_by_row[item[0]]:
895
+ highlight_cell(idx, j, visible_column_widths, colour_pair_number=25)
896
+
897
+ # Visually selected
898
+ if self.is_selecting:
899
+ if self.start_selection <= idx <= self.cursor_pos or self.start_selection >= idx >= self.cursor_pos:
900
+ x_interval = range(min(self.start_selection_col, self.selected_column), max(self.start_selection_col, self.selected_column)+1)
901
+ for col in x_interval:
902
+ highlight_cell(idx, col, visible_column_widths, colour_pair_number=25)
903
+
904
+ # Visually deslected
905
+ if self.is_deselecting:
906
+ if self.start_selection >= idx >= self.cursor_pos or self.start_selection <= idx <= self.cursor_pos:
907
+ x_interval = range(min(self.start_selection_col, self.selected_column), max(self.start_selection_col, self.selected_column)+1)
908
+ for col in x_interval:
909
+ highlight_cell(idx, col, visible_column_widths, colour_pair_number=26)
910
+ # Higlight cursor row and selected rows
911
+ elif self.highlight_full_row:
912
+ if self.selections[item[0]]:
913
+ self.stdscr.addstr(y, self.startx, row_str[:min(w-self.startx, visible_columns_total_width)], curses.color_pair(self.colours_start+25) | curses.A_BOLD)
914
+ # Visually selected
915
+ if self.is_selecting:
916
+ if self.start_selection <= idx <= self.cursor_pos or self.start_selection >= idx >= self.cursor_pos:
917
+ self.stdscr.addstr(y, self.startx, row_str[:min(w-self.startx, visible_columns_total_width)], curses.color_pair(self.colours_start+25))
918
+ # Visually deslected
919
+ elif self.is_deselecting:
920
+ if self.start_selection >= idx >= self.cursor_pos or self.start_selection <= idx <= self.cursor_pos:
921
+ self.stdscr.addstr(y, self.startx, row_str[:min(w-self.startx, visible_columns_total_width)], curses.color_pair(self.colours_start+26))
922
+
923
+ # Highlight the cursor row and the first char of the selected rows.
924
+ else:
925
+ if self.selections[item[0]]:
926
+ self.stdscr.addstr(y, max(self.startx-2,0), ' ', curses.color_pair(self.colours_start+1))
927
+ # Visually selected
928
+ if self.is_selecting:
929
+ if self.start_selection <= idx <= self.cursor_pos or self.start_selection >= idx >= self.cursor_pos:
930
+ self.stdscr.addstr(y, max(self.startx-2,0), ' ', curses.color_pair(self.colours_start+1))
931
+ # Visually deslected
932
+ if self.is_deselecting:
933
+ if self.start_selection >= idx >= self.cursor_pos or self.start_selection <= idx <= self.cursor_pos:
934
+ self.stdscr.addstr(y, max(self.startx-2,0), ' ', curses.color_pair(self.colours_start+10))
935
+
936
+ if not self.highlights_hide:
937
+ draw_highlights(l1_highlights, idx, y, item)
938
+
939
+ # Draw cursor
940
+ if idx == self.cursor_pos:
941
+ if self.cell_cursor:
942
+ highlight_cell(idx, self.selected_column, visible_column_widths)
943
+ else:
944
+ self.stdscr.addstr(y, self.startx, row_str[:min(w-self.startx, visible_columns_total_width)], curses.color_pair(self.colours_start+5) | curses.A_BOLD)
945
+
946
+ if not self.highlights_hide:
947
+ draw_highlights(l2_highlights, idx, y, item)
948
+
949
+ ## Display scrollbar
950
+ if self.scroll_bar and len(self.indexed_items) and len(self.indexed_items) > (self.items_per_page):
951
+ scroll_bar_length = int(self.items_per_page*self.items_per_page/len(self.indexed_items))
952
+ if self.cursor_pos <= self.items_per_page//2:
953
+ scroll_bar_start=self.top_space
954
+ elif self.cursor_pos + self.items_per_page//2 >= len(self.indexed_items):
955
+ scroll_bar_start = h - int(bool(self.show_footer))*self.footer.height - scroll_bar_length
956
+ else:
957
+ scroll_bar_start = int(((self.cursor_pos)/len(self.indexed_items))*self.items_per_page)+self.top_space - scroll_bar_length//2
958
+ scroll_bar_start = min(scroll_bar_start, h-self.top_space-1)
959
+ scroll_bar_length = min(scroll_bar_length, h - scroll_bar_start-1)
960
+ scroll_bar_length = max(1, scroll_bar_length)
961
+ for i in range(scroll_bar_length):
962
+ v = max(self.top_space+int(bool(self.header)), scroll_bar_start-scroll_bar_length//2)
963
+ self.stdscr.addstr(scroll_bar_start+i, w-1, ' ', curses.color_pair(self.colours_start+18))
964
+
965
+ # Display refresh symbol
966
+ if self.auto_refresh:
967
+ if self.refreshing_data:
968
+ self.stdscr.addstr(0,w-3,"  ", curses.color_pair(self.colours_start+21) | curses.A_BOLD)
969
+ else:
970
+ self.stdscr.addstr(0,w-3,"  ", curses.color_pair(self.colours_start+23) | curses.A_BOLD)
971
+
972
+ ## Display footer
973
+ if self.show_footer:
974
+ # self.footer = NoFooter(self.stdscr, self.colours_start, self.get_function_data)
975
+ h, w = self.stdscr.getmaxyx()
976
+ try:
977
+ self.footer.draw(h, w)
978
+ except:
979
+ pass
980
+ elif self.footer_string:
981
+ footer_string_width = min(w-1, len(self.footer_string)+2)
982
+ disp_string = f" {self.footer_string[:footer_string_width]:>{footer_string_width-2}} "
983
+ self.stdscr.addstr(h - 1, w-footer_string_width-1, " "*footer_string_width, curses.color_pair(self.colours_start+24))
984
+ self.stdscr.addstr(h - 1, w-footer_string_width-1, f"{disp_string}", curses.color_pair(self.colours_start+24))
985
+
986
+ ## Display infobox
987
+ if self.display_infobox:
988
+ self.infobox(self.stdscr, message=self.infobox_items, title=self.infobox_title)
989
+ # self.stdscr.timeout(2000) # timeout is set to 50 in order to get the infobox to be displayed so here we reset it to 2000
990
+
991
+
992
+
993
+ def infobox(self, stdscr: curses.window, message: str ="", title: str ="Infobox", colours_end: int = 0, duration: int = 4) -> curses.window:
994
+ """ Display non-interactive infobox window. """
995
+
996
+ self.logger.info(f"function: infobox()")
997
+ h, w = stdscr.getmaxyx()
998
+ notification_width, notification_height = w//2, 3*h//5
999
+ message_width = notification_width-5
1000
+
1001
+ if not message: message = "!!"
1002
+ if isinstance(message, str):
1003
+ submenu_items = [" "+message[i*message_width:(i+1)*message_width] for i in range(len(message)//message_width+1)]
1004
+ else:
1005
+ submenu_items = message
1006
+
1007
+ notification_remap_keys = {
1008
+ curses.KEY_RESIZE: curses.KEY_F5,
1009
+ 27: ord('q')
1010
+ }
1011
+ if len(submenu_items) > notification_height - 2:
1012
+ submenu_items = submenu_items[:notification_height-3] + [f"{'....':^{notification_width}}"]
1013
+ while True:
1014
+ h, w = stdscr.getmaxyx()
1015
+
1016
+ submenu_win = curses.newwin(notification_height, notification_width, 3, w - (notification_width+4))
1017
+ infobox_data = {
1018
+ "items": submenu_items,
1019
+ "colours": notification_colours,
1020
+ "colours_start": self.notification_colours_start,
1021
+ "disabled_keys": [ord('z'), ord('c')],
1022
+ "show_footer": False,
1023
+ "top_gap": 0,
1024
+ "key_remappings": notification_remap_keys,
1025
+ "display_only": True,
1026
+ "hidden_columns": [],
1027
+ "title": title,
1028
+ "reset_colours": False,
1029
+ }
1030
+
1031
+ OptionPicker = Picker(submenu_win, **infobox_data)
1032
+ s, o, f = OptionPicker.run()
1033
+ if o != "refresh": break
1034
+
1035
+ return submenu_win
1036
+
1037
+ def get_function_data(self) -> dict:
1038
+ self.logger.debug(f"function: get_function_data()")
1039
+ """ Returns a dict of the main variables needed to restore the state of list_pikcer. """
1040
+ function_data = {
1041
+ "selections": self.selections,
1042
+ "cell_selections": self.cell_selections,
1043
+ "items_per_page": self.items_per_page,
1044
+ "current_row": self.current_row,
1045
+ "current_page": self.current_page,
1046
+ "cursor_pos": self.cursor_pos,
1047
+ "colours": self.colours,
1048
+ "colour_theme_number": self.colour_theme_number,
1049
+ "selected_column": self.selected_column,
1050
+ "sort_column": self.sort_column,
1051
+ "sort_method": self.sort_method,
1052
+ "sort_reverse": self.sort_reverse,
1053
+ "SORT_METHODS": self.SORT_METHODS,
1054
+ "hidden_columns": self.hidden_columns,
1055
+ "is_selecting": self.is_selecting,
1056
+ "is_deselecting": self.is_deselecting,
1057
+ "user_opts": self.user_opts,
1058
+ "options_list": self.options_list,
1059
+ "user_settings": self.user_settings,
1060
+ "separator": self.separator,
1061
+ "search_query": self.search_query,
1062
+ "search_count": self.search_count,
1063
+ "search_index": self.search_index,
1064
+ "filter_query": self.filter_query,
1065
+ "indexed_items": self.indexed_items,
1066
+ "start_selection": self.start_selection,
1067
+ "start_selection_col": self.start_selection_col,
1068
+ "end_selection": self.end_selection,
1069
+ "highlights": self.highlights,
1070
+ "max_column_width": self.max_column_width,
1071
+ "column_indices": self.column_indices,
1072
+ "mode_index": self.mode_index,
1073
+ "modes": self.modes,
1074
+ "title": self.title,
1075
+ "display_modes": self.display_modes,
1076
+ "require_option": self.require_option,
1077
+ "require_option_default": self.require_option_default,
1078
+ "option_functions": self.option_functions,
1079
+ "top_gap": self.top_gap,
1080
+ "number_columns": self.number_columns,
1081
+ "items": self.items,
1082
+ "indexed_items": self.indexed_items,
1083
+ "header": self.header,
1084
+ "scroll_bar": self.scroll_bar,
1085
+ "columns_sort_method": self.columns_sort_method,
1086
+ "disabled_keys": self.disabled_keys,
1087
+ "show_footer": self.show_footer,
1088
+ "footer_string": self.footer_string,
1089
+ "footer_string_auto_refresh": self.footer_string_auto_refresh,
1090
+ "footer_string_refresh_function": self.footer_string_refresh_function,
1091
+ "footer_timer": self.footer_timer,
1092
+ "footer_style": self.footer_style,
1093
+ "colours_start": self.colours_start,
1094
+ "colours_end": self.colours_end,
1095
+ "display_only": self.display_only,
1096
+ "infobox_items": self.infobox_items,
1097
+ "display_infobox": self.display_infobox,
1098
+ "infobox_title": self.infobox_title,
1099
+ "key_remappings": self.key_remappings,
1100
+ "auto_refresh": self.auto_refresh,
1101
+ "get_new_data": self.get_new_data,
1102
+ "refresh_function": self.refresh_function,
1103
+ "timer": self.timer,
1104
+ "get_data_startup": self.get_data_startup,
1105
+ "get_footer_string_startup": self.get_footer_string_startup,
1106
+ "editable_columns": self.editable_columns,
1107
+ "last_key": self.last_key,
1108
+ "centre_in_terminal": self.centre_in_terminal,
1109
+ "centre_in_terminal_vertical": self.centre_in_terminal_vertical,
1110
+ "centre_in_cols": self.centre_in_cols,
1111
+ "highlight_full_row": self.highlight_full_row,
1112
+ "cell_cursor": self.cell_cursor,
1113
+ "column_widths": self.column_widths,
1114
+ "track_entries_upon_refresh": self.track_entries_upon_refresh,
1115
+ "pin_cursor": self.pin_cursor,
1116
+ "id_column": self.id_column,
1117
+ "startup_notification": self.startup_notification,
1118
+ "keys_dict": self.keys_dict,
1119
+ "cancel_is_back": self.cancel_is_back,
1120
+ "paginate": self.paginate,
1121
+ "leftmost_column": self.leftmost_column,
1122
+ "leftmost_char": self.leftmost_char,
1123
+ "history_filter_and_search" : self.history_filter_and_search,
1124
+ "history_pipes" : self.history_pipes,
1125
+ "history_opts" : self.history_opts,
1126
+ "history_edits" : self.history_edits,
1127
+ "history_settings": self.history_settings,
1128
+ "show_header": self.show_header,
1129
+ "show_row_header": self.show_row_header,
1130
+ "debug": self.debug,
1131
+ "debug_level": self.debug_level,
1132
+ "reset_colours": self.reset_colours,
1133
+ "unicode_char_width": self.unicode_char_width,
1134
+ }
1135
+ return function_data
1136
+
1137
+ def set_function_data(self, function_data: dict) -> None:
1138
+ """ Set variables from state dict containing core variables."""
1139
+ self.logger.info(f"function: set_function_data()")
1140
+ variables = self.get_function_data().keys()
1141
+
1142
+ for var in variables:
1143
+ if var in function_data:
1144
+ setattr(self, var, function_data[var])
1145
+
1146
+ reset_colours = bool("colour_theme_number" in function_data)
1147
+ self.initialise_picker_state(reset_colours=reset_colours)
1148
+
1149
+ self.initialise_variables()
1150
+ # if "colour_theme_number" in function_data:
1151
+ # global COLOURS_SET
1152
+ # COLOURS_SET = False
1153
+ # colours_end = set_colours(pick=self.colour_theme_number, start=self.colours_start)
1154
+
1155
+ # if "items" in function_data: self.items = function_data["items"]
1156
+ # if "header" in function_data: self.header = function_data["header"]
1157
+ # self.indexed_items = function_data["indexed_items"] if "indexed_items" in function_data else []
1158
+
1159
+
1160
+
1161
+ def delete_entries(self) -> None:
1162
+ """ Delete entries from view. """
1163
+
1164
+ self.logger.info(f"function: delete_entries()")
1165
+ # Remove selected items from the list
1166
+ selected_indices = [index for index, selected in self.selections.items() if selected]
1167
+ if not selected_indices:
1168
+ # Remove the currently focused item if nothing is selected
1169
+ selected_indices = [self.indexed_items[self.cursor_pos][0]]
1170
+
1171
+ self.items = [item for i, item in enumerate(self.items) if i not in selected_indices]
1172
+ self.indexed_items = [(i, item) for i, item in enumerate(self.items)]
1173
+ self.selections = {i:False for i in range(len(self.indexed_items))}
1174
+ self.cursor_pos = min(self.cursor_pos, len(self.indexed_items)-1)
1175
+ self.initialise_variables()
1176
+ self.draw_screen(self.indexed_items, self.highlights)
1177
+
1178
+
1179
+ def choose_option(
1180
+ self,
1181
+ stdscr: curses.window,
1182
+ options: list[list[str]] =[],
1183
+ title: str = "Choose option",
1184
+ x:int=0,
1185
+ y:int=0,
1186
+ literal:bool=False,
1187
+ colours_start:int=0,
1188
+ header: list[str] = [],
1189
+ require_option:list = [],
1190
+ option_functions: list = [],
1191
+ ) -> Tuple[dict, str, dict]:
1192
+ """
1193
+ Display input field at x,y
1194
+
1195
+ ---Arguments
1196
+ stdscr: curses screen
1197
+ usrtxt (str): text to be edited by the user
1198
+ title (str): The text to be displayed at the start of the text option picker
1199
+ x (int): prompt begins at (x,y) in the screen given
1200
+ y (int): prompt begins at (x,y) in the screen given
1201
+ colours_start (bool): start index of curses init_pair.
1202
+
1203
+ ---Returns
1204
+ usrtxt, return_code
1205
+ usrtxt: the text inputted by the user
1206
+ return_code:
1207
+ 0: user hit escape
1208
+ 1: user hit return
1209
+ """
1210
+ self.logger.info(f"function: choose_option()")
1211
+ if options == []: options = [[f"{i}"] for i in range(10)]
1212
+ cursor = 0
1213
+
1214
+
1215
+ option_picker_data = {
1216
+ "items": options,
1217
+ "colours": notification_colours,
1218
+ "colours_start": self.notification_colours_start,
1219
+ "title":title,
1220
+ "header":header,
1221
+ "hidden_columns":[],
1222
+ "require_option":require_option,
1223
+ "keys_dict": options_keys,
1224
+ "show_footer": False,
1225
+ "cancel_is_back": True,
1226
+ "number_columns": False,
1227
+ "reset_colours": False,
1228
+ }
1229
+ while True:
1230
+ h, w = stdscr.getmaxyx()
1231
+
1232
+ choose_opts_widths = get_column_widths(options)
1233
+ window_width = min(max(sum(choose_opts_widths) + 6, 50) + 6, w)
1234
+ window_height = min(h//2, max(6, len(options)+3))
1235
+
1236
+ submenu_win = curses.newwin(window_height, window_width, (h-window_height)//2, (w-window_width)//2)
1237
+ submenu_win.keypad(True)
1238
+ OptionPicker = Picker(submenu_win, **option_picker_data)
1239
+ s, o, f = OptionPicker.run()
1240
+
1241
+ if o == "refresh":
1242
+ self.draw_screen(self.indexed_items, self.highlights)
1243
+ continue
1244
+ if s:
1245
+ return {x: options[x] for x in s}, o, f
1246
+ return {}, "", f
1247
+
1248
+
1249
+
1250
+ def notification(self, stdscr: curses.window, message: str="", title:str="Notification", colours_end: int=0, duration:int=4) -> None:
1251
+
1252
+ self.logger.info(f"function: notification()")
1253
+ """ Notification box. """
1254
+ notification_width, notification_height = 50, 7
1255
+ message_width = notification_width-5
1256
+
1257
+ if not message: message = "!!"
1258
+ submenu_items = [" "+message[i*message_width:(i+1)*message_width] for i in range(len(message)//message_width+1)]
1259
+
1260
+ notification_remap_keys = {
1261
+ curses.KEY_RESIZE: curses.KEY_F5,
1262
+ 27: ord('q')
1263
+ }
1264
+ while True:
1265
+ h, w = stdscr.getmaxyx()
1266
+
1267
+ submenu_win = curses.newwin(notification_height, notification_width, 3, w - (notification_width+4))
1268
+ notification_data = {
1269
+ "items": submenu_items,
1270
+ "title": title,
1271
+ "colours_start": self.notification_colours_start,
1272
+ "show_footer": False,
1273
+ "centre_in_terminal": True,
1274
+ "centre_in_terminal_vertical": True,
1275
+ "centre_in_cols": True,
1276
+ "hidden_columns": [],
1277
+ "keys_dict": notification_keys,
1278
+ "disabled_keys": [ord('z'), ord('c')],
1279
+ "highlight_full_row": True,
1280
+ "top_gap": 0,
1281
+ "cancel_is_back": True,
1282
+ "reset_colours": False,
1283
+
1284
+ }
1285
+ OptionPicker = Picker(submenu_win, **notification_data)
1286
+ s, o, f = OptionPicker.run()
1287
+
1288
+ if o != "refresh": break
1289
+ submenu_win.clear()
1290
+ submenu_win.refresh()
1291
+ del submenu_win
1292
+ stdscr.clear()
1293
+ stdscr.refresh()
1294
+ self.draw_screen(self.indexed_items, self.highlights)
1295
+ # set_colours(colours=get_colours(0))
1296
+
1297
+ def toggle_column_visibility(self, col_index:int) -> None:
1298
+ """ Toggle the visibility of the column at col_index. """
1299
+ self.logger.info(f"function: toggle_column_visibility()")
1300
+ if 0 <= col_index < len(self.items[0]):
1301
+ if col_index in self.hidden_columns:
1302
+ self.hidden_columns.remove(col_index)
1303
+ else:
1304
+ self.hidden_columns.append(col_index)
1305
+
1306
+ def apply_settings(self) -> None:
1307
+ """
1308
+ The users settings will be stored in the user_settings variable. This function applies those settings.
1309
+
1310
+ ![0-9]+ show/hide column
1311
+ s[0-9]+ set column focus for sort
1312
+ g[0-9]+ go to index
1313
+ p[0-9]+ go to page
1314
+ nohl hide search highlights
1315
+ """
1316
+ self.logger.info(f"function: apply_settings()")
1317
+ if self.user_settings:
1318
+ settings = re.split(r'\s+', self.user_settings)
1319
+ for setting in settings:
1320
+ if len(setting) == 0: continue
1321
+
1322
+ if setting[0] == "!" and len(setting) > 1:
1323
+ if setting[1:].isnumeric():
1324
+ cols = setting[1:].split(",")
1325
+ for col in cols:
1326
+ self.toggle_column_visibility(int(col))
1327
+ elif setting[1] == "r":
1328
+ self.auto_refresh = not self.auto_refresh
1329
+ elif setting[1] == "h":
1330
+ self.highlights_hide = not self.highlights_hide
1331
+
1332
+ elif setting in ["nhl", "nohl", "nohighlights"]:
1333
+ # highlights = [highlight for highlight in highlights if "type" not in highlight or highlight["type"] != "search" ]
1334
+
1335
+ self.highlights_hide = not self.highlights_hide
1336
+ elif setting[0] == "s":
1337
+ if 0 <= int(setting[1:]) < len(self.items[0]):
1338
+ self.sort_column = int(setting[1:])
1339
+ if len(self.indexed_items):
1340
+ current_pos = self.indexed_items[self.cursor_pos][0]
1341
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort items based on new column
1342
+ if len(self.indexed_items):
1343
+ new_pos = [row[0] for row in self.indexed_items].index(current_pos)
1344
+ self.cursor_pos = new_pos
1345
+ elif setting == "ct":
1346
+ self.centre_in_terminal = not self.centre_in_terminal
1347
+ elif setting == "cc":
1348
+ self.centre_in_cols = not self.centre_in_cols
1349
+ elif setting == "cv":
1350
+ self.centre_in_terminal_vertical = not self.centre_in_terminal_vertical
1351
+ elif setting == "arb":
1352
+ self.insert_row(self.cursor_pos)
1353
+ elif setting == "ara":
1354
+ self.insert_row(self.cursor_pos+1)
1355
+ elif setting == "aca":
1356
+ self.insert_column(self.selected_column+1)
1357
+ elif setting == "acb":
1358
+ self.insert_column(self.selected_column)
1359
+ elif setting.startswith("ir"):
1360
+ if setting[2:].isnumeric():
1361
+ num = int(setting[2:])
1362
+ else:
1363
+ num = self.cursor_pos
1364
+ self.insert_row(num)
1365
+ elif setting.startswith("ic"):
1366
+ if setting[2:].isnumeric():
1367
+ num = int(setting[2:])
1368
+ else:
1369
+ num = self.selected_column
1370
+ self.insert_column(num)
1371
+
1372
+ elif setting == "modes":
1373
+ self.display_modes = not self.display_modes
1374
+ elif setting == "cell":
1375
+ self.cell_cursor = not self.cell_cursor
1376
+ elif setting == "rh":
1377
+ self.show_row_header = not self.show_row_header
1378
+ elif setting == "header":
1379
+ self.show_header = not self.show_header
1380
+ elif setting[0] == "":
1381
+ cols = setting[1:].split(",")
1382
+ elif setting == "footer":
1383
+ self.show_footer = not self.show_footer
1384
+ self.initialise_variables()
1385
+ elif setting == "pc":
1386
+ self.pin_cursor = not self.pin_cursor
1387
+ elif setting == "unicode":
1388
+ self.unicode_char_width = not self.unicode_char_width
1389
+
1390
+ elif setting.startswith("ft"):
1391
+ if len(setting) > 2 and setting[2:].isnumeric():
1392
+
1393
+ num = int(setting[2:])
1394
+ self.footer_style = max(len(self.footer_options)-1, num)
1395
+ self.footer = self.footer_options[self.footer_style]
1396
+ else:
1397
+ self.footer_style = (self.footer_style+1)%len(self.footer_options)
1398
+ self.footer = self.footer_options[self.footer_style]
1399
+ self.initialise_variables()
1400
+
1401
+ elif setting.startswith("cwd="):
1402
+ os.chdir(os.path.expandvars(os.path.expanduser(setting[len("cwd="):])))
1403
+ elif setting.startswith("hl"):
1404
+ hl_list = setting.split(",")
1405
+ if len(hl_list) > 1:
1406
+ hl_list = hl_list[1:]
1407
+ match = hl_list[0]
1408
+ if len(hl_list) > 1:
1409
+ field = hl_list[1]
1410
+ if field.isnumeric() and field != "-1":
1411
+ field = int(field)
1412
+ else:
1413
+ field = "all"
1414
+ else:
1415
+ field = "all"
1416
+ if len(hl_list) > 2 and hl_list[2].isnumeric():
1417
+ colour_pair = int(hl_list[2])
1418
+ else:
1419
+ colour_pair = 10
1420
+
1421
+ highlight = {
1422
+ "match": match,
1423
+ "field": field,
1424
+ "color": colour_pair
1425
+ }
1426
+ self.highlights.append(highlight)
1427
+
1428
+
1429
+ elif setting.startswith("th"):
1430
+ global COLOURS_SET
1431
+ if curses.COLORS < 255:
1432
+ self.notification(self.stdscr, message=f"Theme 4 applied.")
1433
+
1434
+ elif setting[2:].strip().isnumeric():
1435
+ COLOURS_SET = False
1436
+ try:
1437
+ theme_number = int(setting[2:].strip())
1438
+ self.colour_theme_number = min(get_theme_count()-1, theme_number)
1439
+ set_colours(self.colour_theme_number)
1440
+ self.draw_screen(self.indexed_items, self.highlights)
1441
+ self.notification(self.stdscr, message=f"Theme {self.colour_theme_number} applied.")
1442
+ except:
1443
+ pass
1444
+ else:
1445
+ COLOURS_SET = False
1446
+ self.colour_theme_number = (self.colour_theme_number + 1)%get_theme_count()
1447
+ # self.colour_theme_number = int(not bool(self.colour_theme_number))
1448
+ set_colours(self.colour_theme_number)
1449
+ self.draw_screen(self.indexed_items, self.highlights)
1450
+ self.notification(self.stdscr, message=f"Theme {self.colour_theme_number} applied.")
1451
+
1452
+ else:
1453
+ self.user_settings = ""
1454
+ return None
1455
+
1456
+
1457
+ self.command_stack.append(Command("setting", self.user_settings))
1458
+ self.user_settings = ""
1459
+
1460
+ def apply_command(self, command: Command):
1461
+ self.logger.info(f"function: apply_command()")
1462
+ if command.command_type == "setting":
1463
+ self.user_settings = command.command_value
1464
+ self.apply_settings()
1465
+
1466
+ def redo(self):
1467
+ self.logger.info(f"function: redo()")
1468
+ if len(self.command_stack):
1469
+ self.apply_command(self.command_stack[-1])
1470
+
1471
+ def toggle_item(self, index: int) -> None:
1472
+ """ Toggle selection of item at index. """
1473
+ self.logger.info(f"function: toggle_item()")
1474
+ self.selections[index] = not self.selections[index]
1475
+ self.draw_screen(self.indexed_items, self.highlights)
1476
+
1477
+ def select_all(self) -> None:
1478
+ """ Select all in indexed_items. """
1479
+ self.logger.info(f"function: select_all()")
1480
+ for i in range(len(self.indexed_items)):
1481
+ self.selections[self.indexed_items[i][0]] = True
1482
+ for i in self.cell_selections.keys():
1483
+ self.cell_selections[i] = True
1484
+
1485
+ self.draw_screen(self.indexed_items, self.highlights)
1486
+
1487
+ def deselect_all(self) -> None:
1488
+ """ Deselect all items in indexed_items. """
1489
+ self.logger.info(f"function: deselect_all()")
1490
+ for i in range(len(self.selections)):
1491
+ self.selections[i] = False
1492
+ for i in self.cell_selections.keys():
1493
+ self.cell_selections[i] = False
1494
+ self.draw_screen(self.indexed_items, self.highlights)
1495
+
1496
+ def handle_visual_selection(self, selecting:bool = True) -> None:
1497
+ """ Toggle visual selection or deselection. """
1498
+ self.logger.info(f"function: handle_visual_selection()")
1499
+ if not self.is_selecting and not self.is_deselecting and len(self.indexed_items) and len(self.indexed_items[0][1]):
1500
+ self.start_selection = self.cursor_pos
1501
+ self.start_selection_col = self.selected_column
1502
+ if selecting:
1503
+ self.is_selecting = True
1504
+ else:
1505
+ self.is_deselecting = True
1506
+ elif self.is_selecting:
1507
+ # end_selection = indexed_items[current_page * items_per_page + current_row][0]
1508
+ self.end_selection = self.cursor_pos
1509
+ if self.start_selection != -1:
1510
+ start = max(min(self.start_selection, self.end_selection), 0)
1511
+ end = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1512
+ for i in range(start, end + 1):
1513
+ if self.indexed_items[i][0] not in self.unselectable_indices:
1514
+ self.selections[self.indexed_items[i][0]] = True
1515
+ if self.start_selection != -1:
1516
+ ystart = max(min(self.start_selection, self.end_selection), 0)
1517
+ yend = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1518
+ xstart = min(self.start_selection_col, self.selected_column)
1519
+ xend = max(self.start_selection_col, self.selected_column)
1520
+ for i in range(ystart, yend + 1):
1521
+ if self.indexed_items[i][0] not in self.unselectable_indices:
1522
+ row = self.indexed_items[i][0]
1523
+ if row not in self.selected_cells_by_row:
1524
+ self.selected_cells_by_row[row] = []
1525
+
1526
+ for col in range(xstart, xend+1):
1527
+ cell_index = (row, col)
1528
+ self.cell_selections[cell_index] = True
1529
+
1530
+ self.selected_cells_by_row[row].append(col)
1531
+ # Remove duplicates
1532
+ self.selected_cells_by_row[row] = list(set(self.selected_cells_by_row[row]))
1533
+
1534
+ self.start_selection = -1
1535
+ self.end_selection = -1
1536
+ self.is_selecting = False
1537
+
1538
+ self.draw_screen(self.indexed_items, self.highlights)
1539
+
1540
+ elif self.is_deselecting:
1541
+ self.end_selection = self.indexed_items[self.cursor_pos][0]
1542
+ self.end_selection = self.cursor_pos
1543
+ if self.start_selection != -1:
1544
+ start = max(min(self.start_selection, self.end_selection), 0)
1545
+ end = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1546
+ for i in range(start, end + 1):
1547
+ # selections[i] = False
1548
+ self.selections[self.indexed_items[i][0]] = False
1549
+ if self.start_selection != -1:
1550
+ ystart = max(min(self.start_selection, self.end_selection), 0)
1551
+ yend = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1552
+ xstart = min(self.start_selection_col, self.selected_column)
1553
+ xend = max(self.start_selection_col, self.selected_column)
1554
+ for i in range(ystart, yend + 1):
1555
+ if self.indexed_items[i][0] not in self.unselectable_indices:
1556
+ for j in range(xstart, xend+1):
1557
+ cell_index = (self.indexed_items[i][0], j)
1558
+ self.cell_selections[cell_index] = False
1559
+ self.start_selection = -1
1560
+ self.end_selection = -1
1561
+ self.is_deselecting = False
1562
+ self.draw_screen(self.indexed_items, self.highlights)
1563
+
1564
+ def cursor_down(self, count=1) -> bool:
1565
+ """ Move cursor down. """
1566
+ self.logger.info(f"function: cursor_down()")
1567
+ # Returns: whether page is turned
1568
+ new_pos = self.cursor_pos + 1
1569
+ new_pos = min(self.cursor_pos+count, len(self.indexed_items)-1)
1570
+ while True:
1571
+ if self.indexed_items[new_pos][0] in self.unselectable_indices: new_pos+=1
1572
+ else: break
1573
+ self.cursor_pos = new_pos
1574
+ return True
1575
+
1576
+ def cursor_up(self, count=1) -> bool:
1577
+ """ Move cursor up. """
1578
+ self.logger.info(f"function: cursor_up()")
1579
+ # Returns: whether page is turned
1580
+
1581
+ new_pos = max(self.cursor_pos - count, 0)
1582
+ while True:
1583
+ if new_pos < 0: return False
1584
+ elif new_pos in self.unselectable_indices: new_pos -= 1
1585
+ else: break
1586
+ self.cursor_pos = new_pos
1587
+ return True
1588
+
1589
+ def remapped_key(self, key: int, val: int, key_remappings: dict) -> bool:
1590
+ """ Check if key has been remapped to val in key_remappings. """
1591
+ # self.logger.info(f"function: remapped_key()")
1592
+ if key in key_remappings:
1593
+ if key_remappings[key] == val or (isinstance(key_remappings[key], list) and val in key_remappings[key]):
1594
+ return True
1595
+ return False
1596
+
1597
+ def check_key(self, function: str, key: int, keys_dict: dict) -> bool:
1598
+ """
1599
+ Check if $key is assigned to $function in the keys_dict.
1600
+ Allows us to redefine functions to different keys in the keys_dict.
1601
+
1602
+ E.g., keys_dict = { $key, "help": ord('?') },
1603
+ """
1604
+ if function in keys_dict and key in keys_dict[function]:
1605
+ return True
1606
+ return False
1607
+
1608
+ def copy_dialogue(self) -> None:
1609
+ """ Display dialogue to select how rows/cells should be copied. """
1610
+ self.logger.info(f"function: copy_dialogue()")
1611
+ copy_header = [
1612
+ "Representation",
1613
+ "Columns",
1614
+ ]
1615
+ options = [
1616
+ ["Python list of lists", "Exclude hidden"],
1617
+ ["Python list of lists", "Include hidden"],
1618
+ ["Tab-separated values", "Exclude hidden"],
1619
+ ["Tab-separated values", "Include hidden"],
1620
+ ["Comma-separated values", "Exclude hidden"],
1621
+ ["Comma-separated values", "Include hidden"],
1622
+ ["Custom separator", "Exclude hidden"],
1623
+ ["Custom separator", "Include hidden"],
1624
+ ]
1625
+ require_option = [False, False, False, False, False, False, True, True]
1626
+ s, o, f = self.choose_option(self.stdscr, options=options, title="Copy selected", header=copy_header, require_option=require_option)
1627
+
1628
+
1629
+ funcs = [
1630
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="python", copy_hidden_cols=False, cellwise=cell_cursor),
1631
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="python", copy_hidden_cols=True, cellwise=cell_cursor),
1632
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="tsv", copy_hidden_cols=False, cellwise=cell_cursor),
1633
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="tsv", copy_hidden_cols=True, cellwise=cell_cursor),
1634
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="csv", copy_hidden_cols=False, cellwise=cell_cursor),
1635
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="csv", copy_hidden_cols=True, cellwise=cell_cursor),
1636
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="custom_sv", copy_hidden_cols=False, separator=o, cellwise=cell_cursor),
1637
+ lambda items, indexed_items, selections, cell_selections, hidden_columns, cell_cursor: copy_to_clipboard(items, indexed_items, selections, cell_selections, hidden_columns, representation="custom_sv", copy_hidden_cols=True, separator=o, cellwise=cell_cursor),
1638
+ ]
1639
+
1640
+ # Copy items based on selection
1641
+ if s:
1642
+ for idx in s.keys():
1643
+ funcs[idx](self.items, self.indexed_items, self.selections, self.cell_selections, self.hidden_columns, self.cell_cursor)
1644
+ def paste_dialogue(self) -> None:
1645
+ """ Display dialogue to select how to paste from the clipboard. """
1646
+ self.logger.info(f"function: paste_dialogue()")
1647
+ paste_header = [
1648
+ "Representation",
1649
+ "Columns",
1650
+ ]
1651
+ options = [
1652
+ ["Paste values", ""],
1653
+ ]
1654
+ require_option = [False]
1655
+ s, o, f = self.choose_option(self.stdscr, options=options, title="Paste values", header=paste_header, require_option=require_option)
1656
+
1657
+
1658
+ funcs = [
1659
+ lambda items, pasta, paste_row, paste_col: paste_values(items, pasta, paste_row, paste_col)
1660
+ ]
1661
+
1662
+ try:
1663
+ pasta = eval(pyperclip.paste())
1664
+ if type(pasta) == type([]):
1665
+ acceptable_data_type = True
1666
+ for row in pasta:
1667
+ if type(row) != type([]):
1668
+ acceptable_data_type = False
1669
+ break
1670
+
1671
+ for cell in row:
1672
+ if cell != None and type(cell) != type(""):
1673
+ acceptable_data_type = False
1674
+ break
1675
+ if not acceptable_data_type:
1676
+ break
1677
+ if not acceptable_data_type:
1678
+ self.draw_screen(self.indexed_items, self.highlights)
1679
+ self.notification(self.stdscr, message="Error pasting data.")
1680
+ return None
1681
+
1682
+ except:
1683
+ self.draw_screen(self.indexed_items, self.highlights)
1684
+ self.notification(self.stdscr, message="Error pasting data.")
1685
+ return None
1686
+ if type(pasta) == type([]) and len(pasta) > 0 and type(pasta[0]) == type([]):
1687
+ if s:
1688
+ for idx in s.keys():
1689
+ return_val, tmp_items = funcs[idx](self.items, pasta, self.cursor_pos, self.selected_column)
1690
+ if return_val:
1691
+ cursor_pos = self.cursor_pos
1692
+ self.items = tmp_items
1693
+ self.initialise_variables()
1694
+ self.cursor_pos = cursor_pos
1695
+
1696
+ def save_dialog(self) -> None:
1697
+ """ Display dialogue to select how to save the picker data. """
1698
+ self.logger.info(f"function: save_dialog()")
1699
+
1700
+ dump_header = []
1701
+ options = [
1702
+ ["Save data (pickle)."],
1703
+ ["Save data (csv)."],
1704
+ ["Save data (tsv)."],
1705
+ ["Save data (json)."],
1706
+ ["Save data (feather)."],
1707
+ ["Save data (parquet)."],
1708
+ ["Save data (msgpack)."],
1709
+ ["Save state"]
1710
+ ]
1711
+ # require_option = [True, True, True, True, True, True, True, True]
1712
+ s, o, f = self.choose_option(self.stdscr, options=options, title="Save...", header=dump_header)
1713
+
1714
+
1715
+ funcs = [
1716
+ lambda opts: dump_data(self.get_function_data(), opts),
1717
+ lambda opts: dump_data(self.get_function_data(), opts, format="csv"),
1718
+ lambda opts: dump_data(self.get_function_data(), opts, format="tsv"),
1719
+ lambda opts: dump_data(self.get_function_data(), opts, format="json"),
1720
+ lambda opts: dump_data(self.get_function_data(), opts, format="feather"),
1721
+ lambda opts: dump_data(self.get_function_data(), opts, format="parquet"),
1722
+ lambda opts: dump_data(self.get_function_data(), opts, format="msgpack"),
1723
+ lambda opts: dump_state(self.get_function_data(), opts),
1724
+ ]
1725
+
1726
+ if s:
1727
+ for idx in s.keys():
1728
+ save_path_entered, save_path = output_file_option_selector(
1729
+ self.stdscr,
1730
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights)
1731
+ )
1732
+ if save_path_entered:
1733
+ return_val = funcs[idx](save_path)
1734
+ if return_val:
1735
+ self.notification(self.stdscr, message=return_val, title="Error")
1736
+
1737
+ def load_dialog(self) -> None:
1738
+ """ Display dialogue to select which file to load and in what way it should be loaded. """
1739
+ self.logger.info(f"function: load_dialog()")
1740
+
1741
+ dump_header = []
1742
+ options = [
1743
+ ["Load data (pickle)."],
1744
+ ]
1745
+ s, o, f = self.choose_option(self.stdscr, options=options, title="Open file...", header=dump_header)
1746
+
1747
+
1748
+ funcs = [
1749
+ lambda opts: load_state(opts)
1750
+ ]
1751
+
1752
+ if s:
1753
+ file_to_load = file_picker()
1754
+ if file_to_load:
1755
+ index = list(s.keys())[0]
1756
+ return_val = funcs[index](file_to_load)
1757
+ self.set_function_data(return_val)
1758
+
1759
+ # items = return_val["items"]
1760
+ # header = return_val["header"]
1761
+ self.initialise_variables()
1762
+ self.draw_screen(self.indexed_items, self.highlights)
1763
+ self.notification(self.stdscr, f"{repr(file_to_load)} has been loaded!")
1764
+ # if return_val:
1765
+ # notification(stdscr, message=return_val, title="Error")
1766
+
1767
+ def set_registers(self):
1768
+ """ Set registers to be sent to the input field. """
1769
+ self.logger.info(f"function: set_registers()")
1770
+ self.registers = {"*": self.indexed_items[self.cursor_pos][1][self.selected_column]} if len(self.indexed_items) and len(self.indexed_items[0][1]) else {}
1771
+
1772
+
1773
+ def fetch_data(self) -> None:
1774
+ """ Refesh data asynchronously. When data has been fetched self.data_ready is set to True. """
1775
+ self.logger.info(f"function: fetch_data()")
1776
+ tmp_items, tmp_header = self.refresh_function()
1777
+ if self.track_entries_upon_refresh:
1778
+ selected_indices = get_selected_indices(self.selections)
1779
+ self.ids = [item[self.id_column] for i, item in enumerate(self.items) if i in selected_indices]
1780
+ self.ids_tuples = [(i, item[self.id_column]) for i, item in enumerate(self.items) if i in selected_indices]
1781
+ self.selected_cells_by_row = get_selected_cells_by_row(self.cell_selections)
1782
+
1783
+ if len(self.indexed_items) > 0 and len(self.indexed_items) >= self.cursor_pos and len(self.indexed_items[0][1]) >= self.id_column:
1784
+ self.cursor_pos_id = self.indexed_items[self.cursor_pos][1][self.id_column]
1785
+ with self.data_lock:
1786
+ self.items, self.header = tmp_items, tmp_header
1787
+ self.data_ready = True
1788
+
1789
+ def save_input_history(self, file_path: str) -> bool:
1790
+ """ Save input field history. Returns True if successful save. """
1791
+ self.logger.info(f"function: save_input_history()")
1792
+ file_path = os.path.expanduser(file_path)
1793
+ history_dict = {
1794
+ "history_filter_and_search" : self.history_filter_and_search,
1795
+ "history_pipes" : self.history_pipes,
1796
+ "history_opts" : self.history_opts,
1797
+ "history_edits" : self.history_edits,
1798
+ "history_settings": self.history_settings,
1799
+ }
1800
+ with open(file_path, 'w') as f:
1801
+ json.dump(history_dict, f)
1802
+
1803
+ return True
1804
+
1805
+ def load_input_history(self, file_path:str) -> bool:
1806
+ """ Load command history. Returns true if successful load. """
1807
+ self.logger.info(f"function: load_input_history()")
1808
+ file_path = os.path.expanduser(file_path)
1809
+ if not os.path.exists(file_path):
1810
+ return False
1811
+ try:
1812
+ with open(file_path, 'r') as f:
1813
+ history_dict = json.load(f)
1814
+
1815
+ if "history_filter_and_search" in history_dict:
1816
+ self.history_filter_and_search = history_dict["history_filter_and_search"]
1817
+ if "history_pipes" in history_dict:
1818
+ self.history_pipes = history_dict["history_pipes"]
1819
+ if "history_opts" in history_dict:
1820
+ self.history_opts = history_dict["history_opts"]
1821
+ if "history_edits" in history_dict:
1822
+ self.history_edits = history_dict["history_edits"]
1823
+ if "history_settings" in history_dict:
1824
+ self.history_settings = history_dict["history_settings"]
1825
+
1826
+ except:
1827
+ return False
1828
+
1829
+ return True
1830
+
1831
+
1832
+ def get_word_list(self) -> list[str]:
1833
+ """ Get a list of all words used in any cell of the picker. Used for completion in search/filter input_field. """
1834
+ self.logger.info(f"function: infobox()")
1835
+ translator = str.maketrans('', '', string.punctuation)
1836
+
1837
+ words = []
1838
+ # Extract words from lists
1839
+ for row in [x[1] for x in self.indexed_items]:
1840
+ for i, cell in enumerate(row):
1841
+ if i != (self.id_column%len(row)):
1842
+ # Split the item into words and strip punctuation from each word
1843
+ words.extend(word.strip(string.punctuation) for word in cell.split())
1844
+ for cell in self.header:
1845
+ # Split the item into words and strip punctuation from each word
1846
+ words.extend(word.strip(string.punctuation) for word in cell.split())
1847
+ def key_f(s):
1848
+ if len(s):
1849
+ starts_with_char = s[0].isalpha()
1850
+ else:
1851
+ starts_with_char = False
1852
+ return (not starts_with_char, s.lower())
1853
+ # key = lambda s: (s != "" or not s[0].isalpha(), s)
1854
+ words = sorted(list(set(words)), key=key_f)
1855
+ return words
1856
+
1857
+ def insert_row(self, pos: int):
1858
+ """ Insert a blank row at position `pos` """
1859
+ self.logger.info(f"function: insert_row(pos={pos})")
1860
+
1861
+ if self.items != [[]]:
1862
+ row_len = 1
1863
+ if self.header: row_len = len(self.header)
1864
+ elif len(self.items): row_len = len(self.items[0])
1865
+ # if len(self.indexed_items) == 0:
1866
+ # insert_at_pos = 0
1867
+ # else:
1868
+ # insert_at_pos = self.indexed_items[self.cursor_pos][0]
1869
+ self.items = self.items[:pos] + [["" for x in range(row_len)]] + self.items[pos:]
1870
+ if pos <= self.cursor_pos:
1871
+ self.cursor_pos += 1
1872
+ # We are adding a row before so we have to move the cursor down
1873
+ # If there is a filter then we know that an empty row doesn't match
1874
+ current_cursor_pos = self.cursor_pos
1875
+ self.initialise_variables()
1876
+ self.cursor_pos = current_cursor_pos
1877
+ else:
1878
+ self.items = [[""]]
1879
+ self.initialise_variables()
1880
+
1881
+ def insert_column(self, pos:int):
1882
+ """ Insert blank column at `pos`"""
1883
+ self.logger.info(f"function: insert_column(pos={pos})")
1884
+ self.items = [row[:pos]+[""]+row[pos:] for row in self.items]
1885
+ self.header = self.header[:pos] + [""] + self.header[pos:]
1886
+ self.editable_columns = self.editable_columns[:pos] + [self.editable_by_default] + self.editable_columns[pos:]
1887
+ if pos <= self.selected_column:
1888
+ self.selected_column += 1
1889
+ current_cursor_pos = self.cursor_pos
1890
+ self.initialise_variables()
1891
+ self.cursor_pos = current_cursor_pos
1892
+
1893
+
1894
+
1895
+ def run(self) -> Tuple[list[int], str, dict]:
1896
+ """ Run the picker. """
1897
+ self.logger.info(f"function: run()")
1898
+
1899
+ if self.get_footer_string_startup and self.footer_string_refresh_function != None:
1900
+ self.footer_string = self.footer_string_refresh_function()
1901
+
1902
+ self.initialise_variables(get_data=self.get_data_startup)
1903
+
1904
+ self.draw_screen(self.indexed_items, self.highlights)
1905
+
1906
+ initial_time = time.time()
1907
+ initial_time_footer = time.time()-self.footer_timer
1908
+
1909
+ if self.startup_notification:
1910
+ self.notification(self.stdscr, message=self.startup_notification)
1911
+ self.startup_notification = ""
1912
+
1913
+ # curses.curs_set(0)
1914
+ # stdscr.nodelay(1) # Non-blocking input
1915
+ # stdscr.timeout(2000) # Set a timeout for getch() to ensure it does not block indefinitely
1916
+ self.stdscr.timeout(max(min(2000, int(self.timer*1000)//2, int(self.footer_timer*1000))//2, 20)) # Set a timeout for getch() to ensure it does not block indefinitely
1917
+
1918
+ if self.clear_on_start:
1919
+ self.stdscr.clear()
1920
+ self.clear_on_start = False
1921
+ else:
1922
+ self.stdscr.erase()
1923
+
1924
+ self.stdscr.refresh()
1925
+
1926
+ # Initialize colours
1927
+ # Check if terminal supports color
1928
+
1929
+ # Set terminal background color
1930
+ self.stdscr.bkgd(' ', curses.color_pair(self.colours_start+3)) # Apply background color
1931
+ self.draw_screen(self.indexed_items, self.highlights)
1932
+
1933
+ if self.display_only:
1934
+ self.stdscr.refresh()
1935
+ function_data = self.get_function_data()
1936
+ return [], "", function_data
1937
+
1938
+ # Main loop
1939
+
1940
+ while True:
1941
+ key = self.stdscr.getch()
1942
+ if key in self.disabled_keys: continue
1943
+ clear_screen=True
1944
+
1945
+ ## Refresh data asyncronously.
1946
+ if self.refreshing_data:
1947
+ self.logger.debug(f"Data refresh check")
1948
+ with self.data_lock:
1949
+ if self.data_ready:
1950
+ self.logger.debug(f"Data ready after refresh")
1951
+ self.initialise_variables()
1952
+
1953
+ initial_time = time.time()
1954
+
1955
+ self.draw_screen(self.indexed_items, self.highlights, clear=False)
1956
+
1957
+ self.refreshing_data = False
1958
+ self.data_ready = False
1959
+
1960
+ elif self.check_key("refresh", key, self.keys_dict) or self.remapped_key(key, curses.KEY_F5, self.key_remappings) or (self.auto_refresh and (time.time() - initial_time) >= self.timer):
1961
+ self.logger.debug(f"Get new data (refresh).")
1962
+ h, w = self.stdscr.getmaxyx()
1963
+ self.stdscr.addstr(0,w-3,"  ", curses.color_pair(self.colours_start+21) | curses.A_BOLD)
1964
+ self.stdscr.refresh()
1965
+ if self.get_new_data and self.refresh_function:
1966
+ self.refreshing_data = True
1967
+
1968
+ t = threading.Thread(target=self.fetch_data)
1969
+ t.start()
1970
+ else:
1971
+ function_data = self.get_function_data()
1972
+ return [], "refresh", function_data
1973
+
1974
+ # Refresh data synchronously
1975
+ # if self.check_key("refresh", key, self.keys_dict) or self.remapped_key(key, curses.KEY_F5, self.key_remappings) or (self.auto_refresh and (time.time() - initial_time) > self.timer):
1976
+ # h, w = self.stdscr.getmaxyx()
1977
+ # self.stdscr.addstr(0,w-3,"  ", curses.color_pair(self.colours_start+21) | curses.A_BOLD)
1978
+ # self.stdscr.refresh()
1979
+ # if self.get_new_data and self.refresh_function:
1980
+ # self.initialise_variables(get_data=True)
1981
+ #
1982
+ # initial_time = time.time()
1983
+ # self.draw_screen(self.indexed_items, self.highlights, clear=False)
1984
+ # else:
1985
+ #
1986
+ # function_data = self.get_function_data()
1987
+ # return [], "refresh", function_data
1988
+
1989
+ if self.footer_string_auto_refresh and ((time.time() - initial_time_footer) > self.footer_timer):
1990
+ self.logger.debug(f"footer_string_auto_refresh")
1991
+ self.footer_string = self.footer_string_refresh_function()
1992
+ initial_time_footer = time.time()
1993
+ self.draw_screen(self.indexed_items, self.highlights)
1994
+
1995
+ if self.check_key("help", key, self.keys_dict):
1996
+ self.logger.info(f"key_function help")
1997
+ self.stdscr.clear()
1998
+ self.stdscr.refresh()
1999
+ help_data = {
2000
+ # "items": help_lines,
2001
+ "items": build_help_rows(self.keys_dict),
2002
+ "title": f"{self.title} Help",
2003
+ "colours_start": self.help_colours_start,
2004
+ "colours": help_colours,
2005
+ "show_footer": True,
2006
+ "max_selected": 1,
2007
+ "keys_dict": help_keys,
2008
+ "disabled_keys": [ord('?'), ord('v'), ord('V'), ord('m'), ord('M'), ord('l'), curses.KEY_ENTER, ord('\n')],
2009
+ "highlight_full_row": True,
2010
+ "top_gap": 0,
2011
+ "paginate": self.paginate,
2012
+ "centre_in_terminal": True,
2013
+ "centre_in_terminal_vertical": True,
2014
+ "hidden_columns": [],
2015
+ "reset_colours": False,
2016
+
2017
+ }
2018
+ OptionPicker = Picker(self.stdscr, **help_data)
2019
+ s, o, f = OptionPicker.run()
2020
+
2021
+ elif self.check_key("exit", key, self.keys_dict):
2022
+ self.stdscr.clear()
2023
+ function_data = self.get_function_data()
2024
+ function_data["last_key"] = key
2025
+ return [], "", function_data
2026
+ elif self.check_key("full_exit", key, self.keys_dict):
2027
+ close_curses(self.stdscr)
2028
+ exit()
2029
+
2030
+ elif self.check_key("settings_input", key, self.keys_dict):
2031
+ self.logger.info(f"Settings input")
2032
+ usrtxt = f"{self.user_settings.strip()} " if self.user_settings else ""
2033
+ field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2034
+ if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2035
+ else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2036
+ self.set_registers()
2037
+ usrtxt, return_val = input_field(
2038
+ self.stdscr,
2039
+ usrtxt=usrtxt,
2040
+ field_prefix=" Settings: ",
2041
+ x=lambda:2,
2042
+ y=lambda: self.stdscr.getmaxyx()[0]-1,
2043
+ max_length=field_end_f,
2044
+ registers=self.registers,
2045
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2046
+ history=self.history_settings,
2047
+ path_auto_complete=True,
2048
+ formula_auto_complete=False,
2049
+ function_auto_complete=False,
2050
+ word_auto_complete=True,
2051
+ auto_complete_words=["ft", "ct", "cv"]
2052
+ )
2053
+ if return_val:
2054
+ self.user_settings = usrtxt
2055
+ self.apply_settings()
2056
+ self.history_settings.append(usrtxt)
2057
+ self.user_settings = ""
2058
+ elif self.check_key("toggle_footer", key, self.keys_dict):
2059
+ self.logger.info(f"toggle footer")
2060
+ self.user_settings = "footer"
2061
+ self.apply_settings()
2062
+
2063
+ elif self.check_key("settings_options", key, self.keys_dict):
2064
+ options = []
2065
+ if len(self.items) > 0:
2066
+ options += [["cv", "Centre rows vertically"]]
2067
+ options += [["pc", "Pin cursor to row number when data refreshes"]]
2068
+ options += [["ct", "Centre column-set in terminal"]]
2069
+ options += [["cc", "Centre values in cells"]]
2070
+ options += [["!r", "Toggle auto-refresh"]]
2071
+ options += [["th", "Cycle between themes. (accepts th#)"]]
2072
+ options += [["nohl", "Toggle highlights"]]
2073
+ options += [["footer", "Toggle footer"]]
2074
+ options += [["header", "Toggle header"]]
2075
+ options += [["rh", "Toggle row header"]]
2076
+ options += [["modes", "Toggle modes"]]
2077
+ options += [["ft", "Cycle through footer styles (accepts ft#)"]]
2078
+ options += [["unicode", "Toggle b/w using len and wcwidth to calculate char width."]]
2079
+ options += [[f"s{i}", f"Select col. {i}"] for i in range(len(self.items[0]))]
2080
+ options += [[f"!{i}", f"Toggle col. {i}"] for i in range(len(self.items[0]))]
2081
+ options += [["ara", "Add empty row after cursor."]]
2082
+ options += [["arb", "Add empty row before the cursor."]]
2083
+ options += [["aca", "Add empty column after the selected column."]]
2084
+ options += [["acb", "Add empty column before the selected column."]]
2085
+
2086
+
2087
+ settings_options_header = ["Key", "Setting"]
2088
+
2089
+ s, o, f = self.choose_option(self.stdscr, options=options, title="Settings", header=settings_options_header)
2090
+ if s:
2091
+ self.user_settings = " ".join([x[0] for x in s.values()])
2092
+ self.apply_settings()
2093
+
2094
+ elif self.check_key("redo", key, self.keys_dict):
2095
+ self.redo()
2096
+ # elif self.check_key("move_column_left", key, self.keys_dict):
2097
+ # tmp1 = self.column_indices[self.selected_column]
2098
+ # tmp2 = self.column_indices[(self.selected_column-1)%len(self.column_indices)]
2099
+ # self.column_indices[self.selected_column] = tmp2
2100
+ # self.column_indices[(self.selected_column-1)%(len(self.column_indices))] = tmp1
2101
+ # self.selected_column = (self.selected_column-1)%len(self.column_indices)
2102
+ # # self.notification(self.stdscr, f"{str(self.column_indices)}, {tmp1}, {tmp2}")
2103
+ # self.initialise_variables()
2104
+ # self.column_widths = get_column_widths([v[1] for v in self.indexed_items], header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2105
+ # self.draw_screen(self.indexed_items, self.highlights)
2106
+ # # self.move_column(direction=-1)
2107
+ #
2108
+ # elif self.check_key("move_column_right", key, self.keys_dict):
2109
+ # tmp1 = self.column_indices[self.selected_column]
2110
+ # tmp2 = self.column_indices[(self.selected_column+1)%len(self.column_indices)]
2111
+ # self.column_indices[self.selected_column] = tmp2
2112
+ # self.column_indices[(self.selected_column+1)%(len(self.column_indices))] = tmp1
2113
+ # self.selected_column = (self.selected_column+1)%len(self.column_indices)
2114
+ # self.initialise_variables()
2115
+ # self.draw_screen(self.indexed_items, self.highlights)
2116
+ # # self.move_column(direction=1)
2117
+
2118
+ elif self.check_key("cursor_down", key, self.keys_dict):
2119
+ page_turned = self.cursor_down()
2120
+ if not page_turned: clear_screen = False
2121
+ elif self.check_key("half_page_down", key, self.keys_dict):
2122
+ clear_screen = False
2123
+ self.cursor_down(count=self.items_per_page//2)
2124
+ elif self.check_key("five_down", key, self.keys_dict):
2125
+ clear_screen = False
2126
+ self.cursor_down(count=5)
2127
+ elif self.check_key("cursor_up", key, self.keys_dict):
2128
+ page_turned = self.cursor_up()
2129
+ if not page_turned: clear_screen = False
2130
+ elif self.check_key("five_up", key, self.keys_dict):
2131
+ # if self.cursor_up(count=5): clear_screen = True
2132
+ self.cursor_up(count=5)
2133
+ elif self.check_key("half_page_up", key, self.keys_dict):
2134
+ self.cursor_up(count=self.items_per_page//2)
2135
+
2136
+ elif self.check_key("toggle_select", key, self.keys_dict):
2137
+ if len(self.indexed_items) > 0:
2138
+ item_index = self.indexed_items[self.cursor_pos][0]
2139
+ cell_index = (self.indexed_items[self.cursor_pos][0], self.selected_column)
2140
+ selected_count = sum(self.selections.values())
2141
+ if self.max_selected == -1 or selected_count >= self.max_selected:
2142
+ self.toggle_item(item_index)
2143
+
2144
+ self.cell_selections[cell_index] = not self.cell_selections[cell_index]
2145
+ ## Set self.selected_cells_by_row
2146
+ # If any cells in the current row are selected
2147
+ if row in self.selected_cells_by_row:
2148
+ # If the current cell is selected then remove it
2149
+ if col in self.selected_cells_by_row[row]:
2150
+ # If the current cell is the only cell in the row that is selected then remove the row from the dict
2151
+ if len(self.selected_cells_by_row[row]) == 1:
2152
+
2153
+ del self.selected_cells_by_row[row]
2154
+ # else remove only the index of the current cell
2155
+ else:
2156
+ self.selected_cells_by_row[row].remove(col)
2157
+ # If there are cells in the row that are selected then append the current cell to the row
2158
+ else:
2159
+ self.selected_cells_by_row[row].append(col)
2160
+ # Add the a list containing only the current column
2161
+ else:
2162
+ self.selected_cells_by_row[row] = [col]
2163
+
2164
+ self.cursor_down()
2165
+ elif self.check_key("select_all", key, self.keys_dict): # Select all (m or ctrl-a)
2166
+ self.select_all()
2167
+
2168
+ elif self.check_key("select_none", key, self.keys_dict): # Deselect all (M or ctrl-r)
2169
+ self.deselect_all()
2170
+
2171
+ elif self.check_key("cursor_top", key, self.keys_dict):
2172
+ new_pos = 0
2173
+ while True:
2174
+ if new_pos in self.unselectable_indices: new_pos+=1
2175
+ else: break
2176
+ if new_pos < len(self.indexed_items):
2177
+ self.cursor_pos = new_pos
2178
+
2179
+ self.draw_screen(self.indexed_items, self.highlights)
2180
+
2181
+ elif self.check_key("cursor_bottom", key, self.keys_dict):
2182
+ new_pos = len(self.indexed_items)-1
2183
+ while True:
2184
+ if new_pos in self.unselectable_indices: new_pos-=1
2185
+ else: break
2186
+ if new_pos < len(self.items) and new_pos >= 0:
2187
+ self.cursor_pos = new_pos
2188
+
2189
+ elif self.check_key("enter", key, self.keys_dict):
2190
+ self.logger.info(f"key_function enter")
2191
+ # Print the selected indices if any, otherwise print the current index
2192
+ if self.is_selecting or self.is_deselecting: self.handle_visual_selection()
2193
+ if len(self.items) == 0:
2194
+ function_data = self.get_function_data()
2195
+ function_data["last_key"] = key
2196
+ return [], "", function_data
2197
+ selected_indices = get_selected_indices(self.selections)
2198
+ if not selected_indices and len(self.indexed_items):
2199
+ selected_indices = [self.indexed_items[self.cursor_pos][0]]
2200
+
2201
+ options_sufficient = True
2202
+ usrtxt = self.user_opts
2203
+ for index in selected_indices:
2204
+ if self.require_option[index]:
2205
+ if self.option_functions[index] != None:
2206
+ options_sufficient, usrtxt = self.option_functions[index](
2207
+ stdscr=self.stdscr,
2208
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights)
2209
+ )
2210
+ else:
2211
+ self.set_registers()
2212
+ options_sufficient, usrtxt = default_option_input(
2213
+ self.stdscr,
2214
+ starting_value=self.user_opts,
2215
+ registers = self.registers
2216
+ )
2217
+
2218
+ if options_sufficient:
2219
+ self.user_opts = usrtxt
2220
+ self.stdscr.clear()
2221
+ self.stdscr.refresh()
2222
+ function_data = self.get_function_data()
2223
+ function_data["last_key"] = key
2224
+ return selected_indices, usrtxt, function_data
2225
+ elif self.check_key("page_down", key, self.keys_dict): # Next page
2226
+ self.cursor_pos = min(len(self.indexed_items) - 1, self.cursor_pos+self.items_per_page)
2227
+
2228
+ elif self.check_key("page_up", key, self.keys_dict):
2229
+ self.cursor_pos = max(0, self.cursor_pos-self.items_per_page)
2230
+
2231
+ elif self.check_key("redraw_screen", key, self.keys_dict):
2232
+ self.logger.info(f"key_function redraw_screen")
2233
+ self.stdscr.clear()
2234
+ self.stdscr.refresh()
2235
+ restrict_curses(self.stdscr)
2236
+ unrestrict_curses(self.stdscr)
2237
+ self.stdscr.clear()
2238
+ self.stdscr.refresh()
2239
+
2240
+ self.draw_screen(self.indexed_items, self.highlights)
2241
+
2242
+ elif self.check_key("cycle_sort_method", key, self.keys_dict):
2243
+ if self.sort_column == self.selected_column:
2244
+ self.columns_sort_method[self.sort_column] = (self.columns_sort_method[self.sort_column]+1) % len(self.SORT_METHODS)
2245
+ else:
2246
+ self.sort_column = self.selected_column
2247
+ if len(self.indexed_items) > 0:
2248
+ current_index = self.indexed_items[self.cursor_pos][0]
2249
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2250
+ self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2251
+
2252
+ self.logger.info(f"key_function cycle_sort_method. (sort_column, sort_method) = ({self.sort_column}, {self.columns_sort_method[self.sort_column]})")
2253
+ elif self.check_key("cycle_sort_method_reverse", key, self.keys_dict): # Cycle sort method
2254
+ old_sort_column = self.sort_column
2255
+ self.sort_column = self.selected_column
2256
+ self.columns_sort_method[self.sort_column] = (self.columns_sort_method[self.sort_column]-1) % len(self.SORT_METHODS)
2257
+ if len(self.indexed_items) > 0:
2258
+ current_index = self.indexed_items[self.cursor_pos][0]
2259
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2260
+ self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2261
+ self.logger.info(f"key_function cycle_sort_method. (sort_column, sort_method) = ({self.sort_column}, {self.columns_sort_method[self.sort_column]})")
2262
+
2263
+ elif self.check_key("cycle_sort_order", key, self.keys_dict): # Toggle sort order
2264
+ self.sort_reverse[self.sort_column] = not self.sort_reverse[self.sort_column]
2265
+ if len(self.indexed_items) > 0:
2266
+ current_index = self.indexed_items[self.cursor_pos][0]
2267
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2268
+ self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2269
+ self.logger.info(f"key_function cycle_sort_order. (sort_column, sort_method, sort_reverse) = ({self.sort_column}, {self.columns_sort_method[self.sort_column]}, {self.sort_reverse[self.sort_column]})")
2270
+ elif self.check_key("col_select", key, self.keys_dict):
2271
+ col_index = key - ord('0')
2272
+ self.logger.info(f"key_function col_select {col_index}")
2273
+ if 0 <= col_index < len(self.items[0]):
2274
+ self.sort_column = col_index
2275
+ if len(self.indexed_items) > 0:
2276
+ current_index = self.indexed_items[self.cursor_pos][0]
2277
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2278
+ self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2279
+ elif self.check_key("col_select_next", key, self.keys_dict):
2280
+ if len(self.items) > 0 and len(self.items[0]) > 0:
2281
+ col_index = (self.selected_column +1) % (len(self.items[0]))
2282
+ self.selected_column = col_index
2283
+ # Flash when we loop back to the first column
2284
+ # if self.selected_column == 0:
2285
+ # curses.flash()
2286
+ self.logger.info(f"key_function col_select_next {self.selected_column}")
2287
+
2288
+
2289
+ ## Scroll with column select
2290
+ rows = self.get_visible_rows()
2291
+ self.column_widths = get_column_widths(rows, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2292
+ visible_column_widths = [c for i,c in enumerate(self.column_widths) if i not in self.hidden_columns]
2293
+ visible_columns_total_width = sum(visible_column_widths) + len(self.separator)*(len(visible_column_widths)-1)
2294
+ h, w = self.stdscr.getmaxyx()
2295
+ end_of_cell = sum(visible_column_widths[:self.selected_column+1])+len(self.separator)*(self.selected_column+1)
2296
+ display_width = w-self.startx
2297
+ # If the full column is within the current display then don't do anything
2298
+ if start_of_cell >= self.leftmost_char and end_of_cell <= self.leftmost_char + display_width:
2299
+ pass
2300
+ # Otherwise right-justify the cell
2301
+ else:
2302
+ self.leftmost_char = end_of_cell - display_width
2303
+
2304
+ self.leftmost_char = max(0, min(column_set_width - display_width + 5, self.leftmost_char))
2305
+
2306
+ elif self.check_key("col_select_prev", key, self.keys_dict):
2307
+ if len(self.items) > 0 and len(self.items[0]) > 0:
2308
+ col_index = (self.selected_column -1) % (len(self.items[0]))
2309
+ self.selected_column = col_index
2310
+
2311
+ self.logger.info(f"key_function col_select_prev {self.selected_column}")
2312
+ # Flash when we loop back to the last column
2313
+ # if self.selected_column == len(self.column_widths)-1:
2314
+ # curses.flash()
2315
+
2316
+ ## Scroll with column select
2317
+ rows = self.get_visible_rows()
2318
+ self.column_widths = get_column_widths(rows, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns, max_total_width=w, unicode_char_width=self.unicode_char_width)
2319
+ visible_column_widths = [c for i,c in enumerate(self.column_widths) if i not in self.hidden_columns]
2320
+ column_set_width = sum(visible_column_widths)+len(self.separator)*len(visible_column_widths)
2321
+ start_of_cell = sum(visible_column_widths[:self.selected_column])+len(self.separator)*self.selected_column
2322
+ end_of_cell = sum(visible_column_widths[:self.selected_column+1])+len(self.separator)*(self.selected_column+1)
2323
+ display_width = w-self.startx
2324
+
2325
+ # If the entire column is within the current display then don't do anything
2326
+ if start_of_cell >= self.leftmost_char and end_of_cell <= self.leftmost_char + display_width:
2327
+ pass
2328
+ # Otherwise left-justify the cell
2329
+ else:
2330
+ self.leftmost_char = start_of_cell
2331
+
2332
+ self.leftmost_char = max(0, min(column_set_width - display_width + 5, self.leftmost_char))
2333
+
2334
+ elif self.check_key("scroll_right", key, self.keys_dict):
2335
+ self.logger.info(f"key_function scroll_right")
2336
+ if len(self.indexed_items):
2337
+ row_width = sum(self.column_widths) + len(self.separator)*(len(self.column_widths)-1)
2338
+ if row_width-self.leftmost_char >= w-self.startx:
2339
+ self.leftmost_char = self.leftmost_char+5
2340
+
2341
+ elif self.check_key("scroll_left", key, self.keys_dict):
2342
+ self.logger.info(f"key_function scroll_left")
2343
+ self.leftmost_char = max(self.leftmost_char-5, 0)
2344
+
2345
+ elif self.check_key("scroll_far_left", key, self.keys_dict):
2346
+ self.logger.info(f"key_function scroll_far_left")
2347
+ self.leftmost_char = 0
2348
+ self.selected_column = 0
2349
+
2350
+ elif self.check_key("scroll_far_right", key, self.keys_dict):
2351
+ self.logger.info(f"key_function scroll_far_right")
2352
+ longest_row_str_len = 0
2353
+ rows = self.get_visible_rows()
2354
+ for i in range(len(rows)):
2355
+ item = rows[i]
2356
+ row_str = format_row(item, self.hidden_columns, self.column_widths, self.separator, self.centre_in_cols)
2357
+ if len(row_str) > longest_row_str_len: longest_row_str_len=len(row_str)
2358
+ # for i in range(len(self.indexed_items)):
2359
+ # item = self.indexed_items[i]
2360
+ # row_str = format_row(item[1], self.hidden_columns, self.column_widths, self.separator, self.centre_in_cols)
2361
+ # if len(row_str) > longest_row_str_len: longest_row_str_len=len(row_str)
2362
+ # self.notification(self.stdscr, f"{longest_row_str_len}")
2363
+ self.leftmost_char = max(0, longest_row_str_len-w+2+self.startx)
2364
+ if len(self.items):
2365
+ self.selected_column = len(self.items[0])-1
2366
+
2367
+ elif self.check_key("add_column_before", key, self.keys_dict):
2368
+ self.logger.info(f"key_function add_column_before")
2369
+ # self.add_column_before()
2370
+ self.insert_column(self.selected_column)
2371
+
2372
+ elif self.check_key("add_column_after", key, self.keys_dict):
2373
+ self.logger.info(f"key_function add_column_after")
2374
+ # self.add_column_after()
2375
+ self.insert_column(self.selected_column+1)
2376
+
2377
+ elif self.check_key("add_row_before", key, self.keys_dict):
2378
+ self.logger.info(f"key_function add_row_before")
2379
+ # self.add_row_before()
2380
+ self.insert_row(self.cursor_pos)
2381
+
2382
+ elif self.check_key("add_row_after", key, self.keys_dict):
2383
+ self.logger.info(f"key_function add_row_after")
2384
+ # self.add_row_after()
2385
+ self.insert_row(self.cursor_pos+1)
2386
+
2387
+ elif self.check_key("col_hide", key, self.keys_dict):
2388
+ self.logger.info(f"key_function col_hide")
2389
+ d = {'!': 0, '@': 1, '#': 2, '$': 3, '%': 4, '^': 5, '&': 6, '*': 7, '(': 8, ')': 9}
2390
+ d = {s:i for i,s in enumerate(")!@#$%^&*(")}
2391
+ col_index = d[chr(key)]
2392
+ self.toggle_column_visibility(col_index)
2393
+ elif self.check_key("copy", key, self.keys_dict):
2394
+ self.copy_dialogue()
2395
+ elif self.check_key("paste", key, self.keys_dict):
2396
+ self.paste_dialogue()
2397
+ elif self.check_key("save", key, self.keys_dict):
2398
+ self.save_dialog()
2399
+ elif self.check_key("load", key, self.keys_dict):
2400
+ self.load_dialog()
2401
+
2402
+ elif self.check_key("delete", key, self.keys_dict): # Delete key
2403
+ self.delete_entries()
2404
+
2405
+ elif self.check_key("delete_column", key, self.keys_dict): # Delete key
2406
+ self.logger.info(f"key_function delete_column")
2407
+ row_len = 1
2408
+ if self.header: row_len = len(self.header)
2409
+ elif len(self.items): row_len = len(self.items[0])
2410
+ if row_len > 1:
2411
+ self.items = [row[:self.selected_column] + row[self.selected_column+1:] for row in self.items]
2412
+ self.header = self.header[:self.selected_column] + self.header[self.selected_column+1:]
2413
+ self.editable_columns = self.editable_columns[:self.selected_column] + self.editable_columns[self.selected_column+1:]
2414
+ self.selected_column = min(self.selected_column, row_len-2)
2415
+ elif row_len == 1:
2416
+ self.items = [[""] for _ in range(len(self.items))]
2417
+ self.header = [""] if self.header else []
2418
+ self.editable_columns = []
2419
+ self.selected_column = min(self.selected_column, row_len-2)
2420
+ self.initialise_variables()
2421
+
2422
+
2423
+
2424
+
2425
+ # elif self.check_key("increase_lines_per_page", key, self.keys_dict):
2426
+ # self.items_per_page += 1
2427
+ # self.draw_screen(self.indexed_items, self.highlights)
2428
+ # elif self.check_key("decrease_lines_per_page", key, self.keys_dict):
2429
+ # if self.items_per_page > 1:
2430
+ # self.items_per_page -= 1
2431
+ # self.draw_screen(self.indexed_items, self.highlights)
2432
+ elif self.check_key("decrease_column_width", key, self.keys_dict):
2433
+ self.logger.info(f"key_function decrease_column_width")
2434
+ if self.max_column_width > 10:
2435
+ self.max_column_width -= 10
2436
+ self.column_widths[:] = get_column_widths(self.items, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2437
+ self.draw_screen(self.indexed_items, self.highlights)
2438
+ elif self.check_key("increase_column_width", key, self.keys_dict):
2439
+ self.logger.info(f"key_function increase_column_width")
2440
+ if self.max_column_width < 1000:
2441
+ self.max_column_width += 10
2442
+ self.column_widths = get_column_widths(self.items, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2443
+ self.draw_screen(self.indexed_items, self.highlights)
2444
+ elif self.check_key("visual_selection_toggle", key, self.keys_dict):
2445
+ self.logger.info(f"key_function visual_selection_toggle")
2446
+ self.handle_visual_selection()
2447
+ self.draw_screen(self.indexed_items, self.highlights)
2448
+
2449
+ elif self.check_key("visual_deselection_toggle", key, self.keys_dict):
2450
+ self.logger.info(f"key_function visual_deselection_toggle")
2451
+ self.handle_visual_selection(selecting=False)
2452
+ self.draw_screen(self.indexed_items, self.highlights)
2453
+
2454
+ elif key == curses.KEY_RESIZE: # Terminal resize signal
2455
+
2456
+ self.calculate_section_sizes()
2457
+ self.column_widths = get_column_widths(self.items, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2458
+
2459
+ self.draw_screen(self.indexed_items, self.highlights)
2460
+
2461
+
2462
+ elif self.check_key("filter_input", key, self.keys_dict):
2463
+ self.logger.info(f"key_function filter_input")
2464
+ self.draw_screen(self.indexed_items, self.highlights)
2465
+ usrtxt = f"{self.filter_query} " if self.filter_query else ""
2466
+ field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2467
+ if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2468
+ else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2469
+ self.set_registers()
2470
+ words = self.get_word_list()
2471
+ usrtxt, return_val = input_field(
2472
+ self.stdscr,
2473
+ usrtxt=usrtxt,
2474
+ field_prefix=" Filter: ",
2475
+ x=lambda:2,
2476
+ y=lambda: self.stdscr.getmaxyx()[0]-2,
2477
+ # max_length=field_end,
2478
+ max_length=field_end_f,
2479
+ registers=self.registers,
2480
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2481
+ history=self.history_filter_and_search,
2482
+ path_auto_complete=True,
2483
+ formula_auto_complete=False,
2484
+ function_auto_complete=False,
2485
+ word_auto_complete=True,
2486
+ auto_complete_words=words,
2487
+ )
2488
+ if return_val:
2489
+ self.filter_query = usrtxt
2490
+ self.history_filter_and_search.append(usrtxt)
2491
+
2492
+ # If the current mode filter has been changed then go back to the first mode
2493
+ if self.modes and "filter" in self.modes[self.mode_index] and self.modes[self.mode_index]["filter"] not in self.filter_query:
2494
+ self.mode_index = 0
2495
+ # elif "filter" in modes[mode_index] and modes[mode_index]["filter"] in filter_query:
2496
+ # filter_query.split(modes[mode_index]["filter"])
2497
+
2498
+ prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2499
+ self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2500
+ if prev_index in [x[0] for x in self.indexed_items]: new_index = [x[0] for x in self.indexed_items].index(prev_index)
2501
+ else: new_index = 0
2502
+ self.cursor_pos = new_index
2503
+ # Re-sort self.items after applying filter
2504
+ if self.columns_sort_method[self.selected_column] != 0:
2505
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2506
+
2507
+ elif self.check_key("search_input", key, self.keys_dict):
2508
+ self.logger.info(f"key_function search_input")
2509
+ self.draw_screen(self.indexed_items, self.highlights)
2510
+ usrtxt = f"{self.search_query} " if self.search_query else ""
2511
+ field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2512
+ if self.show_footer and self.footer.height >= 3: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2513
+ else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2514
+ self.set_registers()
2515
+ words = self.get_word_list()
2516
+ usrtxt, return_val = input_field(
2517
+ self.stdscr,
2518
+ usrtxt=usrtxt,
2519
+ field_prefix=" Search: ",
2520
+ x=lambda:2,
2521
+ y=lambda: self.stdscr.getmaxyx()[0]-3,
2522
+ max_length=field_end_f,
2523
+ registers=self.registers,
2524
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2525
+ history=self.history_filter_and_search,
2526
+ path_auto_complete=True,
2527
+ formula_auto_complete=False,
2528
+ function_auto_complete=False,
2529
+ word_auto_complete=True,
2530
+ auto_complete_words=words,
2531
+ )
2532
+ if return_val:
2533
+ self.search_query = usrtxt
2534
+ self.history_filter_and_search.append(usrtxt)
2535
+ return_val, tmp_cursor, tmp_index, tmp_count, tmp_highlights = search(
2536
+ query=self.search_query,
2537
+ indexed_items=self.indexed_items,
2538
+ highlights=self.highlights,
2539
+ cursor_pos=self.cursor_pos,
2540
+ unselectable_indices=self.unselectable_indices,
2541
+ )
2542
+ if return_val:
2543
+ self.cursor_pos, self.search_index, self.search_count, self.highlights = tmp_cursor, tmp_index, tmp_count, tmp_highlights
2544
+ else:
2545
+ self.search_index, self.search_count = 0, 0
2546
+
2547
+ elif self.check_key("continue_search_forward", key, self.keys_dict):
2548
+ self.logger.info(f"key_function continue_search_forward")
2549
+ return_val, tmp_cursor, tmp_index, tmp_count, tmp_highlights = search(
2550
+ query=self.search_query,
2551
+ indexed_items=self.indexed_items,
2552
+ highlights=self.highlights,
2553
+ cursor_pos=self.cursor_pos,
2554
+ unselectable_indices=self.unselectable_indices,
2555
+ continue_search=True,
2556
+ )
2557
+ if return_val:
2558
+ self.cursor_pos, self.search_index, self.search_count, self.highlights = tmp_cursor, tmp_index, tmp_count, tmp_highlights
2559
+ elif self.check_key("continue_search_backward", key, self.keys_dict):
2560
+ self.logger.info(f"key_function continue_search_backward")
2561
+ return_val, tmp_cursor, tmp_index, tmp_count, tmp_highlights = search(
2562
+ query=self.search_query,
2563
+ indexed_items=self.indexed_items,
2564
+ highlights=self.highlights,
2565
+ cursor_pos=self.cursor_pos,
2566
+ unselectable_indices=self.unselectable_indices,
2567
+ continue_search=True,
2568
+ reverse=True,
2569
+ )
2570
+ if return_val:
2571
+ self.cursor_pos, self.search_index, self.search_count, self.highlights = tmp_cursor, tmp_index, tmp_count, tmp_highlights
2572
+ elif self.check_key("cancel", key, self.keys_dict): # ESC key
2573
+ # order of escapes:
2574
+ # 1. selecting/deslecting
2575
+ # 2. search
2576
+ # 3. filter
2577
+ # 4. if self.cancel_is_back (e.g., notification) then we exit
2578
+ # 4. selecting
2579
+
2580
+ # Cancel visual de/selection
2581
+ if self.is_selecting or self.is_deselecting:
2582
+ self.start_selection = -1
2583
+ self.end_selection = -1
2584
+ self.is_selecting = False
2585
+ self.is_deselecting = False
2586
+ # Cancel search
2587
+ elif self.search_query:
2588
+ self.search_query = ""
2589
+ self.highlights = [highlight for highlight in self.highlights if "type" not in highlight or highlight["type"] != "search" ]
2590
+ # Remove filter
2591
+ elif self.filter_query:
2592
+ if self.modes and "filter" in self.modes[self.mode_index] and self.modes[self.mode_index]["filter"] in self.filter_query and self.filter_query.strip() != self.modes[self.mode_index]["filter"]:
2593
+ self.filter_query = self.modes[self.mode_index]["filter"]
2594
+ # elif "filter" in modes[mode_index]:
2595
+ else:
2596
+ self.filter_query = ""
2597
+ self.mode_index = 0
2598
+ prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2599
+ self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2600
+ if prev_index in [x[0] for x in self.indexed_items]: new_index = [x[0] for x in self.indexed_items].index(prev_index)
2601
+ else: new_index = 0
2602
+ self.cursor_pos = new_index
2603
+ # Re-sort self.items after applying filter
2604
+ if self.columns_sort_method[self.selected_column] != 0:
2605
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2606
+ elif self.cancel_is_back:
2607
+ function_data = self.get_function_data()
2608
+ function_data["last_key"] = key
2609
+ return [], "escape", function_data
2610
+
2611
+
2612
+ # else:
2613
+ # self.search_query = ""
2614
+ # self.mode_index = 0
2615
+ # self.highlights = [highlight for highlight in self.highlights if "type" not in highlight or highlight["type"] != "search" ]
2616
+ # continue
2617
+ self.draw_screen(self.indexed_items, self.highlights)
2618
+
2619
+ elif self.check_key("opts_input", key, self.keys_dict):
2620
+ self.logger.info(f"key_function opts_input")
2621
+ usrtxt = f"{self.user_opts} " if self.user_opts else ""
2622
+ field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2623
+ if self.show_footer and self.footer.height >= 1: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2624
+ else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2625
+ self.set_registers()
2626
+ words = self.get_word_list()
2627
+ usrtxt, return_val = input_field(
2628
+ self.stdscr,
2629
+ usrtxt=usrtxt,
2630
+ field_prefix=" Opts: ",
2631
+ x=lambda:2,
2632
+ y=lambda: self.stdscr.getmaxyx()[0]-1,
2633
+ max_length=field_end_f,
2634
+ registers=self.registers,
2635
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2636
+ history=self.history_opts,
2637
+ path_auto_complete=True,
2638
+ formula_auto_complete=False,
2639
+ function_auto_complete=True,
2640
+ word_auto_complete=True,
2641
+ auto_complete_words=words,
2642
+ )
2643
+ if return_val:
2644
+ self.user_opts = usrtxt
2645
+ self.history_opts.append(usrtxt)
2646
+ elif self.check_key("opts_select", key, self.keys_dict):
2647
+ self.logger.info(f"key_function opts_select")
2648
+ s, o, f = self.choose_option(self.stdscr, self.options_list)
2649
+ if self.user_opts.strip(): self.user_opts += " "
2650
+ self.user_opts += " ".join([x for x in s.values()])
2651
+ elif self.check_key("notification_toggle", key, self.keys_dict):
2652
+ self.logger.info(f"key_function notification_toggle")
2653
+ self.notification(self.stdscr, colours_end=self.colours_end)
2654
+
2655
+ elif self.check_key("mode_next", key, self.keys_dict): # tab key
2656
+ self.logger.info(f"key_function mode_next")
2657
+ # apply setting
2658
+ prev_mode_index = self.mode_index
2659
+ self.mode_index = (self.mode_index+1)%len(self.modes)
2660
+ mode = self.modes[self.mode_index]
2661
+ for key, val in mode.items():
2662
+ if key == 'filter':
2663
+ if 'filter' in self.modes[prev_mode_index]:
2664
+ self.filter_query = self.filter_query.replace(self.modes[prev_mode_index]['filter'], '')
2665
+ self.filter_query = f"{self.filter_query.strip()} {val.strip()}".strip()
2666
+ prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2667
+
2668
+ self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2669
+ if prev_index in [x[0] for x in self.indexed_items]: new_index = [x[0] for x in self.indexed_items].index(prev_index)
2670
+ else: new_index = 0
2671
+ self.cursor_pos = new_index
2672
+ # Re-sort self.items after applying filter
2673
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2674
+ elif self.check_key("mode_prev", key, self.keys_dict): # shift+tab key
2675
+ self.logger.info(f"key_function mode_prev")
2676
+ # apply setting
2677
+ prev_mode_index = self.mode_index
2678
+ self.mode_index = (self.mode_index-1)%len(self.modes)
2679
+ mode = self.modes[self.mode_index]
2680
+ for key, val in mode.items():
2681
+ if key == 'filter':
2682
+ if 'filter' in self.modes[prev_mode_index]:
2683
+ self.filter_query = self.filter_query.replace(self.modes[prev_mode_index]['filter'], '')
2684
+ self.filter_query = f"{self.filter_query.strip()} {val.strip()}".strip()
2685
+ prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2686
+ self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2687
+ if prev_index in [x[0] for x in self.indexed_items]: new_index = [x[0] for x in self.indexed_items].index(prev_index)
2688
+ else: new_index = 0
2689
+ self.cursor_pos = new_index
2690
+ # Re-sort self.items after applying filter
2691
+ sort_items(self.indexed_items, sort_method=self.columns_sort_method[self.sort_column], sort_column=self.sort_column, sort_reverse=self.sort_reverse[self.sort_column]) # Re-sort self.items based on new column
2692
+ elif self.check_key("pipe_input", key, self.keys_dict):
2693
+ self.logger.info(f"key_function pipe_input")
2694
+ # usrtxt = "xargs -d '\n' -I{} "
2695
+ usrtxt = "xargs "
2696
+ field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2697
+ if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2698
+ else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2699
+ self.set_registers()
2700
+
2701
+ # Get list of available shell commands
2702
+ try:
2703
+ # result = subprocess.run(['compgen', '-c'], capture_output=True, text=True, check=True)
2704
+ # shell_commands = result.stdout.splitlines()
2705
+ result = subprocess.run(['ls', '/usr/bin'], capture_output=True, text=True, check=True)
2706
+ shell_commands = result.stdout.splitlines()
2707
+ except:
2708
+ shell_commands = []
2709
+ usrtxt, return_val = input_field(
2710
+ self.stdscr,
2711
+ usrtxt=usrtxt,
2712
+ field_prefix=" Command: ",
2713
+ x=lambda:2,
2714
+ y=lambda: self.stdscr.getmaxyx()[0]-2,
2715
+ literal=True,
2716
+ max_length=field_end_f,
2717
+ registers=self.registers,
2718
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2719
+ history=self.history_pipes,
2720
+ path_auto_complete=True,
2721
+ formula_auto_complete=False,
2722
+ function_auto_complete=False,
2723
+ word_auto_complete=True,
2724
+ auto_complete_words=shell_commands,
2725
+ )
2726
+
2727
+ if return_val:
2728
+ selected_indices = get_selected_indices(self.selections)
2729
+ self.history_pipes.append(usrtxt)
2730
+ if not selected_indices:
2731
+ selected_indices = [self.indexed_items[self.cursor_pos][0]]
2732
+
2733
+ full_values = [format_row_full(self.items[i], self.hidden_columns) for i in selected_indices] # Use format_row_full for full data
2734
+ full_values = [self.items[i][self.selected_column] for i in selected_indices]
2735
+ if full_values:
2736
+ command = usrtxt.split()
2737
+ # command = ['xargs', '-d' , '"\n"' '-I', '{}', 'mpv', '{}']
2738
+ # command = ['xargs', '-d' , '"\n"' '-I', '{}', 'mpv', '{}']
2739
+ # command = "xargs -d '\n' -I{} mpv {}"
2740
+
2741
+ try:
2742
+ process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
2743
+
2744
+ if process.stdin != None:
2745
+ for value in full_values:
2746
+ process.stdin.write((repr(value) + '\n').encode())
2747
+
2748
+ process.stdin.close()
2749
+
2750
+ self.notification(self.stdscr, message=f"{len(full_values)} strings piped to {repr(usrtxt)}")
2751
+ except Exception as e:
2752
+ self.notification(self.stdscr, message=f"{e}")
2753
+
2754
+
2755
+ elif self.check_key("open", key, self.keys_dict):
2756
+ self.logger.info(f"key_function open")
2757
+ selected_indices = get_selected_indices(self.selections)
2758
+ if not selected_indices:
2759
+ selected_indices = [self.indexed_items[self.cursor_pos][0]]
2760
+
2761
+ file_names = [self.items[i][self.selected_column] for i in selected_indices]
2762
+ response = openFiles(file_names)
2763
+ if response:
2764
+ self.notification(self.stdscr, message=response)
2765
+
2766
+
2767
+ elif self.check_key("reset_opts", key, self.keys_dict):
2768
+ self.logger.info(f"key_function reset_opts")
2769
+ self.user_opts = ""
2770
+
2771
+ elif self.check_key("edit", key, self.keys_dict):
2772
+ self.logger.info(f"key_function edit")
2773
+ if len(self.indexed_items) > 0 and self.selected_column >=0 and self.editable_columns[self.selected_column]:
2774
+ current_val = self.indexed_items[self.cursor_pos][1][self.selected_column]
2775
+ usrtxt = f"{current_val}"
2776
+ field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2777
+ if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2778
+ else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2779
+ self.set_registers()
2780
+ words = self.get_word_list()
2781
+ usrtxt, return_val = input_field(
2782
+ self.stdscr,
2783
+ usrtxt=usrtxt,
2784
+ field_prefix=" Edit value: ",
2785
+ x=lambda:2,
2786
+ y=lambda: self.stdscr.getmaxyx()[0]-2,
2787
+ max_length=field_end_f,
2788
+ registers=self.registers,
2789
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2790
+ history = self.history_edits,
2791
+ path_auto_complete=True,
2792
+ formula_auto_complete=True,
2793
+ function_auto_complete=True,
2794
+ word_auto_complete=True,
2795
+ auto_complete_words=words,
2796
+ )
2797
+ if return_val:
2798
+ if usrtxt.startswith("```"):
2799
+ usrtxt = str(eval(usrtxt[3:]))
2800
+ self.indexed_items[self.cursor_pos][1][self.selected_column] = usrtxt
2801
+ self.history_edits.append(usrtxt)
2802
+
2803
+ elif self.check_key("edit_picker", key, self.keys_dict):
2804
+ self.logger.info(f"key_function edit_picker")
2805
+ if len(self.indexed_items) > 0 and self.selected_column >=0 and self.editable_columns[self.selected_column]:
2806
+ current_val = self.indexed_items[self.cursor_pos][1][self.selected_column]
2807
+ usrtxt = f"{current_val}"
2808
+ field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2809
+ if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2810
+ else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2811
+ self.set_registers()
2812
+ words = self.get_word_list()
2813
+ usrtxt, return_val = input_field(
2814
+ self.stdscr,
2815
+ usrtxt=usrtxt,
2816
+ field_prefix=" Edit value: ",
2817
+ x=lambda:2,
2818
+ y=lambda: self.stdscr.getmaxyx()[0]-2,
2819
+ max_length=field_end_f,
2820
+ registers=self.registers,
2821
+ refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2822
+ history = self.history_edits,
2823
+ path_auto_complete=True,
2824
+ formula_auto_complete=True,
2825
+ function_auto_complete=True,
2826
+ word_auto_complete=True,
2827
+ auto_complete_words=words,
2828
+ )
2829
+ if return_val:
2830
+ self.indexed_items[self.cursor_pos][1][self.selected_column] = usrtxt
2831
+ self.history_edits.append(usrtxt)
2832
+ elif self.check_key("edit_ipython", key, self.keys_dict):
2833
+ self.logger.info(f"key_function edit_picker")
2834
+ import IPython
2835
+ self.stdscr.clear()
2836
+ restrict_curses(self.stdscr)
2837
+ self.stdscr.clear()
2838
+ os.system('cls' if os.name == 'nt' else 'clear')
2839
+ globals()['self'] = self # make the instance available in IPython namespace
2840
+
2841
+ from traitlets.config import Config
2842
+ c = Config()
2843
+ # Doesn't work; Config only works with start_ipython, not embed... but start_ipython causes errors
2844
+ # c.InteractiveShellApp.exec_lines = [
2845
+ # '%clear'
2846
+ # ]
2847
+ msg = "The active Picker object has variable name self.\n"
2848
+ msg += "\te.g., self.items will display the items in Picker"
2849
+ IPython.embed(header=msg, config=c)
2850
+
2851
+ unrestrict_curses(self.stdscr)
2852
+
2853
+ self.stdscr.clear()
2854
+ self.stdscr.refresh()
2855
+ self.initialise_variables()
2856
+ self.draw_screen(self.indexed_items, self.highlights)
2857
+
2858
+
2859
+
2860
+ self.draw_screen(self.indexed_items, self.highlights, clear=clear_screen)
2861
+
2862
+
2863
+
2864
+ def set_colours(pick: int = 0, start: int = 0) -> Optional[int]:
2865
+ """ Initialise curses colour pairs from dictionary. """
2866
+
2867
+
2868
+ global COLOURS_SET, notification_colours, help_colours
2869
+ if COLOURS_SET: return None
2870
+ if start == None: start = 0
2871
+
2872
+
2873
+ if curses.COLORS >= 255:
2874
+ colours = get_colours(pick)
2875
+ notification_colours = get_notification_colours(pick)
2876
+ help_colours = get_help_colours(pick)
2877
+ standard_colours_start, notification_colours_start, help_colours_start = 0, 50, 100
2878
+ else:
2879
+ colours = get_fallback_colours()
2880
+ notification_colours = get_fallback_colours()
2881
+ help_colours = get_fallback_colours()
2882
+ standard_colours_start, help_colours_start, notification_colours_start = 0, 0, 0
2883
+
2884
+ if not colours: return 0
2885
+
2886
+ try:
2887
+ start = standard_colours_start
2888
+ curses.init_pair(start+1, colours['selected_fg'], colours['selected_bg'])
2889
+ curses.init_pair(start+2, colours['unselected_fg'], colours['unselected_bg'])
2890
+ curses.init_pair(start+3, colours['normal_fg'], colours['background'])
2891
+ curses.init_pair(start+4, colours['header_fg'], colours['header_bg'])
2892
+ curses.init_pair(start+5, colours['cursor_fg'], colours['cursor_bg'])
2893
+ curses.init_pair(start+6, colours['normal_fg'], colours['background'])
2894
+ curses.init_pair(start+7, colours['error_fg'], colours['error_bg'])
2895
+ curses.init_pair(start+8, colours['complete_fg'], colours['complete_bg'])
2896
+ curses.init_pair(start+9, colours['active_fg'], colours['active_bg'])
2897
+ curses.init_pair(start+10, colours['search_fg'], colours['search_bg'])
2898
+ curses.init_pair(start+11, colours['waiting_fg'], colours['waiting_bg'])
2899
+ curses.init_pair(start+12, colours['paused_fg'], colours['paused_bg'])
2900
+ curses.init_pair(start+13, colours['active_input_fg'], colours['active_input_bg'])
2901
+ curses.init_pair(start+14, colours['modes_selected_fg'], colours['modes_selected_bg'])
2902
+ curses.init_pair(start+15, colours['modes_unselected_fg'], colours['modes_unselected_bg'])
2903
+ curses.init_pair(start+16, colours['title_fg'], colours['title_bg'])
2904
+ curses.init_pair(start+17, colours['normal_fg'], colours['title_bar'])
2905
+ curses.init_pair(start+18, colours['normal_fg'], colours['scroll_bar_bg'])
2906
+ curses.init_pair(start+19, colours['selected_header_column_fg'], colours['selected_header_column_bg'])
2907
+ curses.init_pair(start+20, colours['footer_fg'], colours['footer_bg'])
2908
+ curses.init_pair(start+21, colours['refreshing_fg'], colours['refreshing_bg'])
2909
+ curses.init_pair(start+22, colours['40pc_fg'], colours['40pc_bg'])
2910
+ curses.init_pair(start+23, colours['refreshing_inactive_fg'], colours['refreshing_inactive_bg'])
2911
+ curses.init_pair(start+24, colours['footer_string_fg'], colours['footer_string_bg'])
2912
+ curses.init_pair(start+25, colours['selected_cell_fg'], colours['selected_cell_bg'])
2913
+ curses.init_pair(start+26, colours['deselecting_cell_fg'], colours['deselecting_cell_bg'])
2914
+
2915
+
2916
+ # notifications 50, infobox 100, help 150
2917
+ # Notification colours
2918
+ colours = notification_colours
2919
+ start = notification_colours_start
2920
+ curses.init_pair(start+1, colours['selected_fg'], colours['selected_bg'])
2921
+ curses.init_pair(start+2, colours['unselected_fg'], colours['unselected_bg'])
2922
+ curses.init_pair(start+3, colours['normal_fg'], colours['background'])
2923
+ curses.init_pair(start+4, colours['header_fg'], colours['header_bg'])
2924
+ curses.init_pair(start+5, colours['cursor_fg'], colours['cursor_bg'])
2925
+ curses.init_pair(start+6, colours['normal_fg'], colours['background'])
2926
+ curses.init_pair(start+7, colours['error_fg'], colours['error_bg'])
2927
+ curses.init_pair(start+8, colours['complete_fg'], colours['complete_bg'])
2928
+ curses.init_pair(start+9, colours['active_fg'], colours['active_bg'])
2929
+ curses.init_pair(start+10, colours['search_fg'], colours['search_bg'])
2930
+ curses.init_pair(start+11, colours['waiting_fg'], colours['waiting_bg'])
2931
+ curses.init_pair(start+12, colours['paused_fg'], colours['paused_bg'])
2932
+ curses.init_pair(start+13, colours['active_input_fg'], colours['active_input_bg'])
2933
+ curses.init_pair(start+14, colours['modes_selected_fg'], colours['modes_selected_bg'])
2934
+ curses.init_pair(start+15, colours['modes_unselected_fg'], colours['modes_unselected_bg'])
2935
+ curses.init_pair(start+16, colours['title_fg'], colours['title_bg'])
2936
+ curses.init_pair(start+17, colours['normal_fg'], colours['title_bar'])
2937
+ curses.init_pair(start+18, colours['normal_fg'], colours['scroll_bar_bg'])
2938
+ curses.init_pair(start+19, colours['selected_header_column_fg'], colours['selected_header_column_bg'])
2939
+ curses.init_pair(start+20, colours['footer_fg'], colours['footer_bg'])
2940
+ curses.init_pair(start+21, colours['refreshing_fg'], colours['refreshing_bg'])
2941
+ curses.init_pair(start+22, colours['40pc_fg'], colours['40pc_bg'])
2942
+ curses.init_pair(start+23, colours['refreshing_inactive_fg'], colours['refreshing_inactive_bg'])
2943
+ curses.init_pair(start+24, colours['footer_string_fg'], colours['footer_string_bg'])
2944
+ curses.init_pair(start+25, colours['selected_cell_fg'], colours['selected_cell_bg'])
2945
+ curses.init_pair(start+26, colours['deselecting_cell_fg'], colours['deselecting_cell_bg'])
2946
+
2947
+ # Help
2948
+ colours = help_colours
2949
+ start = help_colours_start
2950
+ curses.init_pair(start+1, colours['selected_fg'], colours['selected_bg'])
2951
+ curses.init_pair(start+2, colours['unselected_fg'], colours['unselected_bg'])
2952
+ curses.init_pair(start+3, colours['normal_fg'], colours['background'])
2953
+ curses.init_pair(start+4, colours['header_fg'], colours['header_bg'])
2954
+ curses.init_pair(start+5, colours['cursor_fg'], colours['cursor_bg'])
2955
+ curses.init_pair(start+6, colours['normal_fg'], colours['background'])
2956
+ curses.init_pair(start+7, colours['error_fg'], colours['error_bg'])
2957
+ curses.init_pair(start+8, colours['complete_fg'], colours['complete_bg'])
2958
+ curses.init_pair(start+9, colours['active_fg'], colours['active_bg'])
2959
+ curses.init_pair(start+10, colours['search_fg'], colours['search_bg'])
2960
+ curses.init_pair(start+11, colours['waiting_fg'], colours['waiting_bg'])
2961
+ curses.init_pair(start+12, colours['paused_fg'], colours['paused_bg'])
2962
+ curses.init_pair(start+13, colours['active_input_fg'], colours['active_input_bg'])
2963
+ curses.init_pair(start+14, colours['modes_selected_fg'], colours['modes_selected_bg'])
2964
+ curses.init_pair(start+15, colours['modes_unselected_fg'], colours['modes_unselected_bg'])
2965
+ curses.init_pair(start+16, colours['title_fg'], colours['title_bg'])
2966
+ curses.init_pair(start+17, colours['normal_fg'], colours['title_bar'])
2967
+ curses.init_pair(start+18, colours['normal_fg'], colours['scroll_bar_bg'])
2968
+ curses.init_pair(start+19, colours['selected_header_column_fg'], colours['selected_header_column_bg'])
2969
+ curses.init_pair(start+20, colours['footer_fg'], colours['footer_bg'])
2970
+ curses.init_pair(start+21, colours['refreshing_fg'], colours['refreshing_bg'])
2971
+ curses.init_pair(start+22, colours['40pc_fg'], colours['40pc_bg'])
2972
+ curses.init_pair(start+23, colours['refreshing_inactive_fg'], colours['refreshing_inactive_bg'])
2973
+ curses.init_pair(start+24, colours['footer_string_fg'], colours['footer_string_bg'])
2974
+ curses.init_pair(start+25, colours['selected_cell_fg'], colours['selected_cell_bg'])
2975
+ curses.init_pair(start+26, colours['deselecting_cell_fg'], colours['deselecting_cell_bg'])
2976
+ except:
2977
+ pass
2978
+ COLOURS_SET = True
2979
+ return start+21
2980
+
2981
+ def parse_arguments() -> Tuple[argparse.Namespace, dict]:
2982
+ """ Parse command line arguments. """
2983
+ parser = argparse.ArgumentParser(description='Convert table to list of lists.')
2984
+ parser.add_argument('filename', type=str, help='The file to process')
2985
+ parser.add_argument('-i', dest='file', help='File containing the table to be converted.')
2986
+ parser.add_argument('--load', '-l', dest='load', type=str, help='Load file from Picker dump.')
2987
+ parser.add_argument('--stdin', dest='stdin', action='store_true', help='Table passed on stdin')
2988
+ parser.add_argument('--stdin2', action='store_true', help='Table passed on stdin')
2989
+ parser.add_argument('--generate', '-g', type=str, help='Pass file to generate data for listpick Picker.')
2990
+ parser.add_argument('-d', dest='delimiter', default='\t', help='Delimiter for rows in the table (default: tab)')
2991
+ parser.add_argument('-t', dest='file_type', choices=['tsv', 'csv', 'json', 'xlsx', 'ods', 'pkl'], help='Type of file (tsv, csv, json, xlsx, ods)')
2992
+ parser.add_argument('--debug', action="store_true", help="Enable debug log.")
2993
+ parser.add_argument('--debug-verbose', action="store_true", help="Enable debug verbose log.")
2994
+ args = parser.parse_args()
2995
+
2996
+
2997
+ function_data = {
2998
+ "items" : [],
2999
+ "header": [],
3000
+ "unselectable_indices" : [],
3001
+ "colours": get_colours(0),
3002
+ "top_gap": 0,
3003
+ "max_column_width": 70,
3004
+ }
3005
+
3006
+ if args.file:
3007
+ input_arg = args.file
3008
+ elif args.stdin:
3009
+ input_arg = '--stdin'
3010
+ elif args.stdin2:
3011
+ input_arg = '--stdin2'
3012
+ elif args.filename:
3013
+ input_arg = args.filename
3014
+
3015
+ elif args.generate:
3016
+ function_data["refresh_function"] = lambda : generate_picker_data(args.generate)
3017
+ function_data["get_data_startup"] = True
3018
+ function_data["get_new_data"] = True
3019
+ return args, function_data
3020
+ elif args.load:
3021
+ function_data = load_state(args.load)
3022
+ function_data["refresh_function"] = lambda : (load_state(args.load)["items"], load_state(args.load)["header"])
3023
+ function_data["get_new_data"] = True
3024
+ return args, function_data
3025
+
3026
+ else:
3027
+ # print("Error: Please provide input file or use --stdin flag.")
3028
+ print("No data provided. Loading empty Picker.")
3029
+ return args, function_data
3030
+ # sys.exit(1)
3031
+ if args.debug:
3032
+ function_data["debug"] = True
3033
+ function_data["debug_level"] = 1
3034
+
3035
+ if args.debug_verbose:
3036
+ function_data["debug"] = True
3037
+ function_data["debug_level"] = 0
3038
+
3039
+ if not args.file_type:
3040
+ filetype = guess_file_type(input_arg)
3041
+ else:
3042
+ filetype = args.file_type
3043
+
3044
+
3045
+ items, header = table_to_list(input_arg, args.delimiter, filetype)
3046
+ function_data["items"] = items
3047
+ if header: function_data["header"] = header
3048
+ return args, function_data
3049
+
3050
+ def start_curses() -> curses.window:
3051
+ """ Initialise curses and return curses window. """
3052
+ stdscr = curses.initscr()
3053
+ curses.start_color()
3054
+ curses.use_default_colors() # For terminal theme-recolouring
3055
+ curses.noecho() # Turn off automatic echoing of keys to the screen
3056
+ curses.cbreak() # Interpret keystrokes immediately (without requiring Enter)
3057
+ stdscr.keypad(True) # Ensures that arrow and function keys are received as one key by getch
3058
+ curses.raw() # Disable control keys (ctrl-c, ctrl-s, ctrl-q, etc.)
3059
+ curses.curs_set(False)
3060
+
3061
+ return stdscr
3062
+
3063
+ def close_curses(stdscr: curses.window) -> None:
3064
+ """ Close curses. """
3065
+ stdscr.keypad(False)
3066
+ curses.nocbreak()
3067
+ curses.noraw()
3068
+ curses.echo()
3069
+ curses.endwin()
3070
+
3071
+ def restrict_curses(stdscr: curses.window) -> None:
3072
+ """ Restrict curses for normal input. Used when dropping to ipython. """
3073
+ stdscr.keypad(False)
3074
+ curses.nocbreak()
3075
+ curses.noraw()
3076
+ curses.curs_set(True)
3077
+ curses.echo()
3078
+
3079
+ def unrestrict_curses(stdscr: curses.window) -> None:
3080
+ """ Unrestrict curses for terminal input. Used after dropping to ipython. """
3081
+ curses.noecho() # Turn off automatic echoing of keys to the screen
3082
+ curses.cbreak() # Interpret keystrokes immediately (without requiring Enter)
3083
+ stdscr.keypad(True)
3084
+ curses.raw() # Disable control keys (ctrl-c, ctrl-s, ctrl-q, etc.)
3085
+ curses.curs_set(False)
3086
+
3087
+ def main() -> None:
3088
+ """ Main function when listpick is executed. Deals with command line arguments and starts a Picker. """
3089
+ args, function_data = parse_arguments()
3090
+
3091
+ try:
3092
+ if function_data["items"] == []:
3093
+ function_data["items"] = test_items
3094
+ function_data["highlights"] = test_highlights
3095
+ function_data["header"] = test_header
3096
+ except:
3097
+ pass
3098
+
3099
+ function_data["colour_theme_number"] = 3
3100
+ # function_data["modes"] = [
3101
+ # {
3102
+ # 'filter': '',
3103
+ # 'sort': 0,
3104
+ # 'name': 'All',
3105
+ # },
3106
+ # {
3107
+ # 'filter': '--2 miss',
3108
+ # 'name': 'miss',
3109
+ # },
3110
+ # {
3111
+ # 'filter': '--2 mp4',
3112
+ # 'name': 'mp4',
3113
+ # },
3114
+ # ]
3115
+ function_data["cell_cursor"] = True
3116
+ function_data["display_modes"] = True
3117
+ function_data["centre_in_cols"] = True
3118
+ function_data["show_row_header"] = True
3119
+ function_data["keys_dict"] = picker_keys
3120
+ function_data["id_column"] = -1
3121
+ function_data["track_entries_upon_refresh"] = True
3122
+ function_data["centre_in_terminal_vertical"] = True
3123
+ function_data["highlight_full_row"] = True
3124
+ # function_data["debug"] = True
3125
+ # function_data["debug_level"] = 1
3126
+ stdscr = start_curses()
3127
+ try:
3128
+ # Run the Picker
3129
+ # h, w = stdscr.getmaxyx()
3130
+ # if (h>8 and w >20):
3131
+ # curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
3132
+ # stdscr.bkgd(' ', curses.color_pair(1)) # Apply background color
3133
+ # s = "Listpick is loading your data..."
3134
+ # stdscr.addstr(h//2, (w-len(s))//2, s)
3135
+ # stdscr.refresh()
3136
+
3137
+ # app = Picker(stdscr, **function_data)
3138
+ app = Picker(stdscr)
3139
+ app.set_function_data(function_data)
3140
+ app.splash_screen("Listpick is loading your data...")
3141
+ app.load_input_history("~/.config/listpick/cmdhist.json")
3142
+ app.run()
3143
+
3144
+ app.save_input_history("~/.config/listpick/cmdhist.json")
3145
+ except Exception as e:
3146
+ print(e)
3147
+
3148
+ # Clean up
3149
+ close_curses(stdscr)
3150
+
3151
+ if __name__ == '__main__':
3152
+ main()