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