是否可以在Sublime侧边栏中的文件名旁边显示文件大小



我运行一个应用程序,该应用程序在特定文件夹中生成和更新许多文件。当应用程序运行时,我通过崇高的侧边栏观察文件夹的内容。因为我有兴趣在应用程序运行时查看每个文件的当前大小,所以我有一个打开的终端 (Mac),我在其中使用以下命令获取文件夹的实时状态。

watch -d ls -al -h folderName

想知道我是否可以直接从崇高那里获得这些信息。

所以我的问题是:是否可以将每个文件的大小放在崇高的侧边栏中的文件名旁边?如果是,如何?

由于侧边栏不在官方 API 中,我认为这是不可能的,或者至少这并不容易。

但是,将信息转换为崇高的文本很容易。您可以使用视图将其存档。只需执行 ls 命令并将结果写入视图中即可。

为此,我编写了一个小型(ST3)插件:

import subprocess
import sublime
import sublime_plugin
# change to whatever command you want to execute
commands = ["ls", "-a", "-s", "-1", "-h"]
# the update interval
TIMEOUT = 2000  # ms

def watch_folder(view, watch_command):
    """create a closure to watch a folder and update the view content"""
    window = view.window()
    def watch():
        # stop if the view is not longer open
        open_views = [v.id() for v in window.views()]
        if view.id() not in open_views:
            print("closed")
            return
        # execute the command and read the output
        output = subprocess.check_output(watch_command).decode()
        # replace the view content with the output
        view.set_read_only(False)
        view.run_command("select_all")
        view.run_command("insert", {"characters": output})
        view.set_read_only(True)
        # call this function again after the interval
        sublime.set_timeout(watch, TIMEOUT)
    return watch

class WatchFolderCommand(sublime_plugin.WindowCommand):
    def run(self):
        folders = self.window.folders()
        if not folders:
            sublime.error_message("You don't have opened any folders")
            return
        folder = folders[0]  # get the first folder
        watch_command = commands + [folder]
        # create a view and set the desired properties
        view = self.window.new_file()
        view.set_name("Watch files")
        view.set_scratch(True)
        view.set_read_only(True)
        view.settings().set("auto_indent", False)
        # create and call the watch closure
        watch_folder(view, watch_command)()

只需打开 User 文件夹(或包的任何其他子文件夹),创建一个 python 文件(例如 watch_folder.py ) 并粘贴源代码。

您可以通过将以下内容粘贴到键盘映射来将其绑定到键绑定:

{
    "keys": ["ctrl+alt+shift+w"],
    "command": "watch_folder",
},

最新更新