|
| 1 | +import html |
| 2 | +import re |
| 3 | +import threading |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +import sublime |
| 7 | +import sublime_plugin |
| 8 | + |
| 9 | +from ..activity_indicator import ActivityIndicator |
| 10 | +from ..console_write import console_write |
| 11 | +from ..package_manager import PackageManager |
| 12 | +from ..show_error import show_message |
| 13 | + |
| 14 | +USE_QUICK_PANEL_ITEM = hasattr(sublime, "QuickPanelItem") |
| 15 | + |
| 16 | + |
| 17 | +class ListAvailableLibrariesCommand(sublime_plugin.ApplicationCommand): |
| 18 | + |
| 19 | + """ |
| 20 | + A command that presents the list of available packages and allows the |
| 21 | + user to pick one to install. |
| 22 | + """ |
| 23 | + |
| 24 | + def run(self): |
| 25 | + def show_quick_panel(): |
| 26 | + manager = PackageManager() |
| 27 | + |
| 28 | + with ActivityIndicator("Loading libraries...") as progress: |
| 29 | + libraries = manager.list_available_libraries() |
| 30 | + if not libraries: |
| 31 | + message = "There are no libraries available for installation" |
| 32 | + console_write(message) |
| 33 | + progress.finish(message) |
| 34 | + show_message( |
| 35 | + """ |
| 36 | + %s |
| 37 | +
|
| 38 | + Please see https://packagecontrol.io/docs/troubleshooting for help |
| 39 | + """, |
| 40 | + message, |
| 41 | + ) |
| 42 | + return |
| 43 | + |
| 44 | + if USE_QUICK_PANEL_ITEM: |
| 45 | + self.show_quick_panel_st4(libraries.values()) |
| 46 | + else: |
| 47 | + self.show_quick_panel_st3(libraries.values()) |
| 48 | + |
| 49 | + threading.Thread(target=show_quick_panel).start() |
| 50 | + |
| 51 | + def show_quick_panel_st3(self, libraries): |
| 52 | + items = [ |
| 53 | + [info["name"] + " v" + info["releases"][0]["version"], info["description"]] |
| 54 | + for info in libraries |
| 55 | + ] |
| 56 | + |
| 57 | + def on_done(picked): |
| 58 | + if picked > -1: |
| 59 | + sublime.set_clipboard(items[picked][0].split(" ", 1)[0]) |
| 60 | + |
| 61 | + sublime.active_window().show_quick_panel( |
| 62 | + items, on_done, sublime.KEEP_OPEN_ON_FOCUS_LOST |
| 63 | + ) |
| 64 | + |
| 65 | + def show_quick_panel_st4(self, libraries): |
| 66 | + # TODO: display supported python versions |
| 67 | + |
| 68 | + items = [] |
| 69 | + for info in libraries: |
| 70 | + display_name = info["name"] + " v" + info["releases"][0]["version"] |
| 71 | + |
| 72 | + details = [html.escape(info["description"])] |
| 73 | + |
| 74 | + issues = html.escape(info["issues"]) |
| 75 | + issues_display = re.sub(r"^https?://", "", issues) |
| 76 | + if issues_display: |
| 77 | + details.append( |
| 78 | + 'report bug: <a href="{}">{}</a>'.format(issues, issues_display) |
| 79 | + ) |
| 80 | + |
| 81 | + try: |
| 82 | + date = info["releases"][0]["date"].split(" ", 1)[0] |
| 83 | + annotation = datetime.strptime(date, "%Y-%m-%d").strftime( |
| 84 | + "Updated on %a %b %d, %Y" |
| 85 | + ) |
| 86 | + except (IndexError, KeyError, ValueError): |
| 87 | + annotation = "" |
| 88 | + |
| 89 | + items.append(sublime.QuickPanelItem(display_name, details, annotation)) |
| 90 | + |
| 91 | + def on_done(picked): |
| 92 | + if picked > -1: |
| 93 | + sublime.set_clipboard(items[picked].trigger.split(" ", 1)[0]) |
| 94 | + |
| 95 | + sublime.active_window().show_quick_panel( |
| 96 | + items, on_done, sublime.KEEP_OPEN_ON_FOCUS_LOST |
| 97 | + ) |
0 commit comments