【问题标题】:Is it possible to show the size of files next to the file names inside Sublime sidebar?是否可以在 Sublime 侧边栏中的文件名旁边显示文件的大小?
【发布时间】:2016-02-12 01:14:52
【问题描述】:

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

watch -d ls -al -h folderName

我想知道我是否可以直接从 sublime 获得这些信息。

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

【问题讨论】:

  • 使用SideBarEnhancements 可以在选择文件时在状态栏中显示文件 大小。但是我不知道显示文件夹大小。

标签: sublimetext2 sublimetext3 sublimetext sublime-text-plugin


【解决方案1】:

由于侧边栏不在官方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 文件夹(或Packages 的任何其他子文件夹),创建一个python 文件(例如watch_folder.py)并粘贴源代码。

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

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

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2012-08-13
    • 1970-01-01
    • 2012-11-26
    • 2013-08-19
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多