Search code examples
sublimetext2sublimetext3sublimetextsublime-text-plugin

Is it possible to show the size of files next to the file names inside Sublime sidebar?


I run an application that generates and updates a number of files in a specific folder. While the application runs, I observe the content of the folder through the sublime sidebar. Because I am interested to see the current size of each file while the application runs, I have an open terminal (Mac) where I use the following command to get the live state of the folder.

watch -d ls -al -h folderName

I was wondering if I can obtain this information directly from sublime.

So my question is: Is it possible to have the size of each file next to the file-names in the sublime sidebar? And if yes, how?


Solution

  • Since the sidebar is not in the official API, I don't think this is possible or at least it is not easy.

    However getting the information into sublime text is easy. You can archive this by using a view. Just execute the ls command and write the result in the view.

    I wrote a small (ST3) plugin for this purpose:

    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)()
    

    Just open the User folder (or any other sub-folder of Packages), create a python file (e.g. watch_folder.py) and paste the source code.

    You can bind it to a keybinding by pasting the following to your keymap:

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