Léo*_*ard 9 python sublimetext progress-bar sublimetext3
我在MacOSX上使用强大的Sublime Text 3编辑器来运行python代码.我想显示for循环的进度条,以及以下命令:
sys.stdout.write('\rProgress : %03.2f %%' % (100*float(i)/(N)))
sys.flush()
Run Code Online (Sandbox Code Playgroud)
不会按预期(\r)清除输出窗口中先前打印的行但生成N行:
Progress : 0.25 %
Progress : 0.50 %
Progress : 0.75 %
Progress : 1.00 %
Progress : 1.25 %
Progress : 1.50 %
Progress : 1.75 %
Progress : 2.00 %
Progress : 2.25 %
Progress : 2.50 %
...
Run Code Online (Sandbox Code Playgroud)
这不是很好读 - 我得出结论,输出窗口可能是只读的.
有没有人建议改善Sublime Text中进度条的使用?
看一看,sublime.py我们看到该flush方法实际上什么也没做:
class _LogWriter:
def flush(self):
pass
def write(self, s):
sublime_api.log_message(s)
sys.stdout = _LogWriter()
sys.stderr = _LogWriter()
Run Code Online (Sandbox Code Playgroud)
但是我不建议将控制台用于用户输出.通常使用输出面板/视图或状态消息.
状态消息更易于使用,但功能较弱.sergioFC在他的回答中证明了这一点.
这演示了如何使用输出面板.它非常灵活,但您必须编写自己的文本命令来插入文本.这是必要的,因为您需要一个编辑对象来更改视图的内容.
import sublime
import sublime_plugin
class MyInsertProgressBarCommand(sublime_plugin.TextCommand):
def run(self, edit, value):
view = self.view
width, _ = view.viewport_extent()
em_width = view.em_width()
# the number of columns are the width divided by the width of a char
# subtract two to add a little border
columns = int(width / em_width) - 2
# subtract two, because we surround it with [ and ]
bar_length = columns - 2
# calculate the size of the filled and the remaining part
filled_length = int(bar_length * value / 100)
remaining_length = bar_length - filled_length
# assemble the string for the progress bar
text = "[{0}{1}]\n".format("=" * filled_length, "." * remaining_length)
# add the text for the percentages
if value >= 100:
percentage_text = "finished!"
else:
percentage_text = "{:3.2f} %".format(value)
text += " " * (columns - len(percentage_text)) + percentage_text
# replace the content of the view
view.replace(edit, sublime.Region(0, view.size()), text)
# reset sels
view.sel().clear()
view.sel().add(sublime.Region(0, 0))
class ProgressBarCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.create_output_panel("progess_bar")
self.window.run_command("show_panel", {"panel": "output.progess_bar"})
def test_progress_bar():
import random
test_progress_bar.value += 2 * random.random()
if test_progress_bar.value >= 100:
self.finish_progress()
return
self.show_progress(test_progress_bar.value)
sublime.set_timeout(test_progress_bar, 100)
test_progress_bar.value = 0
sublime.set_timeout_async(test_progress_bar, 1)
def show_progress(self, progess):
view = self.window.find_output_panel("progess_bar")
view.run_command("my_insert_progress_bar", {"value": progess})
def finish_progress(self):
self.show_progress(100)
sublime.set_timeout(self._destroy, 5000)
def _destroy(self):
self.window.destroy_output_panel("progess_bar")
Run Code Online (Sandbox Code Playgroud)
输出:

作为另一种替代解决方案,您可以使用状态栏。设置状态栏消息时,先前的文本将被清除。软件包控制在安装软件包时还使用状态栏。
例:
import sublime, sublime_plugin
import time
class ExampleCommand(sublime_plugin.WindowCommand):
def run(self, args):
sublime.set_timeout_async(self.test,1)
def test(self):
i=80
while i <= 100:
sublime.status_message('%03.2f %%' % i)
time.sleep(0.15)
i+=0.25
sublime.status_message('100% Stackoverflow!')
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法创建可视进度条:

(通过输入Progress Bar Demo@命令调色板来运行插件)
有一个控制风格的css文件mdpopups.由于某种原因,该color财产没有任何影响.
此外,mdpopups.show_popup's location参数需要-1将弹出窗口设置在插入符号位置.否则,我不确定location弹出窗口是如何影响的,因为它只需要一个整数值.
我在下面的帖子中询问过这些问题:
不幸的是,这在 Sublime 的输出面板中是不可能的。该面板不是真正的控制台或终端,并且除其他差异外,它不解释转义序列,例如\\r和\\b(但是\\n 解释正确)。如果您想了解它的具体工作原理,请安装PackageResourceViewer,然后打开Packages/Default/exec.py。
为了使其正常工作,您需要创建一个新的构建系统以在终端中运行它。由于 OS X 的变化莫测,您需要创建两个文件。第一个是 shell 脚本:
\n\n#!/bin/sh\nosascript -e \' \n on run parameters \n tell application "Terminal" \n activate \n do script with command "/path/to/python " & parameters \n end tell \n end run\n\' $@\nRun Code Online (Sandbox Code Playgroud)\n\n更改/path/to为python(或python3) 的实际路径。将其另存为PythonTerminal.sh. 接下来,选择Tools -> Build System -> New Build System并粘贴以下内容:
{\n "cmd": ["/bin/sh /path/to/Python3Terminal.sh \\"$file\\""],\n "file_regex": "^[ ]*File \\"(...*?)\\", line ([0-9]*)",\n "selector": "source.python",\n "shell": true\n}\nRun Code Online (Sandbox Code Playgroud)\n\n再次更改/path/to为实际路径PythonTerminal.sh。将文件另存为Packages/User/PythonTerminal.sublime-build(保存时应自动打开正确的目录)。
最后,选择Tools -> Build System -> PythonTerminal,切换到您的 Python 文件,并使用\xe2\x8c\x98B. 将打开一个新的终端窗口,并且进度条应该运行。