3

我運行的應用程序可以生成並更新特定文件夾中的多個文件。在應用程序運行時,我通過sublime側邊欄觀察文件夾的內容。因爲我有興趣在應用程序運行時查看每個文件的當前大小,所以我有一個打開的終端(Mac),我使用以下命令獲取文件夾的實時狀態。是否可以在Sublime側邊欄內顯示文件名旁邊的文件大小?

watch -d ls -al -h folderName 

我在想,如果我可以從崇高直接獲取這些信息。

所以我的問題是:是否有可能有每個文件的大小旁邊的sublime側邊欄文件名?如果是,如何?

+1

有了'SideBarEnhancements'有可能選擇一個文件時,在狀態欄中顯示_FILE_大小。但是我不知道顯示文件夾的大小。 – Karlek

回答

4

由於側邊欄不在官方的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", 
}, 
+0

很好的回答,謝謝。 –

相關問題