diff --git a/Default.sublime-commands b/Default.sublime-commands index dedcc7cf..d783b133 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -43,6 +43,10 @@ "caption": "Package Control: List Packages", "command": "list_packages" }, + { + "caption": "Package Control: List Packages on View", + "command": "list_packages_on_view" + }, { "caption": "Package Control: Remove Channel", "command": "remove_channel" diff --git a/package_control/commands/__init__.py b/package_control/commands/__init__.py index 05272b75..b24d2419 100644 --- a/package_control/commands/__init__.py +++ b/package_control/commands/__init__.py @@ -8,6 +8,7 @@ from .install_local_dependency_command import InstallLocalDependencyCommand from .install_package_command import InstallPackageCommand from .list_packages_command import ListPackagesCommand +from .list_packages_command import ListPackagesOnViewCommand from .list_unmanaged_packages_command import ListUnmanagedPackagesCommand from .remove_package_command import RemovePackageCommand from .upgrade_all_packages_command import UpgradeAllPackagesCommand @@ -30,6 +31,7 @@ 'InstallLocalDependencyCommand', 'InstallPackageCommand', 'ListPackagesCommand', + 'ListPackagesOnViewCommand', 'ListUnmanagedPackagesCommand', 'RemovePackageCommand', 'UpgradeAllPackagesCommand', diff --git a/package_control/commands/list_packages_command.py b/package_control/commands/list_packages_command.py index 21b04247..61e5b587 100644 --- a/package_control/commands/list_packages_command.py +++ b/package_control/commands/list_packages_command.py @@ -1,3 +1,4 @@ +import textwrap import threading import os @@ -10,6 +11,8 @@ from .existing_packages_command import ExistingPackagesCommand +# print( "Reloading `Package Control\package_control\commands\list_packages_command.py`" ) + class ListPackagesCommand(sublime_plugin.WindowCommand): """ @@ -20,13 +23,23 @@ def run(self): ListPackagesThread(self.window).start() +class ListPackagesOnViewCommand(sublime_plugin.WindowCommand): + + """ + A command that shows a list of all installed packages in a new view + """ + + def run(self): + ListPackagesThread(self.window, on_view=True).start() + + class ListPackagesThread(threading.Thread, ExistingPackagesCommand): """ A thread to prevent the listing of existing packages from freezing the UI """ - def __init__(self, window, filter_function=None): + def __init__(self, window, filter_function=None, on_view=False): """ :param window: An instance of :class:`sublime.Window` that represents the Sublime @@ -44,6 +57,7 @@ def __init__(self, window, filter_function=None): """ self.window = window + self.on_view = on_view self.filter_function = filter_function self.manager = PackageManager() threading.Thread.__init__(self) @@ -53,18 +67,57 @@ def run(self): if self.filter_function: self.package_list = list(filter(self.filter_function, self.package_list)) + def show_no_packages(): + sublime.message_dialog(text.format( + u''' + Package Control + + There are no packages to list + ''' + )) + def show_panel(): if not self.package_list: - sublime.message_dialog(text.format( - u''' - Package Control - - There are no packages to list - ''' - )) + show_no_packages() return show_quick_panel(self.window, self.package_list, self.on_done) - sublime.set_timeout(show_panel, 10) + + def show_view(): + if not self.package_list: + show_no_packages() + return + + new_view = sublime.active_window().new_file() + package_count = 0 + prefix_indent = " " + package_string_list = [] + + new_view.set_scratch(True) + new_view.set_name("Packages List") + new_view.set_syntax_file("Packages/Text/Plain text.tmLanguage") + new_view.settings().set('tab_size', 8) + + for package in self.package_list: + package_count += 1; + wrapper = textwrap.TextWrapper(initial_indent=prefix_indent, width=80, subsequent_indent=prefix_indent) + + # Efficient String Concatenation in Python - https://waymoot.org/home/python_string/ + package_string_list.append( "%3d: <%s>\n" % ( package_count, package[0] ) ) + package_string_list.append( wrapper.fill(package[1]) + "\n" + prefix_indent + "[" + package[2] + "]\n\n" ) + + # https://forum.sublimetext.com/t/how-to-insert-text-on-view-with-no-indentation/28496 + new_view.run_command("append", {"characters": "Packages list within %d entries:\n\n" % (len(self.package_list))}) + new_view.run_command("append", {"characters": "".join( package_string_list )}) + + new_view.sel().clear() + initial_region = sublime.Region(0,0) + new_view.sel().add(initial_region) + new_view.show_at_center(initial_region) + + if self.on_view: + sublime.set_timeout(show_view, 10) + else: + sublime.set_timeout(show_panel, 10) def on_done(self, picked): """ @@ -95,3 +148,4 @@ def open_dir(): self.window.run_command('open_dir', open_dir_file) sublime.set_timeout(open_dir, 10) +