listpick 0.1.13.55__py3-none-any.whl → 0.1.13.57__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 DELETED
@@ -1,3153 +0,0 @@
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
- self.stdscr.refresh()
986
-
987
- ## Display infobox
988
- if self.display_infobox:
989
- self.infobox(self.stdscr, message=self.infobox_items, title=self.infobox_title)
990
- # 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
991
-
992
-
993
-
994
- def infobox(self, stdscr: curses.window, message: str ="", title: str ="Infobox", colours_end: int = 0, duration: int = 4) -> curses.window:
995
- """ Display non-interactive infobox window. """
996
-
997
- self.logger.info(f"function: infobox()")
998
- h, w = stdscr.getmaxyx()
999
- notification_width, notification_height = w//2, 3*h//5
1000
- message_width = notification_width-5
1001
-
1002
- if not message: message = "!!"
1003
- if isinstance(message, str):
1004
- submenu_items = [" "+message[i*message_width:(i+1)*message_width] for i in range(len(message)//message_width+1)]
1005
- else:
1006
- submenu_items = message
1007
-
1008
- notification_remap_keys = {
1009
- curses.KEY_RESIZE: curses.KEY_F5,
1010
- 27: ord('q')
1011
- }
1012
- if len(submenu_items) > notification_height - 2:
1013
- submenu_items = submenu_items[:notification_height-3] + [f"{'....':^{notification_width}}"]
1014
- while True:
1015
- h, w = stdscr.getmaxyx()
1016
-
1017
- submenu_win = curses.newwin(notification_height, notification_width, 3, w - (notification_width+4))
1018
- infobox_data = {
1019
- "items": submenu_items,
1020
- "colours": notification_colours,
1021
- "colours_start": self.notification_colours_start,
1022
- "disabled_keys": [ord('z'), ord('c')],
1023
- "show_footer": False,
1024
- "top_gap": 0,
1025
- "key_remappings": notification_remap_keys,
1026
- "display_only": True,
1027
- "hidden_columns": [],
1028
- "title": title,
1029
- "reset_colours": False,
1030
- }
1031
-
1032
- OptionPicker = Picker(submenu_win, **infobox_data)
1033
- s, o, f = OptionPicker.run()
1034
- if o != "refresh": break
1035
-
1036
- return submenu_win
1037
-
1038
- def get_function_data(self) -> dict:
1039
- self.logger.debug(f"function: get_function_data()")
1040
- """ Returns a dict of the main variables needed to restore the state of list_pikcer. """
1041
- function_data = {
1042
- "selections": self.selections,
1043
- "cell_selections": self.cell_selections,
1044
- "items_per_page": self.items_per_page,
1045
- "current_row": self.current_row,
1046
- "current_page": self.current_page,
1047
- "cursor_pos": self.cursor_pos,
1048
- "colours": self.colours,
1049
- "colour_theme_number": self.colour_theme_number,
1050
- "selected_column": self.selected_column,
1051
- "sort_column": self.sort_column,
1052
- "sort_method": self.sort_method,
1053
- "sort_reverse": self.sort_reverse,
1054
- "SORT_METHODS": self.SORT_METHODS,
1055
- "hidden_columns": self.hidden_columns,
1056
- "is_selecting": self.is_selecting,
1057
- "is_deselecting": self.is_deselecting,
1058
- "user_opts": self.user_opts,
1059
- "options_list": self.options_list,
1060
- "user_settings": self.user_settings,
1061
- "separator": self.separator,
1062
- "search_query": self.search_query,
1063
- "search_count": self.search_count,
1064
- "search_index": self.search_index,
1065
- "filter_query": self.filter_query,
1066
- "indexed_items": self.indexed_items,
1067
- "start_selection": self.start_selection,
1068
- "start_selection_col": self.start_selection_col,
1069
- "end_selection": self.end_selection,
1070
- "highlights": self.highlights,
1071
- "max_column_width": self.max_column_width,
1072
- "column_indices": self.column_indices,
1073
- "mode_index": self.mode_index,
1074
- "modes": self.modes,
1075
- "title": self.title,
1076
- "display_modes": self.display_modes,
1077
- "require_option": self.require_option,
1078
- "require_option_default": self.require_option_default,
1079
- "option_functions": self.option_functions,
1080
- "top_gap": self.top_gap,
1081
- "number_columns": self.number_columns,
1082
- "items": self.items,
1083
- "indexed_items": self.indexed_items,
1084
- "header": self.header,
1085
- "scroll_bar": self.scroll_bar,
1086
- "columns_sort_method": self.columns_sort_method,
1087
- "disabled_keys": self.disabled_keys,
1088
- "show_footer": self.show_footer,
1089
- "footer_string": self.footer_string,
1090
- "footer_string_auto_refresh": self.footer_string_auto_refresh,
1091
- "footer_string_refresh_function": self.footer_string_refresh_function,
1092
- "footer_timer": self.footer_timer,
1093
- "footer_style": self.footer_style,
1094
- "colours_start": self.colours_start,
1095
- "colours_end": self.colours_end,
1096
- "display_only": self.display_only,
1097
- "infobox_items": self.infobox_items,
1098
- "display_infobox": self.display_infobox,
1099
- "infobox_title": self.infobox_title,
1100
- "key_remappings": self.key_remappings,
1101
- "auto_refresh": self.auto_refresh,
1102
- "get_new_data": self.get_new_data,
1103
- "refresh_function": self.refresh_function,
1104
- "timer": self.timer,
1105
- "get_data_startup": self.get_data_startup,
1106
- "get_footer_string_startup": self.get_footer_string_startup,
1107
- "editable_columns": self.editable_columns,
1108
- "last_key": self.last_key,
1109
- "centre_in_terminal": self.centre_in_terminal,
1110
- "centre_in_terminal_vertical": self.centre_in_terminal_vertical,
1111
- "centre_in_cols": self.centre_in_cols,
1112
- "highlight_full_row": self.highlight_full_row,
1113
- "cell_cursor": self.cell_cursor,
1114
- "column_widths": self.column_widths,
1115
- "track_entries_upon_refresh": self.track_entries_upon_refresh,
1116
- "pin_cursor": self.pin_cursor,
1117
- "id_column": self.id_column,
1118
- "startup_notification": self.startup_notification,
1119
- "keys_dict": self.keys_dict,
1120
- "cancel_is_back": self.cancel_is_back,
1121
- "paginate": self.paginate,
1122
- "leftmost_column": self.leftmost_column,
1123
- "leftmost_char": self.leftmost_char,
1124
- "history_filter_and_search" : self.history_filter_and_search,
1125
- "history_pipes" : self.history_pipes,
1126
- "history_opts" : self.history_opts,
1127
- "history_edits" : self.history_edits,
1128
- "history_settings": self.history_settings,
1129
- "show_header": self.show_header,
1130
- "show_row_header": self.show_row_header,
1131
- "debug": self.debug,
1132
- "debug_level": self.debug_level,
1133
- "reset_colours": self.reset_colours,
1134
- "unicode_char_width": self.unicode_char_width,
1135
- }
1136
- return function_data
1137
-
1138
- def set_function_data(self, function_data: dict) -> None:
1139
- """ Set variables from state dict containing core variables."""
1140
- self.logger.info(f"function: set_function_data()")
1141
- variables = self.get_function_data().keys()
1142
-
1143
- for var in variables:
1144
- if var in function_data:
1145
- setattr(self, var, function_data[var])
1146
-
1147
- reset_colours = bool("colour_theme_number" in function_data)
1148
- self.initialise_picker_state(reset_colours=reset_colours)
1149
-
1150
- self.initialise_variables()
1151
- # if "colour_theme_number" in function_data:
1152
- # global COLOURS_SET
1153
- # COLOURS_SET = False
1154
- # colours_end = set_colours(pick=self.colour_theme_number, start=self.colours_start)
1155
-
1156
- # if "items" in function_data: self.items = function_data["items"]
1157
- # if "header" in function_data: self.header = function_data["header"]
1158
- # self.indexed_items = function_data["indexed_items"] if "indexed_items" in function_data else []
1159
-
1160
-
1161
-
1162
- def delete_entries(self) -> None:
1163
- """ Delete entries from view. """
1164
-
1165
- self.logger.info(f"function: delete_entries()")
1166
- # Remove selected items from the list
1167
- selected_indices = [index for index, selected in self.selections.items() if selected]
1168
- if not selected_indices:
1169
- # Remove the currently focused item if nothing is selected
1170
- selected_indices = [self.indexed_items[self.cursor_pos][0]]
1171
-
1172
- self.items = [item for i, item in enumerate(self.items) if i not in selected_indices]
1173
- self.indexed_items = [(i, item) for i, item in enumerate(self.items)]
1174
- self.selections = {i:False for i in range(len(self.indexed_items))}
1175
- self.cursor_pos = min(self.cursor_pos, len(self.indexed_items)-1)
1176
- self.initialise_variables()
1177
- self.draw_screen(self.indexed_items, self.highlights)
1178
-
1179
-
1180
- def choose_option(
1181
- self,
1182
- stdscr: curses.window,
1183
- options: list[list[str]] =[],
1184
- title: str = "Choose option",
1185
- x:int=0,
1186
- y:int=0,
1187
- literal:bool=False,
1188
- colours_start:int=0,
1189
- header: list[str] = [],
1190
- require_option:list = [],
1191
- option_functions: list = [],
1192
- ) -> Tuple[dict, str, dict]:
1193
- """
1194
- Display input field at x,y
1195
-
1196
- ---Arguments
1197
- stdscr: curses screen
1198
- usrtxt (str): text to be edited by the user
1199
- title (str): The text to be displayed at the start of the text option picker
1200
- x (int): prompt begins at (x,y) in the screen given
1201
- y (int): prompt begins at (x,y) in the screen given
1202
- colours_start (bool): start index of curses init_pair.
1203
-
1204
- ---Returns
1205
- usrtxt, return_code
1206
- usrtxt: the text inputted by the user
1207
- return_code:
1208
- 0: user hit escape
1209
- 1: user hit return
1210
- """
1211
- self.logger.info(f"function: choose_option()")
1212
- if options == []: options = [[f"{i}"] for i in range(10)]
1213
- cursor = 0
1214
-
1215
-
1216
- option_picker_data = {
1217
- "items": options,
1218
- "colours": notification_colours,
1219
- "colours_start": self.notification_colours_start,
1220
- "title":title,
1221
- "header":header,
1222
- "hidden_columns":[],
1223
- "require_option":require_option,
1224
- "keys_dict": options_keys,
1225
- "show_footer": False,
1226
- "cancel_is_back": True,
1227
- "number_columns": False,
1228
- "reset_colours": False,
1229
- }
1230
- while True:
1231
- h, w = stdscr.getmaxyx()
1232
-
1233
- choose_opts_widths = get_column_widths(options)
1234
- window_width = min(max(sum(choose_opts_widths) + 6, 50) + 6, w)
1235
- window_height = min(h//2, max(6, len(options)+3))
1236
-
1237
- submenu_win = curses.newwin(window_height, window_width, (h-window_height)//2, (w-window_width)//2)
1238
- submenu_win.keypad(True)
1239
- OptionPicker = Picker(submenu_win, **option_picker_data)
1240
- s, o, f = OptionPicker.run()
1241
-
1242
- if o == "refresh":
1243
- self.draw_screen(self.indexed_items, self.highlights)
1244
- continue
1245
- if s:
1246
- return {x: options[x] for x in s}, o, f
1247
- return {}, "", f
1248
-
1249
-
1250
-
1251
- def notification(self, stdscr: curses.window, message: str="", title:str="Notification", colours_end: int=0, duration:int=4) -> None:
1252
-
1253
- self.logger.info(f"function: notification()")
1254
- """ Notification box. """
1255
- notification_width, notification_height = 50, 7
1256
- message_width = notification_width-5
1257
-
1258
- if not message: message = "!!"
1259
- submenu_items = [" "+message[i*message_width:(i+1)*message_width] for i in range(len(message)//message_width+1)]
1260
-
1261
- notification_remap_keys = {
1262
- curses.KEY_RESIZE: curses.KEY_F5,
1263
- 27: ord('q')
1264
- }
1265
- while True:
1266
- h, w = stdscr.getmaxyx()
1267
-
1268
- submenu_win = curses.newwin(notification_height, notification_width, 3, w - (notification_width+4))
1269
- notification_data = {
1270
- "items": submenu_items,
1271
- "title": title,
1272
- "colours_start": self.notification_colours_start,
1273
- "show_footer": False,
1274
- "centre_in_terminal": True,
1275
- "centre_in_terminal_vertical": True,
1276
- "centre_in_cols": True,
1277
- "hidden_columns": [],
1278
- "keys_dict": notification_keys,
1279
- "disabled_keys": [ord('z'), ord('c')],
1280
- "highlight_full_row": True,
1281
- "top_gap": 0,
1282
- "cancel_is_back": True,
1283
- "reset_colours": False,
1284
-
1285
- }
1286
- OptionPicker = Picker(submenu_win, **notification_data)
1287
- s, o, f = OptionPicker.run()
1288
-
1289
- if o != "refresh": break
1290
- submenu_win.clear()
1291
- submenu_win.refresh()
1292
- del submenu_win
1293
- stdscr.clear()
1294
- stdscr.refresh()
1295
- self.draw_screen(self.indexed_items, self.highlights)
1296
- # set_colours(colours=get_colours(0))
1297
-
1298
- def toggle_column_visibility(self, col_index:int) -> None:
1299
- """ Toggle the visibility of the column at col_index. """
1300
- self.logger.info(f"function: toggle_column_visibility()")
1301
- if 0 <= col_index < len(self.items[0]):
1302
- if col_index in self.hidden_columns:
1303
- self.hidden_columns.remove(col_index)
1304
- else:
1305
- self.hidden_columns.append(col_index)
1306
-
1307
- def apply_settings(self) -> None:
1308
- """
1309
- The users settings will be stored in the user_settings variable. This function applies those settings.
1310
-
1311
- ![0-9]+ show/hide column
1312
- s[0-9]+ set column focus for sort
1313
- g[0-9]+ go to index
1314
- p[0-9]+ go to page
1315
- nohl hide search highlights
1316
- """
1317
- self.logger.info(f"function: apply_settings()")
1318
- if self.user_settings:
1319
- settings = re.split(r'\s+', self.user_settings)
1320
- for setting in settings:
1321
- if len(setting) == 0: continue
1322
-
1323
- if setting[0] == "!" and len(setting) > 1:
1324
- if setting[1:].isnumeric():
1325
- cols = setting[1:].split(",")
1326
- for col in cols:
1327
- self.toggle_column_visibility(int(col))
1328
- elif setting[1] == "r":
1329
- self.auto_refresh = not self.auto_refresh
1330
- elif setting[1] == "h":
1331
- self.highlights_hide = not self.highlights_hide
1332
-
1333
- elif setting in ["nhl", "nohl", "nohighlights"]:
1334
- # highlights = [highlight for highlight in highlights if "type" not in highlight or highlight["type"] != "search" ]
1335
-
1336
- self.highlights_hide = not self.highlights_hide
1337
- elif setting[0] == "s":
1338
- if 0 <= int(setting[1:]) < len(self.items[0]):
1339
- self.sort_column = int(setting[1:])
1340
- if len(self.indexed_items):
1341
- current_pos = self.indexed_items[self.cursor_pos][0]
1342
- 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
1343
- if len(self.indexed_items):
1344
- new_pos = [row[0] for row in self.indexed_items].index(current_pos)
1345
- self.cursor_pos = new_pos
1346
- elif setting == "ct":
1347
- self.centre_in_terminal = not self.centre_in_terminal
1348
- elif setting == "cc":
1349
- self.centre_in_cols = not self.centre_in_cols
1350
- elif setting == "cv":
1351
- self.centre_in_terminal_vertical = not self.centre_in_terminal_vertical
1352
- elif setting == "arb":
1353
- self.insert_row(self.cursor_pos)
1354
- elif setting == "ara":
1355
- self.insert_row(self.cursor_pos+1)
1356
- elif setting == "aca":
1357
- self.insert_column(self.selected_column+1)
1358
- elif setting == "acb":
1359
- self.insert_column(self.selected_column)
1360
- elif setting.startswith("ir"):
1361
- if setting[2:].isnumeric():
1362
- num = int(setting[2:])
1363
- else:
1364
- num = self.cursor_pos
1365
- self.insert_row(num)
1366
- elif setting.startswith("ic"):
1367
- if setting[2:].isnumeric():
1368
- num = int(setting[2:])
1369
- else:
1370
- num = self.selected_column
1371
- self.insert_column(num)
1372
-
1373
- elif setting == "modes":
1374
- self.display_modes = not self.display_modes
1375
- elif setting == "cell":
1376
- self.cell_cursor = not self.cell_cursor
1377
- elif setting == "rh":
1378
- self.show_row_header = not self.show_row_header
1379
- elif setting == "header":
1380
- self.show_header = not self.show_header
1381
- elif setting[0] == "":
1382
- cols = setting[1:].split(",")
1383
- elif setting == "footer":
1384
- self.show_footer = not self.show_footer
1385
- self.initialise_variables()
1386
- elif setting == "pc":
1387
- self.pin_cursor = not self.pin_cursor
1388
- elif setting == "unicode":
1389
- self.unicode_char_width = not self.unicode_char_width
1390
-
1391
- elif setting.startswith("ft"):
1392
- if len(setting) > 2 and setting[2:].isnumeric():
1393
-
1394
- num = int(setting[2:])
1395
- self.footer_style = max(len(self.footer_options)-1, num)
1396
- self.footer = self.footer_options[self.footer_style]
1397
- else:
1398
- self.footer_style = (self.footer_style+1)%len(self.footer_options)
1399
- self.footer = self.footer_options[self.footer_style]
1400
- self.initialise_variables()
1401
-
1402
- elif setting.startswith("cwd="):
1403
- os.chdir(os.path.expandvars(os.path.expanduser(setting[len("cwd="):])))
1404
- elif setting.startswith("hl"):
1405
- hl_list = setting.split(",")
1406
- if len(hl_list) > 1:
1407
- hl_list = hl_list[1:]
1408
- match = hl_list[0]
1409
- if len(hl_list) > 1:
1410
- field = hl_list[1]
1411
- if field.isnumeric() and field != "-1":
1412
- field = int(field)
1413
- else:
1414
- field = "all"
1415
- else:
1416
- field = "all"
1417
- if len(hl_list) > 2 and hl_list[2].isnumeric():
1418
- colour_pair = int(hl_list[2])
1419
- else:
1420
- colour_pair = 10
1421
-
1422
- highlight = {
1423
- "match": match,
1424
- "field": field,
1425
- "color": colour_pair
1426
- }
1427
- self.highlights.append(highlight)
1428
-
1429
-
1430
- elif setting.startswith("th"):
1431
- global COLOURS_SET
1432
- if curses.COLORS < 255:
1433
- self.notification(self.stdscr, message=f"Theme 4 applied.")
1434
-
1435
- elif setting[2:].strip().isnumeric():
1436
- COLOURS_SET = False
1437
- try:
1438
- theme_number = int(setting[2:].strip())
1439
- self.colour_theme_number = min(get_theme_count()-1, theme_number)
1440
- set_colours(self.colour_theme_number)
1441
- self.draw_screen(self.indexed_items, self.highlights)
1442
- self.notification(self.stdscr, message=f"Theme {self.colour_theme_number} applied.")
1443
- except:
1444
- pass
1445
- else:
1446
- COLOURS_SET = False
1447
- self.colour_theme_number = (self.colour_theme_number + 1)%get_theme_count()
1448
- # self.colour_theme_number = int(not bool(self.colour_theme_number))
1449
- set_colours(self.colour_theme_number)
1450
- self.draw_screen(self.indexed_items, self.highlights)
1451
- self.notification(self.stdscr, message=f"Theme {self.colour_theme_number} applied.")
1452
-
1453
- else:
1454
- self.user_settings = ""
1455
- return None
1456
-
1457
-
1458
- self.command_stack.append(Command("setting", self.user_settings))
1459
- self.user_settings = ""
1460
-
1461
- def apply_command(self, command: Command):
1462
- self.logger.info(f"function: apply_command()")
1463
- if command.command_type == "setting":
1464
- self.user_settings = command.command_value
1465
- self.apply_settings()
1466
-
1467
- def redo(self):
1468
- self.logger.info(f"function: redo()")
1469
- if len(self.command_stack):
1470
- self.apply_command(self.command_stack[-1])
1471
-
1472
- def toggle_item(self, index: int) -> None:
1473
- """ Toggle selection of item at index. """
1474
- self.logger.info(f"function: toggle_item()")
1475
- self.selections[index] = not self.selections[index]
1476
- self.draw_screen(self.indexed_items, self.highlights)
1477
-
1478
- def select_all(self) -> None:
1479
- """ Select all in indexed_items. """
1480
- self.logger.info(f"function: select_all()")
1481
- for i in range(len(self.indexed_items)):
1482
- self.selections[self.indexed_items[i][0]] = True
1483
- for i in self.cell_selections.keys():
1484
- self.cell_selections[i] = True
1485
-
1486
- self.draw_screen(self.indexed_items, self.highlights)
1487
-
1488
- def deselect_all(self) -> None:
1489
- """ Deselect all items in indexed_items. """
1490
- self.logger.info(f"function: deselect_all()")
1491
- for i in range(len(self.selections)):
1492
- self.selections[i] = False
1493
- for i in self.cell_selections.keys():
1494
- self.cell_selections[i] = False
1495
- self.draw_screen(self.indexed_items, self.highlights)
1496
-
1497
- def handle_visual_selection(self, selecting:bool = True) -> None:
1498
- """ Toggle visual selection or deselection. """
1499
- self.logger.info(f"function: handle_visual_selection()")
1500
- if not self.is_selecting and not self.is_deselecting and len(self.indexed_items) and len(self.indexed_items[0][1]):
1501
- self.start_selection = self.cursor_pos
1502
- self.start_selection_col = self.selected_column
1503
- if selecting:
1504
- self.is_selecting = True
1505
- else:
1506
- self.is_deselecting = True
1507
- elif self.is_selecting:
1508
- # end_selection = indexed_items[current_page * items_per_page + current_row][0]
1509
- self.end_selection = self.cursor_pos
1510
- if self.start_selection != -1:
1511
- start = max(min(self.start_selection, self.end_selection), 0)
1512
- end = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1513
- for i in range(start, end + 1):
1514
- if self.indexed_items[i][0] not in self.unselectable_indices:
1515
- self.selections[self.indexed_items[i][0]] = True
1516
- if self.start_selection != -1:
1517
- ystart = max(min(self.start_selection, self.end_selection), 0)
1518
- yend = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1519
- xstart = min(self.start_selection_col, self.selected_column)
1520
- xend = max(self.start_selection_col, self.selected_column)
1521
- for i in range(ystart, yend + 1):
1522
- if self.indexed_items[i][0] not in self.unselectable_indices:
1523
- row = self.indexed_items[i][0]
1524
- if row not in self.selected_cells_by_row:
1525
- self.selected_cells_by_row[row] = []
1526
-
1527
- for col in range(xstart, xend+1):
1528
- cell_index = (row, col)
1529
- self.cell_selections[cell_index] = True
1530
-
1531
- self.selected_cells_by_row[row].append(col)
1532
- # Remove duplicates
1533
- self.selected_cells_by_row[row] = list(set(self.selected_cells_by_row[row]))
1534
-
1535
- self.start_selection = -1
1536
- self.end_selection = -1
1537
- self.is_selecting = False
1538
-
1539
- self.draw_screen(self.indexed_items, self.highlights)
1540
-
1541
- elif self.is_deselecting:
1542
- self.end_selection = self.indexed_items[self.cursor_pos][0]
1543
- self.end_selection = self.cursor_pos
1544
- if self.start_selection != -1:
1545
- start = max(min(self.start_selection, self.end_selection), 0)
1546
- end = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1547
- for i in range(start, end + 1):
1548
- # selections[i] = False
1549
- self.selections[self.indexed_items[i][0]] = False
1550
- if self.start_selection != -1:
1551
- ystart = max(min(self.start_selection, self.end_selection), 0)
1552
- yend = min(max(self.start_selection, self.end_selection), len(self.indexed_items)-1)
1553
- xstart = min(self.start_selection_col, self.selected_column)
1554
- xend = max(self.start_selection_col, self.selected_column)
1555
- for i in range(ystart, yend + 1):
1556
- if self.indexed_items[i][0] not in self.unselectable_indices:
1557
- for j in range(xstart, xend+1):
1558
- cell_index = (self.indexed_items[i][0], j)
1559
- self.cell_selections[cell_index] = False
1560
- self.start_selection = -1
1561
- self.end_selection = -1
1562
- self.is_deselecting = False
1563
- self.draw_screen(self.indexed_items, self.highlights)
1564
-
1565
- def cursor_down(self, count=1) -> bool:
1566
- """ Move cursor down. """
1567
- self.logger.info(f"function: cursor_down()")
1568
- # Returns: whether page is turned
1569
- new_pos = self.cursor_pos + 1
1570
- new_pos = min(self.cursor_pos+count, len(self.indexed_items)-1)
1571
- while True:
1572
- if self.indexed_items[new_pos][0] in self.unselectable_indices: new_pos+=1
1573
- else: break
1574
- self.cursor_pos = new_pos
1575
- return True
1576
-
1577
- def cursor_up(self, count=1) -> bool:
1578
- """ Move cursor up. """
1579
- self.logger.info(f"function: cursor_up()")
1580
- # Returns: whether page is turned
1581
-
1582
- new_pos = max(self.cursor_pos - count, 0)
1583
- while True:
1584
- if new_pos < 0: return False
1585
- elif new_pos in self.unselectable_indices: new_pos -= 1
1586
- else: break
1587
- self.cursor_pos = new_pos
1588
- return True
1589
-
1590
- def remapped_key(self, key: int, val: int, key_remappings: dict) -> bool:
1591
- """ Check if key has been remapped to val in key_remappings. """
1592
- # self.logger.info(f"function: remapped_key()")
1593
- if key in key_remappings:
1594
- if key_remappings[key] == val or (isinstance(key_remappings[key], list) and val in key_remappings[key]):
1595
- return True
1596
- return False
1597
-
1598
- def check_key(self, function: str, key: int, keys_dict: dict) -> bool:
1599
- """
1600
- Check if $key is assigned to $function in the keys_dict.
1601
- Allows us to redefine functions to different keys in the keys_dict.
1602
-
1603
- E.g., keys_dict = { $key, "help": ord('?') },
1604
- """
1605
- if function in keys_dict and key in keys_dict[function]:
1606
- return True
1607
- return False
1608
-
1609
- def copy_dialogue(self) -> None:
1610
- """ Display dialogue to select how rows/cells should be copied. """
1611
- self.logger.info(f"function: copy_dialogue()")
1612
- copy_header = [
1613
- "Representation",
1614
- "Columns",
1615
- ]
1616
- options = [
1617
- ["Python list of lists", "Exclude hidden"],
1618
- ["Python list of lists", "Include hidden"],
1619
- ["Tab-separated values", "Exclude hidden"],
1620
- ["Tab-separated values", "Include hidden"],
1621
- ["Comma-separated values", "Exclude hidden"],
1622
- ["Comma-separated values", "Include hidden"],
1623
- ["Custom separator", "Exclude hidden"],
1624
- ["Custom separator", "Include hidden"],
1625
- ]
1626
- require_option = [False, False, False, False, False, False, True, True]
1627
- s, o, f = self.choose_option(self.stdscr, options=options, title="Copy selected", header=copy_header, require_option=require_option)
1628
-
1629
-
1630
- funcs = [
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=False, 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="python", copy_hidden_cols=True, 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=False, 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="tsv", copy_hidden_cols=True, 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=False, 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="csv", copy_hidden_cols=True, 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=False, separator=o, cellwise=cell_cursor),
1638
- 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),
1639
- ]
1640
-
1641
- # Copy items based on selection
1642
- if s:
1643
- for idx in s.keys():
1644
- funcs[idx](self.items, self.indexed_items, self.selections, self.cell_selections, self.hidden_columns, self.cell_cursor)
1645
- def paste_dialogue(self) -> None:
1646
- """ Display dialogue to select how to paste from the clipboard. """
1647
- self.logger.info(f"function: paste_dialogue()")
1648
- paste_header = [
1649
- "Representation",
1650
- "Columns",
1651
- ]
1652
- options = [
1653
- ["Paste values", ""],
1654
- ]
1655
- require_option = [False]
1656
- s, o, f = self.choose_option(self.stdscr, options=options, title="Paste values", header=paste_header, require_option=require_option)
1657
-
1658
-
1659
- funcs = [
1660
- lambda items, pasta, paste_row, paste_col: paste_values(items, pasta, paste_row, paste_col)
1661
- ]
1662
-
1663
- try:
1664
- pasta = eval(pyperclip.paste())
1665
- if type(pasta) == type([]):
1666
- acceptable_data_type = True
1667
- for row in pasta:
1668
- if type(row) != type([]):
1669
- acceptable_data_type = False
1670
- break
1671
-
1672
- for cell in row:
1673
- if cell != None and type(cell) != type(""):
1674
- acceptable_data_type = False
1675
- break
1676
- if not acceptable_data_type:
1677
- break
1678
- if not acceptable_data_type:
1679
- self.draw_screen(self.indexed_items, self.highlights)
1680
- self.notification(self.stdscr, message="Error pasting data.")
1681
- return None
1682
-
1683
- except:
1684
- self.draw_screen(self.indexed_items, self.highlights)
1685
- self.notification(self.stdscr, message="Error pasting data.")
1686
- return None
1687
- if type(pasta) == type([]) and len(pasta) > 0 and type(pasta[0]) == type([]):
1688
- if s:
1689
- for idx in s.keys():
1690
- return_val, tmp_items = funcs[idx](self.items, pasta, self.cursor_pos, self.selected_column)
1691
- if return_val:
1692
- cursor_pos = self.cursor_pos
1693
- self.items = tmp_items
1694
- self.initialise_variables()
1695
- self.cursor_pos = cursor_pos
1696
-
1697
- def save_dialog(self) -> None:
1698
- """ Display dialogue to select how to save the picker data. """
1699
- self.logger.info(f"function: save_dialog()")
1700
-
1701
- dump_header = []
1702
- options = [
1703
- ["Save data (pickle)."],
1704
- ["Save data (csv)."],
1705
- ["Save data (tsv)."],
1706
- ["Save data (json)."],
1707
- ["Save data (feather)."],
1708
- ["Save data (parquet)."],
1709
- ["Save data (msgpack)."],
1710
- ["Save state"]
1711
- ]
1712
- # require_option = [True, True, True, True, True, True, True, True]
1713
- s, o, f = self.choose_option(self.stdscr, options=options, title="Save...", header=dump_header)
1714
-
1715
-
1716
- funcs = [
1717
- lambda opts: dump_data(self.get_function_data(), opts),
1718
- lambda opts: dump_data(self.get_function_data(), opts, format="csv"),
1719
- lambda opts: dump_data(self.get_function_data(), opts, format="tsv"),
1720
- lambda opts: dump_data(self.get_function_data(), opts, format="json"),
1721
- lambda opts: dump_data(self.get_function_data(), opts, format="feather"),
1722
- lambda opts: dump_data(self.get_function_data(), opts, format="parquet"),
1723
- lambda opts: dump_data(self.get_function_data(), opts, format="msgpack"),
1724
- lambda opts: dump_state(self.get_function_data(), opts),
1725
- ]
1726
-
1727
- if s:
1728
- for idx in s.keys():
1729
- save_path_entered, save_path = output_file_option_selector(
1730
- self.stdscr,
1731
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights)
1732
- )
1733
- if save_path_entered:
1734
- return_val = funcs[idx](save_path)
1735
- if return_val:
1736
- self.notification(self.stdscr, message=return_val, title="Error")
1737
-
1738
- def load_dialog(self) -> None:
1739
- """ Display dialogue to select which file to load and in what way it should be loaded. """
1740
- self.logger.info(f"function: load_dialog()")
1741
-
1742
- dump_header = []
1743
- options = [
1744
- ["Load data (pickle)."],
1745
- ]
1746
- s, o, f = self.choose_option(self.stdscr, options=options, title="Open file...", header=dump_header)
1747
-
1748
-
1749
- funcs = [
1750
- lambda opts: load_state(opts)
1751
- ]
1752
-
1753
- if s:
1754
- file_to_load = file_picker()
1755
- if file_to_load:
1756
- index = list(s.keys())[0]
1757
- return_val = funcs[index](file_to_load)
1758
- self.set_function_data(return_val)
1759
-
1760
- # items = return_val["items"]
1761
- # header = return_val["header"]
1762
- self.initialise_variables()
1763
- self.draw_screen(self.indexed_items, self.highlights)
1764
- self.notification(self.stdscr, f"{repr(file_to_load)} has been loaded!")
1765
- # if return_val:
1766
- # notification(stdscr, message=return_val, title="Error")
1767
-
1768
- def set_registers(self):
1769
- """ Set registers to be sent to the input field. """
1770
- self.logger.info(f"function: set_registers()")
1771
- 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 {}
1772
-
1773
-
1774
- def fetch_data(self) -> None:
1775
- """ Refesh data asynchronously. When data has been fetched self.data_ready is set to True. """
1776
- self.logger.info(f"function: fetch_data()")
1777
- tmp_items, tmp_header = self.refresh_function()
1778
- if self.track_entries_upon_refresh:
1779
- selected_indices = get_selected_indices(self.selections)
1780
- self.ids = [item[self.id_column] for i, item in enumerate(self.items) if i in selected_indices]
1781
- self.ids_tuples = [(i, item[self.id_column]) for i, item in enumerate(self.items) if i in selected_indices]
1782
- self.selected_cells_by_row = get_selected_cells_by_row(self.cell_selections)
1783
-
1784
- if len(self.indexed_items) > 0 and len(self.indexed_items) >= self.cursor_pos and len(self.indexed_items[0][1]) >= self.id_column:
1785
- self.cursor_pos_id = self.indexed_items[self.cursor_pos][1][self.id_column]
1786
- with self.data_lock:
1787
- self.items, self.header = tmp_items, tmp_header
1788
- self.data_ready = True
1789
-
1790
- def save_input_history(self, file_path: str) -> bool:
1791
- """ Save input field history. Returns True if successful save. """
1792
- self.logger.info(f"function: save_input_history()")
1793
- file_path = os.path.expanduser(file_path)
1794
- history_dict = {
1795
- "history_filter_and_search" : self.history_filter_and_search,
1796
- "history_pipes" : self.history_pipes,
1797
- "history_opts" : self.history_opts,
1798
- "history_edits" : self.history_edits,
1799
- "history_settings": self.history_settings,
1800
- }
1801
- with open(file_path, 'w') as f:
1802
- json.dump(history_dict, f)
1803
-
1804
- return True
1805
-
1806
- def load_input_history(self, file_path:str) -> bool:
1807
- """ Load command history. Returns true if successful load. """
1808
- self.logger.info(f"function: load_input_history()")
1809
- file_path = os.path.expanduser(file_path)
1810
- if not os.path.exists(file_path):
1811
- return False
1812
- try:
1813
- with open(file_path, 'r') as f:
1814
- history_dict = json.load(f)
1815
-
1816
- if "history_filter_and_search" in history_dict:
1817
- self.history_filter_and_search = history_dict["history_filter_and_search"]
1818
- if "history_pipes" in history_dict:
1819
- self.history_pipes = history_dict["history_pipes"]
1820
- if "history_opts" in history_dict:
1821
- self.history_opts = history_dict["history_opts"]
1822
- if "history_edits" in history_dict:
1823
- self.history_edits = history_dict["history_edits"]
1824
- if "history_settings" in history_dict:
1825
- self.history_settings = history_dict["history_settings"]
1826
-
1827
- except:
1828
- return False
1829
-
1830
- return True
1831
-
1832
-
1833
- def get_word_list(self) -> list[str]:
1834
- """ Get a list of all words used in any cell of the picker. Used for completion in search/filter input_field. """
1835
- self.logger.info(f"function: get_word_list()")
1836
- translator = str.maketrans('', '', string.punctuation)
1837
-
1838
- words = []
1839
- # Extract words from lists
1840
- for row in [x[1] for x in self.indexed_items]:
1841
- for i, cell in enumerate(row):
1842
- if i != (self.id_column%len(row)):
1843
- # Split the item into words and strip punctuation from each word
1844
- words.extend(word.strip(string.punctuation) for word in cell.split())
1845
- for cell in self.header:
1846
- # Split the item into words and strip punctuation from each word
1847
- words.extend(word.strip(string.punctuation) for word in cell.split())
1848
- def key_f(s):
1849
- if len(s):
1850
- starts_with_char = s[0].isalpha()
1851
- else:
1852
- starts_with_char = False
1853
- return (not starts_with_char, s.lower())
1854
- # key = lambda s: (s != "" or not s[0].isalpha(), s)
1855
- words = sorted(list(set(words)), key=key_f)
1856
- return words
1857
-
1858
- def insert_row(self, pos: int):
1859
- """ Insert a blank row at position `pos` """
1860
- self.logger.info(f"function: insert_row(pos={pos})")
1861
-
1862
- if self.items != [[]]:
1863
- row_len = 1
1864
- if self.header: row_len = len(self.header)
1865
- elif len(self.items): row_len = len(self.items[0])
1866
- # if len(self.indexed_items) == 0:
1867
- # insert_at_pos = 0
1868
- # else:
1869
- # insert_at_pos = self.indexed_items[self.cursor_pos][0]
1870
- self.items = self.items[:pos] + [["" for x in range(row_len)]] + self.items[pos:]
1871
- if pos <= self.cursor_pos:
1872
- self.cursor_pos += 1
1873
- # We are adding a row before so we have to move the cursor down
1874
- # If there is a filter then we know that an empty row doesn't match
1875
- current_cursor_pos = self.cursor_pos
1876
- self.initialise_variables()
1877
- self.cursor_pos = current_cursor_pos
1878
- else:
1879
- self.items = [[""]]
1880
- self.initialise_variables()
1881
-
1882
- def insert_column(self, pos:int):
1883
- """ Insert blank column at `pos`"""
1884
- self.logger.info(f"function: insert_column(pos={pos})")
1885
- self.items = [row[:pos]+[""]+row[pos:] for row in self.items]
1886
- self.header = self.header[:pos] + [""] + self.header[pos:]
1887
- self.editable_columns = self.editable_columns[:pos] + [self.editable_by_default] + self.editable_columns[pos:]
1888
- if pos <= self.selected_column:
1889
- self.selected_column += 1
1890
- current_cursor_pos = self.cursor_pos
1891
- self.initialise_variables()
1892
- self.cursor_pos = current_cursor_pos
1893
-
1894
-
1895
-
1896
- def run(self) -> Tuple[list[int], str, dict]:
1897
- """ Run the picker. """
1898
- self.logger.info(f"function: run()")
1899
-
1900
- if self.get_footer_string_startup and self.footer_string_refresh_function != None:
1901
- self.footer_string = self.footer_string_refresh_function()
1902
-
1903
- self.initialise_variables(get_data=self.get_data_startup)
1904
-
1905
- self.draw_screen(self.indexed_items, self.highlights)
1906
-
1907
- initial_time = time.time()
1908
- initial_time_footer = time.time()-self.footer_timer
1909
-
1910
- if self.startup_notification:
1911
- self.notification(self.stdscr, message=self.startup_notification)
1912
- self.startup_notification = ""
1913
-
1914
- # curses.curs_set(0)
1915
- # stdscr.nodelay(1) # Non-blocking input
1916
- # stdscr.timeout(2000) # Set a timeout for getch() to ensure it does not block indefinitely
1917
- 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
1918
-
1919
- if self.clear_on_start:
1920
- self.stdscr.clear()
1921
- self.clear_on_start = False
1922
- else:
1923
- self.stdscr.erase()
1924
-
1925
- self.stdscr.refresh()
1926
-
1927
- # Initialize colours
1928
- # Check if terminal supports color
1929
-
1930
- # Set terminal background color
1931
- self.stdscr.bkgd(' ', curses.color_pair(self.colours_start+3)) # Apply background color
1932
- self.draw_screen(self.indexed_items, self.highlights)
1933
-
1934
- if self.display_only:
1935
- self.stdscr.refresh()
1936
- function_data = self.get_function_data()
1937
- return [], "", function_data
1938
-
1939
- # Main loop
1940
-
1941
- while True:
1942
- key = self.stdscr.getch()
1943
- if key in self.disabled_keys: continue
1944
- clear_screen=True
1945
-
1946
- ## Refresh data asyncronously.
1947
- if self.refreshing_data:
1948
- self.logger.debug(f"Data refresh check")
1949
- with self.data_lock:
1950
- if self.data_ready:
1951
- self.logger.debug(f"Data ready after refresh")
1952
- self.initialise_variables()
1953
-
1954
- initial_time = time.time()
1955
-
1956
- self.draw_screen(self.indexed_items, self.highlights, clear=False)
1957
-
1958
- self.refreshing_data = False
1959
- self.data_ready = False
1960
-
1961
- 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):
1962
- self.logger.debug(f"Get new data (refresh).")
1963
- h, w = self.stdscr.getmaxyx()
1964
- self.stdscr.addstr(0,w-3,"  ", curses.color_pair(self.colours_start+21) | curses.A_BOLD)
1965
- self.stdscr.refresh()
1966
- if self.get_new_data and self.refresh_function:
1967
- self.refreshing_data = True
1968
-
1969
- t = threading.Thread(target=self.fetch_data)
1970
- t.start()
1971
- else:
1972
- function_data = self.get_function_data()
1973
- return [], "refresh", function_data
1974
-
1975
- # Refresh data synchronously
1976
- # 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):
1977
- # h, w = self.stdscr.getmaxyx()
1978
- # self.stdscr.addstr(0,w-3,"  ", curses.color_pair(self.colours_start+21) | curses.A_BOLD)
1979
- # self.stdscr.refresh()
1980
- # if self.get_new_data and self.refresh_function:
1981
- # self.initialise_variables(get_data=True)
1982
- #
1983
- # initial_time = time.time()
1984
- # self.draw_screen(self.indexed_items, self.highlights, clear=False)
1985
- # else:
1986
- #
1987
- # function_data = self.get_function_data()
1988
- # return [], "refresh", function_data
1989
-
1990
- if self.footer_string_auto_refresh and ((time.time() - initial_time_footer) > self.footer_timer):
1991
- self.logger.debug(f"footer_string_auto_refresh")
1992
- self.footer_string = self.footer_string_refresh_function()
1993
- initial_time_footer = time.time()
1994
- self.draw_screen(self.indexed_items, self.highlights)
1995
-
1996
- if self.check_key("help", key, self.keys_dict):
1997
- self.logger.info(f"key_function help")
1998
- self.stdscr.clear()
1999
- self.stdscr.refresh()
2000
- help_data = {
2001
- # "items": help_lines,
2002
- "items": build_help_rows(self.keys_dict),
2003
- "title": f"{self.title} Help",
2004
- "colours_start": self.help_colours_start,
2005
- "colours": help_colours,
2006
- "show_footer": True,
2007
- "max_selected": 1,
2008
- "keys_dict": help_keys,
2009
- "disabled_keys": [ord('?'), ord('v'), ord('V'), ord('m'), ord('M'), ord('l'), curses.KEY_ENTER, ord('\n')],
2010
- "highlight_full_row": True,
2011
- "top_gap": 0,
2012
- "paginate": self.paginate,
2013
- "centre_in_terminal": True,
2014
- "centre_in_terminal_vertical": True,
2015
- "hidden_columns": [],
2016
- "reset_colours": False,
2017
-
2018
- }
2019
- OptionPicker = Picker(self.stdscr, **help_data)
2020
- s, o, f = OptionPicker.run()
2021
-
2022
- elif self.check_key("exit", key, self.keys_dict):
2023
- self.stdscr.clear()
2024
- function_data = self.get_function_data()
2025
- function_data["last_key"] = key
2026
- return [], "", function_data
2027
- elif self.check_key("full_exit", key, self.keys_dict):
2028
- close_curses(self.stdscr)
2029
- exit()
2030
-
2031
- elif self.check_key("settings_input", key, self.keys_dict):
2032
- self.logger.info(f"Settings input")
2033
- usrtxt = f"{self.user_settings.strip()} " if self.user_settings else ""
2034
- field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2035
- if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2036
- else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2037
- self.set_registers()
2038
- usrtxt, return_val = input_field(
2039
- self.stdscr,
2040
- usrtxt=usrtxt,
2041
- field_prefix=" Settings: ",
2042
- x=lambda:2,
2043
- y=lambda: self.stdscr.getmaxyx()[0]-1,
2044
- max_length=field_end_f,
2045
- registers=self.registers,
2046
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2047
- history=self.history_settings,
2048
- path_auto_complete=True,
2049
- formula_auto_complete=False,
2050
- function_auto_complete=False,
2051
- word_auto_complete=True,
2052
- auto_complete_words=["ft", "ct", "cv"]
2053
- )
2054
- if return_val:
2055
- self.user_settings = usrtxt
2056
- self.apply_settings()
2057
- self.history_settings.append(usrtxt)
2058
- self.user_settings = ""
2059
- elif self.check_key("toggle_footer", key, self.keys_dict):
2060
- self.logger.info(f"toggle footer")
2061
- self.user_settings = "footer"
2062
- self.apply_settings()
2063
-
2064
- elif self.check_key("settings_options", key, self.keys_dict):
2065
- options = []
2066
- if len(self.items) > 0:
2067
- options += [["cv", "Centre rows vertically"]]
2068
- options += [["pc", "Pin cursor to row number when data refreshes"]]
2069
- options += [["ct", "Centre column-set in terminal"]]
2070
- options += [["cc", "Centre values in cells"]]
2071
- options += [["!r", "Toggle auto-refresh"]]
2072
- options += [["th", "Cycle between themes. (accepts th#)"]]
2073
- options += [["nohl", "Toggle highlights"]]
2074
- options += [["footer", "Toggle footer"]]
2075
- options += [["header", "Toggle header"]]
2076
- options += [["rh", "Toggle row header"]]
2077
- options += [["modes", "Toggle modes"]]
2078
- options += [["ft", "Cycle through footer styles (accepts ft#)"]]
2079
- options += [["unicode", "Toggle b/w using len and wcwidth to calculate char width."]]
2080
- options += [[f"s{i}", f"Select col. {i}"] for i in range(len(self.items[0]))]
2081
- options += [[f"!{i}", f"Toggle col. {i}"] for i in range(len(self.items[0]))]
2082
- options += [["ara", "Add empty row after cursor."]]
2083
- options += [["arb", "Add empty row before the cursor."]]
2084
- options += [["aca", "Add empty column after the selected column."]]
2085
- options += [["acb", "Add empty column before the selected column."]]
2086
-
2087
-
2088
- settings_options_header = ["Key", "Setting"]
2089
-
2090
- s, o, f = self.choose_option(self.stdscr, options=options, title="Settings", header=settings_options_header)
2091
- if s:
2092
- self.user_settings = " ".join([x[0] for x in s.values()])
2093
- self.apply_settings()
2094
-
2095
- elif self.check_key("redo", key, self.keys_dict):
2096
- self.redo()
2097
- # elif self.check_key("move_column_left", key, self.keys_dict):
2098
- # tmp1 = self.column_indices[self.selected_column]
2099
- # tmp2 = self.column_indices[(self.selected_column-1)%len(self.column_indices)]
2100
- # self.column_indices[self.selected_column] = tmp2
2101
- # self.column_indices[(self.selected_column-1)%(len(self.column_indices))] = tmp1
2102
- # self.selected_column = (self.selected_column-1)%len(self.column_indices)
2103
- # # self.notification(self.stdscr, f"{str(self.column_indices)}, {tmp1}, {tmp2}")
2104
- # self.initialise_variables()
2105
- # 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)
2106
- # self.draw_screen(self.indexed_items, self.highlights)
2107
- # # self.move_column(direction=-1)
2108
- #
2109
- # elif self.check_key("move_column_right", key, self.keys_dict):
2110
- # tmp1 = self.column_indices[self.selected_column]
2111
- # tmp2 = self.column_indices[(self.selected_column+1)%len(self.column_indices)]
2112
- # self.column_indices[self.selected_column] = tmp2
2113
- # self.column_indices[(self.selected_column+1)%(len(self.column_indices))] = tmp1
2114
- # self.selected_column = (self.selected_column+1)%len(self.column_indices)
2115
- # self.initialise_variables()
2116
- # self.draw_screen(self.indexed_items, self.highlights)
2117
- # # self.move_column(direction=1)
2118
-
2119
- elif self.check_key("cursor_down", key, self.keys_dict):
2120
- page_turned = self.cursor_down()
2121
- if not page_turned: clear_screen = False
2122
- elif self.check_key("half_page_down", key, self.keys_dict):
2123
- clear_screen = False
2124
- self.cursor_down(count=self.items_per_page//2)
2125
- elif self.check_key("five_down", key, self.keys_dict):
2126
- clear_screen = False
2127
- self.cursor_down(count=5)
2128
- elif self.check_key("cursor_up", key, self.keys_dict):
2129
- page_turned = self.cursor_up()
2130
- if not page_turned: clear_screen = False
2131
- elif self.check_key("five_up", key, self.keys_dict):
2132
- # if self.cursor_up(count=5): clear_screen = True
2133
- self.cursor_up(count=5)
2134
- elif self.check_key("half_page_up", key, self.keys_dict):
2135
- self.cursor_up(count=self.items_per_page//2)
2136
-
2137
- elif self.check_key("toggle_select", key, self.keys_dict):
2138
- if len(self.indexed_items) > 0:
2139
- item_index = self.indexed_items[self.cursor_pos][0]
2140
- cell_index = (self.indexed_items[self.cursor_pos][0], self.selected_column)
2141
- selected_count = sum(self.selections.values())
2142
- if self.max_selected == -1 or selected_count >= self.max_selected:
2143
- self.toggle_item(item_index)
2144
-
2145
- self.cell_selections[cell_index] = not self.cell_selections[cell_index]
2146
- ## Set self.selected_cells_by_row
2147
- # If any cells in the current row are selected
2148
- if row in self.selected_cells_by_row:
2149
- # If the current cell is selected then remove it
2150
- if col in self.selected_cells_by_row[row]:
2151
- # If the current cell is the only cell in the row that is selected then remove the row from the dict
2152
- if len(self.selected_cells_by_row[row]) == 1:
2153
-
2154
- del self.selected_cells_by_row[row]
2155
- # else remove only the index of the current cell
2156
- else:
2157
- self.selected_cells_by_row[row].remove(col)
2158
- # If there are cells in the row that are selected then append the current cell to the row
2159
- else:
2160
- self.selected_cells_by_row[row].append(col)
2161
- # Add the a list containing only the current column
2162
- else:
2163
- self.selected_cells_by_row[row] = [col]
2164
-
2165
- self.cursor_down()
2166
- elif self.check_key("select_all", key, self.keys_dict): # Select all (m or ctrl-a)
2167
- self.select_all()
2168
-
2169
- elif self.check_key("select_none", key, self.keys_dict): # Deselect all (M or ctrl-r)
2170
- self.deselect_all()
2171
-
2172
- elif self.check_key("cursor_top", key, self.keys_dict):
2173
- new_pos = 0
2174
- while True:
2175
- if new_pos in self.unselectable_indices: new_pos+=1
2176
- else: break
2177
- if new_pos < len(self.indexed_items):
2178
- self.cursor_pos = new_pos
2179
-
2180
- self.draw_screen(self.indexed_items, self.highlights)
2181
-
2182
- elif self.check_key("cursor_bottom", key, self.keys_dict):
2183
- new_pos = len(self.indexed_items)-1
2184
- while True:
2185
- if new_pos in self.unselectable_indices: new_pos-=1
2186
- else: break
2187
- if new_pos < len(self.items) and new_pos >= 0:
2188
- self.cursor_pos = new_pos
2189
-
2190
- elif self.check_key("enter", key, self.keys_dict):
2191
- self.logger.info(f"key_function enter")
2192
- # Print the selected indices if any, otherwise print the current index
2193
- if self.is_selecting or self.is_deselecting: self.handle_visual_selection()
2194
- if len(self.items) == 0:
2195
- function_data = self.get_function_data()
2196
- function_data["last_key"] = key
2197
- return [], "", function_data
2198
- selected_indices = get_selected_indices(self.selections)
2199
- if not selected_indices and len(self.indexed_items):
2200
- selected_indices = [self.indexed_items[self.cursor_pos][0]]
2201
-
2202
- options_sufficient = True
2203
- usrtxt = self.user_opts
2204
- for index in selected_indices:
2205
- if self.require_option[index]:
2206
- if self.option_functions[index] != None:
2207
- options_sufficient, usrtxt = self.option_functions[index](
2208
- stdscr=self.stdscr,
2209
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights)
2210
- )
2211
- else:
2212
- self.set_registers()
2213
- options_sufficient, usrtxt = default_option_input(
2214
- self.stdscr,
2215
- starting_value=self.user_opts,
2216
- registers = self.registers
2217
- )
2218
-
2219
- if options_sufficient:
2220
- self.user_opts = usrtxt
2221
- self.stdscr.clear()
2222
- self.stdscr.refresh()
2223
- function_data = self.get_function_data()
2224
- function_data["last_key"] = key
2225
- return selected_indices, usrtxt, function_data
2226
- elif self.check_key("page_down", key, self.keys_dict): # Next page
2227
- self.cursor_pos = min(len(self.indexed_items) - 1, self.cursor_pos+self.items_per_page)
2228
-
2229
- elif self.check_key("page_up", key, self.keys_dict):
2230
- self.cursor_pos = max(0, self.cursor_pos-self.items_per_page)
2231
-
2232
- elif self.check_key("redraw_screen", key, self.keys_dict):
2233
- self.logger.info(f"key_function redraw_screen")
2234
- self.stdscr.clear()
2235
- self.stdscr.refresh()
2236
- restrict_curses(self.stdscr)
2237
- unrestrict_curses(self.stdscr)
2238
- self.stdscr.clear()
2239
- self.stdscr.refresh()
2240
-
2241
- self.draw_screen(self.indexed_items, self.highlights)
2242
-
2243
- elif self.check_key("cycle_sort_method", key, self.keys_dict):
2244
- if self.sort_column == self.selected_column:
2245
- self.columns_sort_method[self.sort_column] = (self.columns_sort_method[self.sort_column]+1) % len(self.SORT_METHODS)
2246
- else:
2247
- self.sort_column = self.selected_column
2248
- if len(self.indexed_items) > 0:
2249
- current_index = self.indexed_items[self.cursor_pos][0]
2250
- 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
2251
- self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2252
-
2253
- self.logger.info(f"key_function cycle_sort_method. (sort_column, sort_method) = ({self.sort_column}, {self.columns_sort_method[self.sort_column]})")
2254
- elif self.check_key("cycle_sort_method_reverse", key, self.keys_dict): # Cycle sort method
2255
- old_sort_column = self.sort_column
2256
- self.sort_column = self.selected_column
2257
- self.columns_sort_method[self.sort_column] = (self.columns_sort_method[self.sort_column]-1) % len(self.SORT_METHODS)
2258
- if len(self.indexed_items) > 0:
2259
- current_index = self.indexed_items[self.cursor_pos][0]
2260
- 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
2261
- self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2262
- self.logger.info(f"key_function cycle_sort_method. (sort_column, sort_method) = ({self.sort_column}, {self.columns_sort_method[self.sort_column]})")
2263
-
2264
- elif self.check_key("cycle_sort_order", key, self.keys_dict): # Toggle sort order
2265
- self.sort_reverse[self.sort_column] = not self.sort_reverse[self.sort_column]
2266
- if len(self.indexed_items) > 0:
2267
- current_index = self.indexed_items[self.cursor_pos][0]
2268
- 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
2269
- self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2270
- 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]})")
2271
- elif self.check_key("col_select", key, self.keys_dict):
2272
- col_index = key - ord('0')
2273
- self.logger.info(f"key_function col_select {col_index}")
2274
- if 0 <= col_index < len(self.items[0]):
2275
- self.sort_column = col_index
2276
- if len(self.indexed_items) > 0:
2277
- current_index = self.indexed_items[self.cursor_pos][0]
2278
- 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
2279
- self.cursor_pos = [row[0] for row in self.indexed_items].index(current_index)
2280
- elif self.check_key("col_select_next", key, self.keys_dict):
2281
- if len(self.items) > 0 and len(self.items[0]) > 0:
2282
- col_index = (self.selected_column +1) % (len(self.items[0]))
2283
- self.selected_column = col_index
2284
- # Flash when we loop back to the first column
2285
- # if self.selected_column == 0:
2286
- # curses.flash()
2287
- self.logger.info(f"key_function col_select_next {self.selected_column}")
2288
-
2289
-
2290
- ## Scroll with column select
2291
- rows = self.get_visible_rows()
2292
- self.column_widths = get_column_widths(rows, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2293
- visible_column_widths = [c for i,c in enumerate(self.column_widths) if i not in self.hidden_columns]
2294
- visible_columns_total_width = sum(visible_column_widths) + len(self.separator)*(len(visible_column_widths)-1)
2295
- h, w = self.stdscr.getmaxyx()
2296
- end_of_cell = sum(visible_column_widths[:self.selected_column+1])+len(self.separator)*(self.selected_column+1)
2297
- display_width = w-self.startx
2298
- # If the full column is within the current display then don't do anything
2299
- if start_of_cell >= self.leftmost_char and end_of_cell <= self.leftmost_char + display_width:
2300
- pass
2301
- # Otherwise right-justify the cell
2302
- else:
2303
- self.leftmost_char = end_of_cell - display_width
2304
-
2305
- self.leftmost_char = max(0, min(column_set_width - display_width + 5, self.leftmost_char))
2306
-
2307
- elif self.check_key("col_select_prev", key, self.keys_dict):
2308
- if len(self.items) > 0 and len(self.items[0]) > 0:
2309
- col_index = (self.selected_column -1) % (len(self.items[0]))
2310
- self.selected_column = col_index
2311
-
2312
- self.logger.info(f"key_function col_select_prev {self.selected_column}")
2313
- # Flash when we loop back to the last column
2314
- # if self.selected_column == len(self.column_widths)-1:
2315
- # curses.flash()
2316
-
2317
- ## Scroll with column select
2318
- rows = self.get_visible_rows()
2319
- 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)
2320
- visible_column_widths = [c for i,c in enumerate(self.column_widths) if i not in self.hidden_columns]
2321
- column_set_width = sum(visible_column_widths)+len(self.separator)*len(visible_column_widths)
2322
- start_of_cell = sum(visible_column_widths[:self.selected_column])+len(self.separator)*self.selected_column
2323
- end_of_cell = sum(visible_column_widths[:self.selected_column+1])+len(self.separator)*(self.selected_column+1)
2324
- display_width = w-self.startx
2325
-
2326
- # If the entire column is within the current display then don't do anything
2327
- if start_of_cell >= self.leftmost_char and end_of_cell <= self.leftmost_char + display_width:
2328
- pass
2329
- # Otherwise left-justify the cell
2330
- else:
2331
- self.leftmost_char = start_of_cell
2332
-
2333
- self.leftmost_char = max(0, min(column_set_width - display_width + 5, self.leftmost_char))
2334
-
2335
- elif self.check_key("scroll_right", key, self.keys_dict):
2336
- self.logger.info(f"key_function scroll_right")
2337
- if len(self.indexed_items):
2338
- row_width = sum(self.column_widths) + len(self.separator)*(len(self.column_widths)-1)
2339
- if row_width-self.leftmost_char >= w-self.startx:
2340
- self.leftmost_char = self.leftmost_char+5
2341
-
2342
- elif self.check_key("scroll_left", key, self.keys_dict):
2343
- self.logger.info(f"key_function scroll_left")
2344
- self.leftmost_char = max(self.leftmost_char-5, 0)
2345
-
2346
- elif self.check_key("scroll_far_left", key, self.keys_dict):
2347
- self.logger.info(f"key_function scroll_far_left")
2348
- self.leftmost_char = 0
2349
- self.selected_column = 0
2350
-
2351
- elif self.check_key("scroll_far_right", key, self.keys_dict):
2352
- self.logger.info(f"key_function scroll_far_right")
2353
- longest_row_str_len = 0
2354
- rows = self.get_visible_rows()
2355
- for i in range(len(rows)):
2356
- item = rows[i]
2357
- row_str = format_row(item, self.hidden_columns, self.column_widths, self.separator, self.centre_in_cols)
2358
- if len(row_str) > longest_row_str_len: longest_row_str_len=len(row_str)
2359
- # for i in range(len(self.indexed_items)):
2360
- # item = self.indexed_items[i]
2361
- # row_str = format_row(item[1], self.hidden_columns, self.column_widths, self.separator, self.centre_in_cols)
2362
- # if len(row_str) > longest_row_str_len: longest_row_str_len=len(row_str)
2363
- # self.notification(self.stdscr, f"{longest_row_str_len}")
2364
- self.leftmost_char = max(0, longest_row_str_len-w+2+self.startx)
2365
- if len(self.items):
2366
- self.selected_column = len(self.items[0])-1
2367
-
2368
- elif self.check_key("add_column_before", key, self.keys_dict):
2369
- self.logger.info(f"key_function add_column_before")
2370
- # self.add_column_before()
2371
- self.insert_column(self.selected_column)
2372
-
2373
- elif self.check_key("add_column_after", key, self.keys_dict):
2374
- self.logger.info(f"key_function add_column_after")
2375
- # self.add_column_after()
2376
- self.insert_column(self.selected_column+1)
2377
-
2378
- elif self.check_key("add_row_before", key, self.keys_dict):
2379
- self.logger.info(f"key_function add_row_before")
2380
- # self.add_row_before()
2381
- self.insert_row(self.cursor_pos)
2382
-
2383
- elif self.check_key("add_row_after", key, self.keys_dict):
2384
- self.logger.info(f"key_function add_row_after")
2385
- # self.add_row_after()
2386
- self.insert_row(self.cursor_pos+1)
2387
-
2388
- elif self.check_key("col_hide", key, self.keys_dict):
2389
- self.logger.info(f"key_function col_hide")
2390
- d = {'!': 0, '@': 1, '#': 2, '$': 3, '%': 4, '^': 5, '&': 6, '*': 7, '(': 8, ')': 9}
2391
- d = {s:i for i,s in enumerate(")!@#$%^&*(")}
2392
- col_index = d[chr(key)]
2393
- self.toggle_column_visibility(col_index)
2394
- elif self.check_key("copy", key, self.keys_dict):
2395
- self.copy_dialogue()
2396
- elif self.check_key("paste", key, self.keys_dict):
2397
- self.paste_dialogue()
2398
- elif self.check_key("save", key, self.keys_dict):
2399
- self.save_dialog()
2400
- elif self.check_key("load", key, self.keys_dict):
2401
- self.load_dialog()
2402
-
2403
- elif self.check_key("delete", key, self.keys_dict): # Delete key
2404
- self.delete_entries()
2405
-
2406
- elif self.check_key("delete_column", key, self.keys_dict): # Delete key
2407
- self.logger.info(f"key_function delete_column")
2408
- row_len = 1
2409
- if self.header: row_len = len(self.header)
2410
- elif len(self.items): row_len = len(self.items[0])
2411
- if row_len > 1:
2412
- self.items = [row[:self.selected_column] + row[self.selected_column+1:] for row in self.items]
2413
- self.header = self.header[:self.selected_column] + self.header[self.selected_column+1:]
2414
- self.editable_columns = self.editable_columns[:self.selected_column] + self.editable_columns[self.selected_column+1:]
2415
- self.selected_column = min(self.selected_column, row_len-2)
2416
- elif row_len == 1:
2417
- self.items = [[""] for _ in range(len(self.items))]
2418
- self.header = [""] if self.header else []
2419
- self.editable_columns = []
2420
- self.selected_column = min(self.selected_column, row_len-2)
2421
- self.initialise_variables()
2422
-
2423
-
2424
-
2425
-
2426
- # elif self.check_key("increase_lines_per_page", key, self.keys_dict):
2427
- # self.items_per_page += 1
2428
- # self.draw_screen(self.indexed_items, self.highlights)
2429
- # elif self.check_key("decrease_lines_per_page", key, self.keys_dict):
2430
- # if self.items_per_page > 1:
2431
- # self.items_per_page -= 1
2432
- # self.draw_screen(self.indexed_items, self.highlights)
2433
- elif self.check_key("decrease_column_width", key, self.keys_dict):
2434
- self.logger.info(f"key_function decrease_column_width")
2435
- if self.max_column_width > 10:
2436
- self.max_column_width -= 10
2437
- self.column_widths[:] = get_column_widths(self.items, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2438
- self.draw_screen(self.indexed_items, self.highlights)
2439
- elif self.check_key("increase_column_width", key, self.keys_dict):
2440
- self.logger.info(f"key_function increase_column_width")
2441
- if self.max_column_width < 1000:
2442
- self.max_column_width += 10
2443
- self.column_widths = get_column_widths(self.items, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2444
- self.draw_screen(self.indexed_items, self.highlights)
2445
- elif self.check_key("visual_selection_toggle", key, self.keys_dict):
2446
- self.logger.info(f"key_function visual_selection_toggle")
2447
- self.handle_visual_selection()
2448
- self.draw_screen(self.indexed_items, self.highlights)
2449
-
2450
- elif self.check_key("visual_deselection_toggle", key, self.keys_dict):
2451
- self.logger.info(f"key_function visual_deselection_toggle")
2452
- self.handle_visual_selection(selecting=False)
2453
- self.draw_screen(self.indexed_items, self.highlights)
2454
-
2455
- elif key == curses.KEY_RESIZE: # Terminal resize signal
2456
-
2457
- self.calculate_section_sizes()
2458
- self.column_widths = get_column_widths(self.items, header=self.header, max_column_width=self.max_column_width, number_columns=self.number_columns)
2459
-
2460
- self.draw_screen(self.indexed_items, self.highlights)
2461
-
2462
-
2463
- elif self.check_key("filter_input", key, self.keys_dict):
2464
- self.logger.info(f"key_function filter_input")
2465
- self.draw_screen(self.indexed_items, self.highlights)
2466
- usrtxt = f"{self.filter_query} " if self.filter_query else ""
2467
- field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2468
- if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2469
- else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2470
- self.set_registers()
2471
- words = self.get_word_list()
2472
- usrtxt, return_val = input_field(
2473
- self.stdscr,
2474
- usrtxt=usrtxt,
2475
- field_prefix=" Filter: ",
2476
- x=lambda:2,
2477
- y=lambda: self.stdscr.getmaxyx()[0]-2,
2478
- # max_length=field_end,
2479
- max_length=field_end_f,
2480
- registers=self.registers,
2481
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2482
- history=self.history_filter_and_search,
2483
- path_auto_complete=True,
2484
- formula_auto_complete=False,
2485
- function_auto_complete=False,
2486
- word_auto_complete=True,
2487
- auto_complete_words=words,
2488
- )
2489
- if return_val:
2490
- self.filter_query = usrtxt
2491
- self.history_filter_and_search.append(usrtxt)
2492
-
2493
- # If the current mode filter has been changed then go back to the first mode
2494
- if self.modes and "filter" in self.modes[self.mode_index] and self.modes[self.mode_index]["filter"] not in self.filter_query:
2495
- self.mode_index = 0
2496
- # elif "filter" in modes[mode_index] and modes[mode_index]["filter"] in filter_query:
2497
- # filter_query.split(modes[mode_index]["filter"])
2498
-
2499
- prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2500
- self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2501
- 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)
2502
- else: new_index = 0
2503
- self.cursor_pos = new_index
2504
- # Re-sort self.items after applying filter
2505
- if self.columns_sort_method[self.selected_column] != 0:
2506
- 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
2507
-
2508
- elif self.check_key("search_input", key, self.keys_dict):
2509
- self.logger.info(f"key_function search_input")
2510
- self.draw_screen(self.indexed_items, self.highlights)
2511
- usrtxt = f"{self.search_query} " if self.search_query else ""
2512
- field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2513
- if self.show_footer and self.footer.height >= 3: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2514
- else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2515
- self.set_registers()
2516
- words = self.get_word_list()
2517
- usrtxt, return_val = input_field(
2518
- self.stdscr,
2519
- usrtxt=usrtxt,
2520
- field_prefix=" Search: ",
2521
- x=lambda:2,
2522
- y=lambda: self.stdscr.getmaxyx()[0]-3,
2523
- max_length=field_end_f,
2524
- registers=self.registers,
2525
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2526
- history=self.history_filter_and_search,
2527
- path_auto_complete=True,
2528
- formula_auto_complete=False,
2529
- function_auto_complete=False,
2530
- word_auto_complete=True,
2531
- auto_complete_words=words,
2532
- )
2533
- if return_val:
2534
- self.search_query = usrtxt
2535
- self.history_filter_and_search.append(usrtxt)
2536
- return_val, tmp_cursor, tmp_index, tmp_count, tmp_highlights = search(
2537
- query=self.search_query,
2538
- indexed_items=self.indexed_items,
2539
- highlights=self.highlights,
2540
- cursor_pos=self.cursor_pos,
2541
- unselectable_indices=self.unselectable_indices,
2542
- )
2543
- if return_val:
2544
- self.cursor_pos, self.search_index, self.search_count, self.highlights = tmp_cursor, tmp_index, tmp_count, tmp_highlights
2545
- else:
2546
- self.search_index, self.search_count = 0, 0
2547
-
2548
- elif self.check_key("continue_search_forward", key, self.keys_dict):
2549
- self.logger.info(f"key_function continue_search_forward")
2550
- return_val, tmp_cursor, tmp_index, tmp_count, tmp_highlights = search(
2551
- query=self.search_query,
2552
- indexed_items=self.indexed_items,
2553
- highlights=self.highlights,
2554
- cursor_pos=self.cursor_pos,
2555
- unselectable_indices=self.unselectable_indices,
2556
- continue_search=True,
2557
- )
2558
- if return_val:
2559
- self.cursor_pos, self.search_index, self.search_count, self.highlights = tmp_cursor, tmp_index, tmp_count, tmp_highlights
2560
- elif self.check_key("continue_search_backward", key, self.keys_dict):
2561
- self.logger.info(f"key_function continue_search_backward")
2562
- return_val, tmp_cursor, tmp_index, tmp_count, tmp_highlights = search(
2563
- query=self.search_query,
2564
- indexed_items=self.indexed_items,
2565
- highlights=self.highlights,
2566
- cursor_pos=self.cursor_pos,
2567
- unselectable_indices=self.unselectable_indices,
2568
- continue_search=True,
2569
- reverse=True,
2570
- )
2571
- if return_val:
2572
- self.cursor_pos, self.search_index, self.search_count, self.highlights = tmp_cursor, tmp_index, tmp_count, tmp_highlights
2573
- elif self.check_key("cancel", key, self.keys_dict): # ESC key
2574
- # order of escapes:
2575
- # 1. selecting/deslecting
2576
- # 2. search
2577
- # 3. filter
2578
- # 4. if self.cancel_is_back (e.g., notification) then we exit
2579
- # 4. selecting
2580
-
2581
- # Cancel visual de/selection
2582
- if self.is_selecting or self.is_deselecting:
2583
- self.start_selection = -1
2584
- self.end_selection = -1
2585
- self.is_selecting = False
2586
- self.is_deselecting = False
2587
- # Cancel search
2588
- elif self.search_query:
2589
- self.search_query = ""
2590
- self.highlights = [highlight for highlight in self.highlights if "type" not in highlight or highlight["type"] != "search" ]
2591
- # Remove filter
2592
- elif self.filter_query:
2593
- 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"]:
2594
- self.filter_query = self.modes[self.mode_index]["filter"]
2595
- # elif "filter" in modes[mode_index]:
2596
- else:
2597
- self.filter_query = ""
2598
- self.mode_index = 0
2599
- prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2600
- self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2601
- 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)
2602
- else: new_index = 0
2603
- self.cursor_pos = new_index
2604
- # Re-sort self.items after applying filter
2605
- if self.columns_sort_method[self.selected_column] != 0:
2606
- 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
2607
- elif self.cancel_is_back:
2608
- function_data = self.get_function_data()
2609
- function_data["last_key"] = key
2610
- return [], "escape", function_data
2611
-
2612
-
2613
- # else:
2614
- # self.search_query = ""
2615
- # self.mode_index = 0
2616
- # self.highlights = [highlight for highlight in self.highlights if "type" not in highlight or highlight["type"] != "search" ]
2617
- # continue
2618
- self.draw_screen(self.indexed_items, self.highlights)
2619
-
2620
- elif self.check_key("opts_input", key, self.keys_dict):
2621
- self.logger.info(f"key_function opts_input")
2622
- usrtxt = f"{self.user_opts} " if self.user_opts else ""
2623
- field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2624
- if self.show_footer and self.footer.height >= 1: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2625
- else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2626
- self.set_registers()
2627
- words = self.get_word_list()
2628
- usrtxt, return_val = input_field(
2629
- self.stdscr,
2630
- usrtxt=usrtxt,
2631
- field_prefix=" Opts: ",
2632
- x=lambda:2,
2633
- y=lambda: self.stdscr.getmaxyx()[0]-1,
2634
- max_length=field_end_f,
2635
- registers=self.registers,
2636
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2637
- history=self.history_opts,
2638
- path_auto_complete=True,
2639
- formula_auto_complete=False,
2640
- function_auto_complete=True,
2641
- word_auto_complete=True,
2642
- auto_complete_words=words,
2643
- )
2644
- if return_val:
2645
- self.user_opts = usrtxt
2646
- self.history_opts.append(usrtxt)
2647
- elif self.check_key("opts_select", key, self.keys_dict):
2648
- self.logger.info(f"key_function opts_select")
2649
- s, o, f = self.choose_option(self.stdscr, self.options_list)
2650
- if self.user_opts.strip(): self.user_opts += " "
2651
- self.user_opts += " ".join([x for x in s.values()])
2652
- elif self.check_key("notification_toggle", key, self.keys_dict):
2653
- self.logger.info(f"key_function notification_toggle")
2654
- self.notification(self.stdscr, colours_end=self.colours_end)
2655
-
2656
- elif self.check_key("mode_next", key, self.keys_dict): # tab key
2657
- self.logger.info(f"key_function mode_next")
2658
- # apply setting
2659
- prev_mode_index = self.mode_index
2660
- self.mode_index = (self.mode_index+1)%len(self.modes)
2661
- mode = self.modes[self.mode_index]
2662
- for key, val in mode.items():
2663
- if key == 'filter':
2664
- if 'filter' in self.modes[prev_mode_index]:
2665
- self.filter_query = self.filter_query.replace(self.modes[prev_mode_index]['filter'], '')
2666
- self.filter_query = f"{self.filter_query.strip()} {val.strip()}".strip()
2667
- prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2668
-
2669
- self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2670
- 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)
2671
- else: new_index = 0
2672
- self.cursor_pos = new_index
2673
- # Re-sort self.items after applying filter
2674
- 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
2675
- elif self.check_key("mode_prev", key, self.keys_dict): # shift+tab key
2676
- self.logger.info(f"key_function mode_prev")
2677
- # apply setting
2678
- prev_mode_index = self.mode_index
2679
- self.mode_index = (self.mode_index-1)%len(self.modes)
2680
- mode = self.modes[self.mode_index]
2681
- for key, val in mode.items():
2682
- if key == 'filter':
2683
- if 'filter' in self.modes[prev_mode_index]:
2684
- self.filter_query = self.filter_query.replace(self.modes[prev_mode_index]['filter'], '')
2685
- self.filter_query = f"{self.filter_query.strip()} {val.strip()}".strip()
2686
- prev_index = self.indexed_items[self.cursor_pos][0] if len(self.indexed_items)>0 else 0
2687
- self.indexed_items = filter_items(self.items, self.indexed_items, self.filter_query)
2688
- 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)
2689
- else: new_index = 0
2690
- self.cursor_pos = new_index
2691
- # Re-sort self.items after applying filter
2692
- 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
2693
- elif self.check_key("pipe_input", key, self.keys_dict):
2694
- self.logger.info(f"key_function pipe_input")
2695
- # usrtxt = "xargs -d '\n' -I{} "
2696
- usrtxt = "xargs "
2697
- field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2698
- if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2699
- else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2700
- self.set_registers()
2701
-
2702
- # Get list of available shell commands
2703
- try:
2704
- # result = subprocess.run(['compgen', '-c'], capture_output=True, text=True, check=True)
2705
- # shell_commands = result.stdout.splitlines()
2706
- result = subprocess.run(['ls', '/usr/bin'], capture_output=True, text=True, check=True)
2707
- shell_commands = result.stdout.splitlines()
2708
- except:
2709
- shell_commands = []
2710
- usrtxt, return_val = input_field(
2711
- self.stdscr,
2712
- usrtxt=usrtxt,
2713
- field_prefix=" Command: ",
2714
- x=lambda:2,
2715
- y=lambda: self.stdscr.getmaxyx()[0]-2,
2716
- literal=True,
2717
- max_length=field_end_f,
2718
- registers=self.registers,
2719
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2720
- history=self.history_pipes,
2721
- path_auto_complete=True,
2722
- formula_auto_complete=False,
2723
- function_auto_complete=False,
2724
- word_auto_complete=True,
2725
- auto_complete_words=shell_commands,
2726
- )
2727
-
2728
- if return_val:
2729
- selected_indices = get_selected_indices(self.selections)
2730
- self.history_pipes.append(usrtxt)
2731
- if not selected_indices:
2732
- selected_indices = [self.indexed_items[self.cursor_pos][0]]
2733
-
2734
- full_values = [format_row_full(self.items[i], self.hidden_columns) for i in selected_indices] # Use format_row_full for full data
2735
- full_values = [self.items[i][self.selected_column] for i in selected_indices]
2736
- if full_values:
2737
- command = usrtxt.split()
2738
- # command = ['xargs', '-d' , '"\n"' '-I', '{}', 'mpv', '{}']
2739
- # command = ['xargs', '-d' , '"\n"' '-I', '{}', 'mpv', '{}']
2740
- # command = "xargs -d '\n' -I{} mpv {}"
2741
-
2742
- try:
2743
- process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
2744
-
2745
- if process.stdin != None:
2746
- for value in full_values:
2747
- process.stdin.write((repr(value) + '\n').encode())
2748
-
2749
- process.stdin.close()
2750
-
2751
- self.notification(self.stdscr, message=f"{len(full_values)} strings piped to {repr(usrtxt)}")
2752
- except Exception as e:
2753
- self.notification(self.stdscr, message=f"{e}")
2754
-
2755
-
2756
- elif self.check_key("open", key, self.keys_dict):
2757
- self.logger.info(f"key_function open")
2758
- selected_indices = get_selected_indices(self.selections)
2759
- if not selected_indices:
2760
- selected_indices = [self.indexed_items[self.cursor_pos][0]]
2761
-
2762
- file_names = [self.items[i][self.selected_column] for i in selected_indices]
2763
- response = openFiles(file_names)
2764
- if response:
2765
- self.notification(self.stdscr, message=response)
2766
-
2767
-
2768
- elif self.check_key("reset_opts", key, self.keys_dict):
2769
- self.logger.info(f"key_function reset_opts")
2770
- self.user_opts = ""
2771
-
2772
- elif self.check_key("edit", key, self.keys_dict):
2773
- self.logger.info(f"key_function edit")
2774
- if len(self.indexed_items) > 0 and self.selected_column >=0 and self.editable_columns[self.selected_column]:
2775
- current_val = self.indexed_items[self.cursor_pos][1][self.selected_column]
2776
- usrtxt = f"{current_val}"
2777
- field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2778
- if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2779
- else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2780
- self.set_registers()
2781
- words = self.get_word_list()
2782
- usrtxt, return_val = input_field(
2783
- self.stdscr,
2784
- usrtxt=usrtxt,
2785
- field_prefix=" Edit value: ",
2786
- x=lambda:2,
2787
- y=lambda: self.stdscr.getmaxyx()[0]-2,
2788
- max_length=field_end_f,
2789
- registers=self.registers,
2790
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2791
- history = self.history_edits,
2792
- path_auto_complete=True,
2793
- formula_auto_complete=True,
2794
- function_auto_complete=True,
2795
- word_auto_complete=True,
2796
- auto_complete_words=words,
2797
- )
2798
- if return_val:
2799
- if usrtxt.startswith("```"):
2800
- usrtxt = str(eval(usrtxt[3:]))
2801
- self.indexed_items[self.cursor_pos][1][self.selected_column] = usrtxt
2802
- self.history_edits.append(usrtxt)
2803
-
2804
- elif self.check_key("edit_picker", key, self.keys_dict):
2805
- self.logger.info(f"key_function edit_picker")
2806
- if len(self.indexed_items) > 0 and self.selected_column >=0 and self.editable_columns[self.selected_column]:
2807
- current_val = self.indexed_items[self.cursor_pos][1][self.selected_column]
2808
- usrtxt = f"{current_val}"
2809
- field_end_f = lambda: self.stdscr.getmaxyx()[1]-38 if self.show_footer else lambda: self.stdscr.getmaxyx()[1]-3
2810
- if self.show_footer and self.footer.height >= 2: field_end_f = lambda: self.stdscr.getmaxyx()[1]-38
2811
- else: field_end_f = lambda: self.stdscr.getmaxyx()[1]-3
2812
- self.set_registers()
2813
- words = self.get_word_list()
2814
- usrtxt, return_val = input_field(
2815
- self.stdscr,
2816
- usrtxt=usrtxt,
2817
- field_prefix=" Edit value: ",
2818
- x=lambda:2,
2819
- y=lambda: self.stdscr.getmaxyx()[0]-2,
2820
- max_length=field_end_f,
2821
- registers=self.registers,
2822
- refresh_screen_function=lambda: self.draw_screen(self.indexed_items, self.highlights),
2823
- history = self.history_edits,
2824
- path_auto_complete=True,
2825
- formula_auto_complete=True,
2826
- function_auto_complete=True,
2827
- word_auto_complete=True,
2828
- auto_complete_words=words,
2829
- )
2830
- if return_val:
2831
- self.indexed_items[self.cursor_pos][1][self.selected_column] = usrtxt
2832
- self.history_edits.append(usrtxt)
2833
- elif self.check_key("edit_ipython", key, self.keys_dict):
2834
- self.logger.info(f"key_function edit_picker")
2835
- import IPython
2836
- self.stdscr.clear()
2837
- restrict_curses(self.stdscr)
2838
- self.stdscr.clear()
2839
- os.system('cls' if os.name == 'nt' else 'clear')
2840
- globals()['self'] = self # make the instance available in IPython namespace
2841
-
2842
- from traitlets.config import Config
2843
- c = Config()
2844
- # Doesn't work; Config only works with start_ipython, not embed... but start_ipython causes errors
2845
- # c.InteractiveShellApp.exec_lines = [
2846
- # '%clear'
2847
- # ]
2848
- msg = "The active Picker object has variable name self.\n"
2849
- msg += "\te.g., self.items will display the items in Picker"
2850
- IPython.embed(header=msg, config=c)
2851
-
2852
- unrestrict_curses(self.stdscr)
2853
-
2854
- self.stdscr.clear()
2855
- self.stdscr.refresh()
2856
- self.initialise_variables()
2857
- self.draw_screen(self.indexed_items, self.highlights)
2858
-
2859
-
2860
-
2861
- self.draw_screen(self.indexed_items, self.highlights, clear=clear_screen)
2862
-
2863
-
2864
-
2865
- def set_colours(pick: int = 0, start: int = 0) -> Optional[int]:
2866
- """ Initialise curses colour pairs from dictionary. """
2867
-
2868
-
2869
- global COLOURS_SET, notification_colours, help_colours
2870
- if COLOURS_SET: return None
2871
- if start == None: start = 0
2872
-
2873
-
2874
- if curses.COLORS >= 255:
2875
- colours = get_colours(pick)
2876
- notification_colours = get_notification_colours(pick)
2877
- help_colours = get_help_colours(pick)
2878
- standard_colours_start, notification_colours_start, help_colours_start = 0, 50, 100
2879
- else:
2880
- colours = get_fallback_colours()
2881
- notification_colours = get_fallback_colours()
2882
- help_colours = get_fallback_colours()
2883
- standard_colours_start, help_colours_start, notification_colours_start = 0, 0, 0
2884
-
2885
- if not colours: return 0
2886
-
2887
- try:
2888
- start = standard_colours_start
2889
- curses.init_pair(start+1, colours['selected_fg'], colours['selected_bg'])
2890
- curses.init_pair(start+2, colours['unselected_fg'], colours['unselected_bg'])
2891
- curses.init_pair(start+3, colours['normal_fg'], colours['background'])
2892
- curses.init_pair(start+4, colours['header_fg'], colours['header_bg'])
2893
- curses.init_pair(start+5, colours['cursor_fg'], colours['cursor_bg'])
2894
- curses.init_pair(start+6, colours['normal_fg'], colours['background'])
2895
- curses.init_pair(start+7, colours['error_fg'], colours['error_bg'])
2896
- curses.init_pair(start+8, colours['complete_fg'], colours['complete_bg'])
2897
- curses.init_pair(start+9, colours['active_fg'], colours['active_bg'])
2898
- curses.init_pair(start+10, colours['search_fg'], colours['search_bg'])
2899
- curses.init_pair(start+11, colours['waiting_fg'], colours['waiting_bg'])
2900
- curses.init_pair(start+12, colours['paused_fg'], colours['paused_bg'])
2901
- curses.init_pair(start+13, colours['active_input_fg'], colours['active_input_bg'])
2902
- curses.init_pair(start+14, colours['modes_selected_fg'], colours['modes_selected_bg'])
2903
- curses.init_pair(start+15, colours['modes_unselected_fg'], colours['modes_unselected_bg'])
2904
- curses.init_pair(start+16, colours['title_fg'], colours['title_bg'])
2905
- curses.init_pair(start+17, colours['normal_fg'], colours['title_bar'])
2906
- curses.init_pair(start+18, colours['normal_fg'], colours['scroll_bar_bg'])
2907
- curses.init_pair(start+19, colours['selected_header_column_fg'], colours['selected_header_column_bg'])
2908
- curses.init_pair(start+20, colours['footer_fg'], colours['footer_bg'])
2909
- curses.init_pair(start+21, colours['refreshing_fg'], colours['refreshing_bg'])
2910
- curses.init_pair(start+22, colours['40pc_fg'], colours['40pc_bg'])
2911
- curses.init_pair(start+23, colours['refreshing_inactive_fg'], colours['refreshing_inactive_bg'])
2912
- curses.init_pair(start+24, colours['footer_string_fg'], colours['footer_string_bg'])
2913
- curses.init_pair(start+25, colours['selected_cell_fg'], colours['selected_cell_bg'])
2914
- curses.init_pair(start+26, colours['deselecting_cell_fg'], colours['deselecting_cell_bg'])
2915
-
2916
-
2917
- # notifications 50, infobox 100, help 150
2918
- # Notification colours
2919
- colours = notification_colours
2920
- start = notification_colours_start
2921
- curses.init_pair(start+1, colours['selected_fg'], colours['selected_bg'])
2922
- curses.init_pair(start+2, colours['unselected_fg'], colours['unselected_bg'])
2923
- curses.init_pair(start+3, colours['normal_fg'], colours['background'])
2924
- curses.init_pair(start+4, colours['header_fg'], colours['header_bg'])
2925
- curses.init_pair(start+5, colours['cursor_fg'], colours['cursor_bg'])
2926
- curses.init_pair(start+6, colours['normal_fg'], colours['background'])
2927
- curses.init_pair(start+7, colours['error_fg'], colours['error_bg'])
2928
- curses.init_pair(start+8, colours['complete_fg'], colours['complete_bg'])
2929
- curses.init_pair(start+9, colours['active_fg'], colours['active_bg'])
2930
- curses.init_pair(start+10, colours['search_fg'], colours['search_bg'])
2931
- curses.init_pair(start+11, colours['waiting_fg'], colours['waiting_bg'])
2932
- curses.init_pair(start+12, colours['paused_fg'], colours['paused_bg'])
2933
- curses.init_pair(start+13, colours['active_input_fg'], colours['active_input_bg'])
2934
- curses.init_pair(start+14, colours['modes_selected_fg'], colours['modes_selected_bg'])
2935
- curses.init_pair(start+15, colours['modes_unselected_fg'], colours['modes_unselected_bg'])
2936
- curses.init_pair(start+16, colours['title_fg'], colours['title_bg'])
2937
- curses.init_pair(start+17, colours['normal_fg'], colours['title_bar'])
2938
- curses.init_pair(start+18, colours['normal_fg'], colours['scroll_bar_bg'])
2939
- curses.init_pair(start+19, colours['selected_header_column_fg'], colours['selected_header_column_bg'])
2940
- curses.init_pair(start+20, colours['footer_fg'], colours['footer_bg'])
2941
- curses.init_pair(start+21, colours['refreshing_fg'], colours['refreshing_bg'])
2942
- curses.init_pair(start+22, colours['40pc_fg'], colours['40pc_bg'])
2943
- curses.init_pair(start+23, colours['refreshing_inactive_fg'], colours['refreshing_inactive_bg'])
2944
- curses.init_pair(start+24, colours['footer_string_fg'], colours['footer_string_bg'])
2945
- curses.init_pair(start+25, colours['selected_cell_fg'], colours['selected_cell_bg'])
2946
- curses.init_pair(start+26, colours['deselecting_cell_fg'], colours['deselecting_cell_bg'])
2947
-
2948
- # Help
2949
- colours = help_colours
2950
- start = help_colours_start
2951
- curses.init_pair(start+1, colours['selected_fg'], colours['selected_bg'])
2952
- curses.init_pair(start+2, colours['unselected_fg'], colours['unselected_bg'])
2953
- curses.init_pair(start+3, colours['normal_fg'], colours['background'])
2954
- curses.init_pair(start+4, colours['header_fg'], colours['header_bg'])
2955
- curses.init_pair(start+5, colours['cursor_fg'], colours['cursor_bg'])
2956
- curses.init_pair(start+6, colours['normal_fg'], colours['background'])
2957
- curses.init_pair(start+7, colours['error_fg'], colours['error_bg'])
2958
- curses.init_pair(start+8, colours['complete_fg'], colours['complete_bg'])
2959
- curses.init_pair(start+9, colours['active_fg'], colours['active_bg'])
2960
- curses.init_pair(start+10, colours['search_fg'], colours['search_bg'])
2961
- curses.init_pair(start+11, colours['waiting_fg'], colours['waiting_bg'])
2962
- curses.init_pair(start+12, colours['paused_fg'], colours['paused_bg'])
2963
- curses.init_pair(start+13, colours['active_input_fg'], colours['active_input_bg'])
2964
- curses.init_pair(start+14, colours['modes_selected_fg'], colours['modes_selected_bg'])
2965
- curses.init_pair(start+15, colours['modes_unselected_fg'], colours['modes_unselected_bg'])
2966
- curses.init_pair(start+16, colours['title_fg'], colours['title_bg'])
2967
- curses.init_pair(start+17, colours['normal_fg'], colours['title_bar'])
2968
- curses.init_pair(start+18, colours['normal_fg'], colours['scroll_bar_bg'])
2969
- curses.init_pair(start+19, colours['selected_header_column_fg'], colours['selected_header_column_bg'])
2970
- curses.init_pair(start+20, colours['footer_fg'], colours['footer_bg'])
2971
- curses.init_pair(start+21, colours['refreshing_fg'], colours['refreshing_bg'])
2972
- curses.init_pair(start+22, colours['40pc_fg'], colours['40pc_bg'])
2973
- curses.init_pair(start+23, colours['refreshing_inactive_fg'], colours['refreshing_inactive_bg'])
2974
- curses.init_pair(start+24, colours['footer_string_fg'], colours['footer_string_bg'])
2975
- curses.init_pair(start+25, colours['selected_cell_fg'], colours['selected_cell_bg'])
2976
- curses.init_pair(start+26, colours['deselecting_cell_fg'], colours['deselecting_cell_bg'])
2977
- except:
2978
- pass
2979
- COLOURS_SET = True
2980
- return start+21
2981
-
2982
- def parse_arguments() -> Tuple[argparse.Namespace, dict]:
2983
- """ Parse command line arguments. """
2984
- parser = argparse.ArgumentParser(description='Convert table to list of lists.')
2985
- parser.add_argument('filename', type=str, help='The file to process')
2986
- parser.add_argument('-i', dest='file', help='File containing the table to be converted.')
2987
- parser.add_argument('--load', '-l', dest='load', type=str, help='Load file from Picker dump.')
2988
- parser.add_argument('--stdin', dest='stdin', action='store_true', help='Table passed on stdin')
2989
- parser.add_argument('--stdin2', action='store_true', help='Table passed on stdin')
2990
- parser.add_argument('--generate', '-g', type=str, help='Pass file to generate data for listpick Picker.')
2991
- parser.add_argument('-d', dest='delimiter', default='\t', help='Delimiter for rows in the table (default: tab)')
2992
- parser.add_argument('-t', dest='file_type', choices=['tsv', 'csv', 'json', 'xlsx', 'ods', 'pkl'], help='Type of file (tsv, csv, json, xlsx, ods)')
2993
- parser.add_argument('--debug', action="store_true", help="Enable debug log.")
2994
- parser.add_argument('--debug-verbose', action="store_true", help="Enable debug verbose log.")
2995
- args = parser.parse_args()
2996
-
2997
-
2998
- function_data = {
2999
- "items" : [],
3000
- "header": [],
3001
- "unselectable_indices" : [],
3002
- "colours": get_colours(0),
3003
- "top_gap": 0,
3004
- "max_column_width": 70,
3005
- }
3006
-
3007
- if args.file:
3008
- input_arg = args.file
3009
- elif args.stdin:
3010
- input_arg = '--stdin'
3011
- elif args.stdin2:
3012
- input_arg = '--stdin2'
3013
- elif args.filename:
3014
- input_arg = args.filename
3015
-
3016
- elif args.generate:
3017
- function_data["refresh_function"] = lambda : generate_picker_data(args.generate)
3018
- function_data["get_data_startup"] = True
3019
- function_data["get_new_data"] = True
3020
- return args, function_data
3021
- elif args.load:
3022
- function_data = load_state(args.load)
3023
- function_data["refresh_function"] = lambda : (load_state(args.load)["items"], load_state(args.load)["header"])
3024
- function_data["get_new_data"] = True
3025
- return args, function_data
3026
-
3027
- else:
3028
- # print("Error: Please provide input file or use --stdin flag.")
3029
- print("No data provided. Loading empty Picker.")
3030
- return args, function_data
3031
- # sys.exit(1)
3032
- if args.debug:
3033
- function_data["debug"] = True
3034
- function_data["debug_level"] = 1
3035
-
3036
- if args.debug_verbose:
3037
- function_data["debug"] = True
3038
- function_data["debug_level"] = 0
3039
-
3040
- if not args.file_type:
3041
- filetype = guess_file_type(input_arg)
3042
- else:
3043
- filetype = args.file_type
3044
-
3045
-
3046
- items, header = table_to_list(input_arg, args.delimiter, filetype)
3047
- function_data["items"] = items
3048
- if header: function_data["header"] = header
3049
- return args, function_data
3050
-
3051
- def start_curses() -> curses.window:
3052
- """ Initialise curses and return curses window. """
3053
- stdscr = curses.initscr()
3054
- curses.start_color()
3055
- curses.use_default_colors() # For terminal theme-recolouring
3056
- curses.noecho() # Turn off automatic echoing of keys to the screen
3057
- curses.cbreak() # Interpret keystrokes immediately (without requiring Enter)
3058
- stdscr.keypad(True) # Ensures that arrow and function keys are received as one key by getch
3059
- curses.raw() # Disable control keys (ctrl-c, ctrl-s, ctrl-q, etc.)
3060
- curses.curs_set(False)
3061
-
3062
- return stdscr
3063
-
3064
- def close_curses(stdscr: curses.window) -> None:
3065
- """ Close curses. """
3066
- stdscr.keypad(False)
3067
- curses.nocbreak()
3068
- curses.noraw()
3069
- curses.echo()
3070
- curses.endwin()
3071
-
3072
- def restrict_curses(stdscr: curses.window) -> None:
3073
- """ Restrict curses for normal input. Used when dropping to ipython. """
3074
- stdscr.keypad(False)
3075
- curses.nocbreak()
3076
- curses.noraw()
3077
- curses.curs_set(True)
3078
- curses.echo()
3079
-
3080
- def unrestrict_curses(stdscr: curses.window) -> None:
3081
- """ Unrestrict curses for terminal input. Used after dropping to ipython. """
3082
- curses.noecho() # Turn off automatic echoing of keys to the screen
3083
- curses.cbreak() # Interpret keystrokes immediately (without requiring Enter)
3084
- stdscr.keypad(True)
3085
- curses.raw() # Disable control keys (ctrl-c, ctrl-s, ctrl-q, etc.)
3086
- curses.curs_set(False)
3087
-
3088
- def main() -> None:
3089
- """ Main function when listpick is executed. Deals with command line arguments and starts a Picker. """
3090
- args, function_data = parse_arguments()
3091
-
3092
- try:
3093
- if function_data["items"] == []:
3094
- function_data["items"] = test_items
3095
- function_data["highlights"] = test_highlights
3096
- function_data["header"] = test_header
3097
- except:
3098
- pass
3099
-
3100
- function_data["colour_theme_number"] = 3
3101
- # function_data["modes"] = [
3102
- # {
3103
- # 'filter': '',
3104
- # 'sort': 0,
3105
- # 'name': 'All',
3106
- # },
3107
- # {
3108
- # 'filter': '--2 miss',
3109
- # 'name': 'miss',
3110
- # },
3111
- # {
3112
- # 'filter': '--2 mp4',
3113
- # 'name': 'mp4',
3114
- # },
3115
- # ]
3116
- function_data["cell_cursor"] = True
3117
- function_data["display_modes"] = True
3118
- function_data["centre_in_cols"] = True
3119
- function_data["show_row_header"] = True
3120
- function_data["keys_dict"] = picker_keys
3121
- function_data["id_column"] = -1
3122
- function_data["track_entries_upon_refresh"] = True
3123
- function_data["centre_in_terminal_vertical"] = True
3124
- function_data["highlight_full_row"] = True
3125
- # function_data["debug"] = True
3126
- # function_data["debug_level"] = 1
3127
- stdscr = start_curses()
3128
- try:
3129
- # Run the Picker
3130
- # h, w = stdscr.getmaxyx()
3131
- # if (h>8 and w >20):
3132
- # curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
3133
- # stdscr.bkgd(' ', curses.color_pair(1)) # Apply background color
3134
- # s = "Listpick is loading your data..."
3135
- # stdscr.addstr(h//2, (w-len(s))//2, s)
3136
- # stdscr.refresh()
3137
-
3138
- # app = Picker(stdscr, **function_data)
3139
- app = Picker(stdscr)
3140
- app.set_function_data(function_data)
3141
- app.splash_screen("Listpick is loading your data...")
3142
- app.load_input_history("~/.config/listpick/cmdhist.json")
3143
- app.run()
3144
-
3145
- app.save_input_history("~/.config/listpick/cmdhist.json")
3146
- except Exception as e:
3147
- print(e)
3148
-
3149
- # Clean up
3150
- close_curses(stdscr)
3151
-
3152
- if __name__ == '__main__':
3153
- main()