Best Python code snippet using elementium_python
pick.py
Source:pick.py
1# -*-coding:utf-8-*-2import curses3__all__ = ['Picker', 'pick']4KEYS_ENTER = (curses.KEY_ENTER, ord ('\n'), ord ('\r'))5KEYS_UP = (curses.KEY_UP, ord ('k'))6KEYS_DOWN = (curses.KEY_DOWN, ord ('j'))7KEYS_SELECT = (curses.KEY_RIGHT, ord (' '))8class Picker (object):9 """The :class:`Picker <Picker>` object10 :param options: a list of options to choose from11 :param title: (optional) a title above options list12 :param multi_select: (optional) if true its possible to select multiple values by hitting SPACE, defaults to False13 :param indicator: (optional) custom the selection indicator14 :param default_index: (optional) set this if the default selected option is not the first one15 """16 def __init__(self, options, title=None, indicator='*', default_index=0, multi_select=False, min_selection_count=0):17 if len (options) == 0:18 raise ValueError ('options should not be an empty list')19 self.options = options20 self.title = title21 self.indicator = indicator22 self.multi_select = multi_select23 self.min_selection_count = min_selection_count24 self.all_selected = []25 if default_index >= len (options):26 raise ValueError ('default_index should be less than the length of options')27 if multi_select and min_selection_count > len (options):28 raise ValueError (29 'min_selection_count is bigger than the available options, you will not be able to make any selection')30 self.index = default_index31 self.custom_handlers = {}32 def register_custom_handler(self, key, func):33 self.custom_handlers[key] = func34 def move_up(self):35 self.index -= 136 if self.index < 0:37 self.index = len (self.options) - 138 def move_down(self):39 self.index += 140 if self.index >= len (self.options):41 self.index = 042 def mark_index(self):43 if self.multi_select:44 if self.index in self.all_selected:45 self.all_selected.remove (self.index)46 else:47 self.all_selected.append (self.index)48 def get_selected(self):49 """return the current selected option as a tuple: (option, index)50 or as a list of tuples (in case multi_select==True)51 """52 if self.multi_select:53 return_tuples = []54 for selected in self.all_selected:55 return_tuples.append ((self.options[selected], selected))56 return return_tuples57 else:58 return self.options[self.index], self.index59 def get_title_lines(self):60 if self.title:61 return self.title.split ('\n') + ['']62 return []63 def get_option_lines(self):64 lines = []65 for index, option in enumerate (self.options):66 if index == self.index:67 prefix = self.indicator68 else:69 prefix = len (self.indicator) * ' '70 if self.multi_select and index in self.all_selected:71 format = curses.color_pair (1)72 line = ('{0} {1}'.format (prefix, option), format)73 else:74 line = '{0} {1}'.format (prefix, option)75 lines.append (line)76 return lines77 def get_lines(self):78 title_lines = self.get_title_lines ( )79 option_lines = self.get_option_lines ( )80 lines = title_lines + option_lines81 current_line = self.index + len (title_lines) + 182 return lines, current_line83 def draw(self):84 """draw the curses ui on the screen, handle scroll if needed"""85 self.screen.clear ( )86 x, y = 1, 1 # start point87 max_y, max_x = self.screen.getmaxyx ( )88 max_rows = max_y - y # the max rows we can draw89 lines, current_line = self.get_lines ( )90 # calculate how many lines we should scroll, relative to the top91 scroll_top = getattr (self, 'scroll_top', 0)92 if current_line <= scroll_top:93 scroll_top = 094 elif current_line - scroll_top > max_rows:95 scroll_top = current_line - max_rows96 self.scroll_top = scroll_top97 lines_to_draw = lines[scroll_top:scroll_top + max_rows]98 for line in lines_to_draw:99 if type (line) is tuple:100 self.screen.addnstr (y, x, line[0], max_x - 2, line[1])101 else:102 self.screen.addnstr (y, x, line, max_x - 2)103 y += 1104 self.screen.refresh ( )105 def run_loop(self):106 while True:107 self.draw ( )108 c = self.screen.getch ( )109 if c in KEYS_UP:110 self.move_up ( )111 elif c in KEYS_DOWN:112 self.move_down ( )113 elif c in KEYS_ENTER:114 if self.multi_select and len (self.all_selected) < self.min_selection_count:115 continue116 return self.get_selected ( )117 elif c in KEYS_SELECT and self.multi_select:118 self.mark_index ( )119 elif c in self.custom_handlers:120 ret = self.custom_handlers[c] (self)121 if ret:122 return ret123 def config_curses(self):124 # use the default colors of the terminal125 curses.use_default_colors ( )126 # hide the cursor127 curses.curs_set (0)128 # add some color for multi_select129 # @todo make colors configurable130 curses.init_pair (1, curses.COLOR_GREEN, curses.COLOR_WHITE)131 def _start(self, screen):132 self.screen = screen133 self.config_curses ( )134 return self.run_loop ( )135 def start(self):136 return curses.wrapper (self._start)137def pick(options, title=None, indicator='*', default_index=0, multi_select=False, min_selection_count=0):138 """Construct and start a :class:`Picker <Picker>`.139 Usage::140 >>> from pick import pick141 >>> title = 'Please choose an option: '142 >>> options = ['option1', 'option2', 'option3']143 >>> option, index = pick(options, title)144 """145 picker = Picker (options, title, indicator, default_index, multi_select, min_selection_count)...
Picker.py
Source:Picker.py
1# -*-coding:utf-8-*-2"""3Class defining list picker using curses4Original repo - https://github.com/wong2/pick5Deleted multiselect, simplified and reformatted code6Formatted title in bold7"""8import curses9KEYS_UP = (curses.KEY_UP, ord("k"))10KEYS_DOWN = (curses.KEY_DOWN, ord("j"))11KEYS_SELECT = (curses.KEY_RIGHT, ord(" "), curses.KEY_ENTER, ord("\n"), ord("\r"))12class Picker(object):13 def __init__(self, title: str, options: list[str], indicator="->") -> None:14 if len(options) == 0:15 raise ValueError("Options must not be empty")16 self.title = title17 self.options = options18 self.indicator = indicator19 self.index = 020 def move_up(self) -> None:21 self.index = max(self.index - 1, 0)22 def move_down(self) -> None:23 self.index = min(self.index + 1, len(self.options) - 1)24 def get_selected(self) -> tuple[str, int]:25 return self.options[self.index], self.index26 def get_title_lines(self) -> list[str]:27 if self.title:28 return self.title.split("\n") + [""]29 return []30 def get_options_lines(self) -> list[str]:31 lines = []32 for index, option in enumerate(self.options):33 if index == self.index:34 prefix = self.indicator35 else:36 prefix = len(self.indicator) * " "37 line = prefix + " " + option38 lines.append(line)39 return lines40 def get_lines(self) -> tuple[list[str], int]:41 title_lines = self.get_title_lines()42 options_lines = self.get_options_lines()43 lines = title_lines + options_lines44 current_line = len(title_lines) + 1 + self.index45 return lines, current_line46 def draw(self) -> None:47 self.screen.clear()48 x, y = 1, 149 max_y, max_x = self.screen.getmaxyx()50 max_rows = max_y - y51 lines, cur_line = self.get_lines()52 scroll_top = getattr(self, "scroll_top", 0)53 if cur_line <= scroll_top:54 scroll_top = 055 elif cur_line - scroll_top > max_rows:56 scroll_top = cur_line - max_rows57 self.scroll_top = scroll_top58 lines_to_draw = lines[scroll_top : scroll_top + max_rows]59 title_is_bold_flag = False60 if scroll_top == 0:61 title_is_bold_flag = False62 else:63 title_is_bold_flag = True64 for line in lines_to_draw:65 if type(line) is tuple:66 self.screen.addnstr(y, x, line[0], max_x - 2, line[1])67 else:68 if not title_is_bold_flag:69 self.screen.addnstr(y, x, line, max_x - 2, curses.A_BOLD)70 title_is_bold_flag = True71 else:72 self.screen.addnstr(y, x, line, max_x - 2)73 y += 174 self.screen.refresh()75 def run_loop(self):76 while True:77 self.draw()78 c = self.screen.getch()79 if c in KEYS_UP:80 self.move_up()81 elif c in KEYS_DOWN:82 self.move_down()83 elif c in KEYS_SELECT:84 return self.get_selected()85 def _start(self, screen):86 self.screen = screen87 curses.initscr()88 curses.curs_set(0)89 return self.run_loop()90 def start(self):...
base.py
Source:base.py
...32 """33 thumb_char = '\u2588'34 trough_char = '\u2502'35 @property36 def scroll_top(self):37 return getattr(self, '_scroll_top', 0)38 @scroll_top.setter39 def scroll_top(self, value):40 """41 The current scroll position from the top of the widget.42 This many lines will be trimmed from the top of the widget.43 """44 self._scroll_top = max(value, 0)45 self._invalidate() # invalidate render cache46 def render(self, size, focus):47 maxcols, maxrows = size48 rows = self._original_widget.rows((maxcols,), focus)49 if rows <= maxrows:50 # no clipping, so just fill as normal51 return super().render(size, focus)52 # limit scroll_top to top of last page53 self.scroll_top = min(self.scroll_top, rows - maxrows)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!