def on_event(self, event, extension): arg = event.get_argument() items = [] if arg is None: items = self.__help() else: try: results = locator.run(arg) alt_action = ExtensionCustomAction(results, True) for file in results: items.append( ExtensionSmallResultItem(icon='images/ok.png', name=str(file, 'utf-8'), on_enter=OpenAction(file), on_alt_enter=alt_action)) except Exception as e: error_info = str(e) items = [ ExtensionSmallResultItem( icon='images/error.png', name=error_info, on_enter=CopyToClipboardAction(error_info)) ] return RenderResultListAction(items)
def on_event(self, event, extension): """ Handles the event """ data = event.get_data() item = extension.lp.get_item(data["id"]) if item["is_note"]: return RenderResultListAction([ ExtensionSmallResultItem( icon='images/icon.png', name='Copy note to clipboard', highlightable=False, on_enter=CopyToClipboardAction(item["note"]), ) ]) return RenderResultListAction([ ExtensionSmallResultItem( icon='images/icon.png', name='Copy username to clipboard for %s' % item["name"], highlightable=False, on_enter=CopyToClipboardAction(item["username"]), ), ExtensionSmallResultItem( icon='images/icon.png', name='Copy password to clipboard for %s' % item["name"], highlightable=False, on_enter=CopyToClipboardAction(item["password"]), ) ])
def show_applications(self, query): """ shows the list of available applications known by the extension """ apps = self.shortcuts_service.get_applications(query) if not apps: return RenderResultListAction([ ExtensionSmallResultItem( icon='images/icon.png', name='No applications found matching your criteria', highlightable=False, on_enter=HideWindowAction()) ]) items = [] for app in apps[:15]: items.append( ExtensionSmallResultItem(icon=app['icon'], name=app['name'], on_enter=ExtensionCustomAction({ "action": "show", "app": app, }), on_alt_enter=OpenUrlAction( app['reference_url']))) return RenderResultListAction(items)
def on_event(self, event, extension): """ Handles the event """ items = [] try: boards = extension.trello_client.get_boards(event.get_argument()) if not boards: return [ ExtensionResultItem(icon='images/icon.png', name='No boards found with name %s' % event.get_argument(), on_enter=HideWindowAction()) ] for board in boards[:25]: items.append( ExtensionSmallResultItem( icon='images/icon.png', name=board['name'], description=board['description'], on_enter=RunScriptAction(board['script']) if bool( board['script']) else OpenUrlAction(board['url']))) except TrelloApiException as e: # pylint: disable=invalid-name LOGGER.error(e) items.append( ExtensionResultItem( icon='images/icon.png', name= 'Error when connecting to Trello API ( status code : %s )' % e.get_status_code(), on_enter=HideWindowAction())) return RenderResultListAction(items)
def create_note_item(note, idx_item): """ Create item linking to a database note :param note: :param idx_item: :return: """ # HACK: Using note from Notes or FTS table if 'id' in note: note_id = note['id'] elif 'uid' in note: note_id = note['uid'] else: raise Exception return ExtensionSmallResultItem( icon='images/joplin.png', name=note['title'], # description=note['body'], on_enter=ExtensionCustomAction( { 'func': open_note_edition_action, 'uid': note_id }, keep_app_open=True), on_alt_enter=ExtensionCustomAction( { 'func': imfeelinglucky_action, 'uid': note_id }, keep_app_open=True), )
def create_default_items_list(history_uids, do_history_clean=True): items = list() # Find notes for ids in history notes = pyjoplin.get_notes_by_id(history_uids[::-1], ordered=True) if do_history_clean: # Remove any not found history uids found_uids = [note['id'] for note in notes] notfound_uids = set(history_uids) - set(found_uids) for notfound_uid in notfound_uids: # NOTE: Use that argument is passed by reference to fix in place history_uids.remove(notfound_uid) # Add entries from recent history for note in notes: idx_item = len(items) item = create_note_item(note, idx_item) items.append(item) # Create last entry with instructions items.append( ExtensionSmallResultItem( icon='images/search.png', name='Or write search query ended with space...')) return items
def on_event(self, event, extension): """ Handles the event """ items = [] query = event.get_argument() if query is None: tabs = extension.tabs else: tabs = [ t for t in extension.tabs if query.lower() in t['name'].lower() ] if not tabs: items.append( ExtensionResultItem( icon='images/icon.png', name='No tabs found', description= 'Make sure your ulauncher-tabs-helper chrome extension is installing and running', on_enter=HideWindowAction())) else: for tab in tabs[:25]: items.append( ExtensionSmallResultItem(icon='images/icon.png', name=tab['name'].encode("utf-8"), on_enter=ExtensionCustomAction( tab['id']))) return RenderResultListAction(items)
def more_results_available_item(cnt): return ExtensionSmallResultItem( icon=EMPTY_ICON, name="...{} more results available, please refine the search query...". format(cnt), on_enter=DoNothingAction(), )
def formatEntry(icon, query, entry): entryArr = entry.strip().split('\n') context = [] pos = 0 if query: line = next(l for l in entryArr if query in l.lower()) pos = entryArr.index(line) if pos > 0: line = entryArr[pos - 1].strip() if line: context.append('...' + line) context.append(entryArr[pos]) if len(entryArr) > pos + 1: line = entryArr[pos + 1].strip() if line: context.append(line + '...') return ExtensionSmallResultItem( icon = icon, name = '\n'.join(context), on_enter = ExtensionCustomAction(entry) )
def search_results(keyword: str, arg: str, entries: List[str], max_items: int) -> BaseAction: """ Build list of result items `max_items` long """ items = [] if not entries: items.append(NO_SEARCH_RESULTS_ITEM) else: for entry in entries[:max_items]: # FUTURE replace with call_object_method action = ExtensionCustomAction( { "action": "activate_entry", "entry": entry, "keyword": keyword, "prev_query_arg": arg, }, keep_app_open=True, ) items.append( ExtensionSmallResultItem(icon="images/key.svg", name=entry, on_enter=action)) if len(entries) > max_items: items.append(item_more_results_available(len(entries) - max_items)) return RenderResultListAction(items)
def search_tabs(self, event): """ Search tabs """ if not self.brotab_client.is_installed(): return RenderResultListAction([ ExtensionResultItem( icon="images/icon.png", name="Brotab is not installed on your system", description="Press enter to open install instructions.", highlightable=False, on_enter=OpenUrlAction("https://github.com/balta2ar/brotab#installation"), ) ]) items = [] tabs = self.brotab_client.search_tabs(event.get_argument()) for tab in tabs[:DISPLAY_MAX_RESULTS]: data = {"tab": tab["prefix"], "mode": self.mode} items.append( ExtensionSmallResultItem( icon="images/%s" % tab["icon"], name=tab["name"], description=tab["url"], on_enter=ExtensionCustomAction(data), on_alt_enter=CopyToClipboardAction(tab["url"]), )) if not items: return self.show_no_results_message() return RenderResultListAction(items)
def generate_results(self, event): for (pid, cpu, cmd) in get_process_list(): name = '[%s%% CPU] %s' % (cpu, cmd) if cpu > 1 else cmd on_enter = {'alt_enter': False, 'pid': pid, 'cmd': cmd} on_alt_enter = on_enter.copy() on_alt_enter['alt_enter'] = True if event.get_argument(): if event.get_argument() in cmd: yield ExtensionSmallResultItem(icon=exec_icon, name=name, on_enter=ExtensionCustomAction(on_enter), on_alt_enter=ExtensionCustomAction(on_alt_enter, keep_app_open=True)) else: yield ExtensionSmallResultItem(icon=exec_icon, name=name, on_enter=ExtensionCustomAction(on_enter), on_alt_enter=ExtensionCustomAction(on_alt_enter, keep_app_open=True))
def generate_results(self, extension, pattern, locate_flags, open_script, terminal_emulator, dirs_only): for (f) in get_file_list(extension, pattern, locate_flags, dirs_only): file_path = '%s' % os.path.abspath(f) file_dir = '%s' % os.path.abspath(os.path.join(file_path, os.pardir)) yield ExtensionSmallResultItem( icon=get_icon(f), name=file_path, on_enter=RunScriptAction(' '.join([open_script, file_path])), on_alt_enter=RunScriptAction(' '.join([terminal_emulator, '--working-directory', file_dir])))
def on_event(self, event, extension): results = event.get_data() items = [] for file in results: items.append( ExtensionSmallResultItem(icon='images/copy.png', name=file, on_enter=CopyToClipboardAction(file))) return RenderResultListAction(items)
def item_more_results_available(cnt: int) -> ResultItem: """ Item showing how many more results are available """ return ExtensionSmallResultItem( icon="images/empty.png", name="...{} more results available, please refine the search query...". format(cnt), on_enter=DoNothingAction(), )
def refresh_shortcuts(self): """ Refreshes the shortcuts """ return RenderResultListAction([ ExtensionSmallResultItem( icon='images/icon.png', name='Refresh Shortcuts', description='Press enter to refresh shortcuts', highlightable=False, on_enter=ExtensionCustomAction({"action": "refresh"})) ])
def on_event(self, event, extension): """ Handles the event """ fd_cmd = extension.preferences["fd_cmd"] if shutil.which(fd_cmd) is None: return RenderResultListAction([ ExtensionResultItem( icon="images/icon.png", name= f"Command {fd_cmd} is not found, please install it first", on_enter=HideWindowAction()) ]) query = event.get_argument() if not query or len(query) < 3: return RenderResultListAction([ ExtensionResultItem( icon="images/icon.png", name="Keep typing your search criteria ...", on_enter=DoNothingAction()) ]) keyword = event.get_keyword() keyword_id = None # Find the keyword id using the keyword (since the keyword can be changed by users) for kw_id, kw in list(extension.preferences.items()): if kw == keyword: keyword_id = kw_id file_type = FILE_SEARCH_ALL if keyword_id == "ff_kw": file_type = FILE_SEARCH_FILE elif keyword_id == "fd_kw": file_type = FILE_SEARCH_DIRECTORY results = extension.search(query.strip(), file_type, fd_cmd) if not results: return RenderResultListAction([ ExtensionResultItem(icon="images/icon.png", name=f"No Results found matching {query}", on_enter=HideWindowAction()) ]) items = [] for result in results[:15]: items.append( ExtensionSmallResultItem( icon=result["icon"], name=result["path"].decode("utf-8"), on_enter=OpenAction(result["path"].decode("utf-8")), on_alt_enter=extension.get_open_in_terminal_script( result["path"].decode("utf-8")))) return RenderResultListAction(items)
def show_signal_options(self, data): result_items = [] options = [('TERM', '15 TERM (default)'), ('KILL', '9 KILL'), ('HUP', '1 HUP')] for sig, name in options: on_enter = data.copy() on_enter['alt_enter'] = False on_enter['signal'] = sig result_items.append(ExtensionSmallResultItem(icon=ext_icon, name=name, highlightable=False, on_enter=ExtensionCustomAction(on_enter))) return RenderResultListAction(result_items)
def __help(self): all_opt = opts.get_all() items = [] for i in range(len(all_opt)): hint_str = 'locate ' + all_opt[i] query_str = 's r ' + all_opt[i] + ' ' items.append( ExtensionSmallResultItem( icon='images/info.png', name=hint_str, on_enter=SetUserQueryAction(query_str))) return items
def on_event(self, event, extension): """ Handles the event """ data = event.get_data() item = extension.lp.get_item(data["id"]) if re.search("NoteType:Server", item["note"]): hostname = re.search("(?<=Hostname:).+?(?=\n)", item["note"]).group(0) username = re.search("(?<=Username:).+?(?=\n)", item["note"]).group(0) password = re.search("(?<=Password:).+?(?=\n)", item["note"]).group(0) return RenderResultListAction([ ExtensionSmallResultItem( icon='images/icon.png', name='Copy hostname to clipboard for %s' % item["name"], highlightable=False, on_enter=CopyToClipboardAction(hostname), ), ExtensionSmallResultItem( icon='images/icon.png', name='Copy username to clipboard for %s' % item["name"], highlightable=False, on_enter=CopyToClipboardAction(username), ), ExtensionSmallResultItem( icon='images/icon.png', name='Copy password to clipboard for %s' % item["name"], highlightable=False, on_enter=CopyToClipboardAction(password), ) ]) if item["is_note"]: return RenderResultListAction([ ExtensionSmallResultItem( icon='images/icon.png', name='Copy note to clipboard', highlightable=False, on_enter=CopyToClipboardAction(item["note"]), ) ]) return RenderResultListAction([ ExtensionSmallResultItem( icon='images/icon.png', name='Copy username to clipboard for %s' % item["name"], highlightable=False, on_enter=CopyToClipboardAction(item["username"]), ), ExtensionSmallResultItem( icon='images/icon.png', name='Copy password to clipboard for %s' % item["name"], highlightable=False, on_enter=CopyToClipboardAction(item["password"]), ) ])
def error_item(message: str, details: Optional[str] = None) -> ResultItem: """ Show small result item with error icon and a message. """ if not details: return ExtensionSmallResultItem(icon="images/error.svg", name=message, on_enter=DoNothingAction()) return ExtensionResultItem( icon="images/error.svg", name=message, description=details, on_enter=DoNothingAction(), )
def on_event(self, event, extension): """ Handles the event """ items = [] query = event.get_argument() if not query or len(query) < 3: return RenderResultListAction([ ExtensionResultItem( icon='images/icon.png', name='Keep typing your search criteria ...', on_enter=DoNothingAction()) ]) keyword = event.get_keyword() # Find the keyword id using the keyword (since the keyword can be changed by users) for kw_id, kw in list(extension.preferences.items()): if kw == keyword: keyword_id = kw_id file_type = FILE_SEARCH_ALL if keyword_id == "ff_kw": file_type = FILE_SEARCH_FILE elif keyword_id == "fd_kw": file_type = FILE_SEARCH_DIRECTORY results = extension.search(query.strip(), file_type) if not results: return RenderResultListAction([ ExtensionResultItem(icon='images/icon.png', name='No Results found matching %s' % query, on_enter=HideWindowAction()) ]) items = [] for result in results[:15]: items.append( ExtensionSmallResultItem( icon=result['icon'], name=result['path'].decode("utf-8"), on_enter=OpenAction(result['path'].decode("utf-8")), on_alt_enter=extension.get_open_in_terminal_script( result['path'].decode("utf-8")))) return RenderResultListAction(items)
def render_search_results(self, keyword, entries, extension): max_items = extension.get_max_result_items() items = [] if not entries: items.append(NO_SEARCH_RESULTS_ITEM) else: for e in entries[:max_items]: action = ExtensionCustomAction( {"action": "activate_entry", "entry": e, "keyword": keyword}, keep_app_open=True, ) items.append( ExtensionSmallResultItem(icon=ITEM_ICON, name=e, on_enter=action) ) if len(entries) > max_items: items.append(more_results_available_item(len(entries) - max_items)) return RenderResultListAction(items)
def render_results(self, query): items = SortedList(query, min_score=0, limit=9) try: for buddy in pidgin.getBuddies(): items.append( ExtensionSmallResultItem( icon='images/icon.png', name='%s' % buddy.alias, description='Account desc: %s' % buddy.name, on_enter=ExtensionCustomAction(buddy))) res = [] for i in items: res.append(i) return RenderResultListAction(res) except dbus.exceptions.DBusException: return self._pidgin_not_launched()
def show_active_entry(self, entry): items = [] details = self.keepassxc_db.get_entry_details(entry) attrs = [ ("Password", "password"), ("UserName", "username"), ("URL", "URL"), ("Notes", "notes"), ] for attr, attr_nice in attrs: val = details.get(attr, "") if val: action = ActionList( [ ExtensionCustomAction( { "action": "show_notification", "summary": "{} copied to the clipboard.".format( attr_nice.capitalize() ), } ), CopyToClipboardAction(val), ] ) if attr == "Password": items.append( ExtensionSmallResultItem( icon=COPY_ICON, name="Copy password to the clipboard", on_enter=action, ) ) else: items.append( ExtensionResultItem( icon=COPY_ICON, name="{}: {}".format(attr_nice.capitalize(), val), description="Copy {} to the clipboard".format(attr_nice), on_enter=action, ) ) return RenderResultListAction(items)
def active_entry(details: Dict[str, str]) -> BaseAction: """ Show details of an entry and allow various items to be copied to the clipboard """ attrs = [ ("Password", "password"), ("UserName", "username"), ("URL", "URL"), ("Notes", "notes"), ] items = [] for attr, attr_nice in attrs: val = details.get(attr, "") if val: action = ActionList([ # FUTURE replace with call_object_method ExtensionCustomAction({ "action": "show_notification", "summary": "{} copied to the clipboard.".format( attr_nice.capitalize()), }), CopyToClipboardAction(val), ]) if attr == "Password": items.append( ExtensionSmallResultItem( icon="images/copy.svg", name="Copy password to the clipboard", on_enter=action, )) else: items.append( ExtensionResultItem( icon="images/copy.svg", name="{}: {}".format(attr_nice.capitalize(), val), description="Copy {} to the clipboard".format( attr_nice), on_enter=action, )) return RenderResultListAction(items)
def on_event(self, event, extension): """ Handles the event """ items = [] query = event.get_argument() if not query or len(query) < 3: return RenderResultListAction([ ExtensionResultItem( icon='images/icon.png', name='Keep typing your search criteria ...', on_enter=DoNothingAction()) ]) keyword = event.get_keyword() file_type = FILE_SEARCH_ALL if keyword == "ff": file_type = FILE_SEARCH_FILE elif keyword == "fdir": file_type = FILE_SEARCH_DIRECTORY results = extension.search(query.strip(), file_type) if not results: return RenderResultListAction([ ExtensionResultItem(icon='images/icon.png', name='No Results found matching %s' % query, on_enter=HideWindowAction()) ]) items = [] for result in results[:15]: items.append( ExtensionSmallResultItem( icon=result['icon'], name=result['path'], on_enter=OpenAction(result['path']), on_alt_enter=extension.get_open_in_terminal_script( result['path']))) return RenderResultListAction(items)
def search_tabs(self, event): """ Search tabs """ items = [] tabs = self.brotab_client.search_tabs(event.get_argument()) for tab in tabs[:DISPLAY_MAX_RESULTS]: data = {"action": RESULT_ITEM_ENTER, 'tab': tab['prefix']} items.append( ExtensionSmallResultItem(icon='images/%s' % tab["icon"], name=tab["name"], description=tab['url'], on_enter=ExtensionCustomAction(data), on_alt_enter=CopyToClipboardAction( tab["url"]))) if not items: return self.show_no_results_message() return RenderResultListAction(items)
def on_event(self, event, extension): items = [] for i in range(5): # items.append(ExtensionResultItem(icon='images/icon.png', # name='Item %s' % i, # description='Item description %s' % i, # on_enter=HideWindowAction())) on_enter_data = {'new_name': 'Item %s was clicked' % i} on_alt_enter_data = {'new_name': 'Item %s was alt-entered' % i} items.append( ExtensionSmallResultItem( icon='images/icon.png', name='Item %s' % i, description='Item description %s' % i, on_enter=ExtensionCustomAction(on_enter_data, keep_app_open=True), on_alt_enter=ExtensionCustomAction(on_alt_enter_data, keep_app_open=True))) return RenderResultListAction(items)
def create_search_item(search_str): """ Create item linking to a new search action :param search_str: :return: """ return ExtensionSmallResultItem( icon='images/note-chrome-add-64.png', name='New search note: %s' % search_str, on_enter=ExtensionCustomAction( { 'func': open_new_note_with_websearch_action, 'str_search': search_str, }, keep_app_open=True), on_alt_enter=ExtensionCustomAction( { 'func': open_new_note_without_websearch_action, 'str_search': search_str, }, keep_app_open=True), )