V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
brucmao
V2EX  ›  Sublime Text

SublimeREPL 输出换行很怪异

  •  
  •   brucmao · 2020-11-16 10:06:44 +08:00 · 1735 次点击
    这是一个创建于 1229 天前的主题,其中的信息可能已经有所发展或是发生改变。

    F5 运行多次,每次换行还不一样

    代码

    class Singleton(object):
    
        def __init__(self):
            import time
            time.sleep(1)
    
        @classmethod
        def instance(cls, *args, **kwargs):
            if not hasattr(Singleton, "_instance"):
                Singleton._instance = Singleton(*args, **kwargs)
            return Singleton._instance
    
    import threading
    
    def task(arg):
        obj = Singleton.instance()
        print(obj)
    
    for i in range(10):
        t = threading.Thread(target=task,args=[i,])
        t.start()
    

    配置如下

        {
            "keys":["f5"],
            "caption":"SublimeREPL: save & Python - RUN current file",
            "command":"chain",
            "args":
            {
            "commands":[
                ["save"],
                [ "run_existing_window_command",{"id": "repl_python_run","file": "config/Python/Main.sublime-menu"}]
                   ]
            }
        },
    
    第 1 条附言  ·  2020-11-16 16:32:19 +08:00

    已用上Terminus https://github.com/randy3k/Terminus 配合插件Chain of Command,运行前先保存,配置如下

        {
            "keys":["f5"],
            "caption":"Terminus: save & Python",
            "command":"chain",
            "args":
            {
                "commands":[
                    ["save"],
                    ["toggle_terminus_panel",{"cwd" :"${file_path:${folder}}","cmd" : [ "python","-i","$file" ],},]
               ]
            }
        },
    
    5 条回复    2020-11-17 13:45:19 +08:00
    floyda
        1
    floyda  
       2020-11-16 11:11:52 +08:00   ❤️ 1
    不一样是你 Python 代码的问题, 和 Sublime 无关.
    至于换行的问题, 这是异步导致的输出流的 Bug.
    brucmao
        2
    brucmao  
    OP
       2020-11-16 12:49:30 +08:00
    @floyda #1 在 cmd 控制台运行就正常换行,不知道 sublime 能不能嵌入系统的控制台
    brucmao
        3
    brucmao  
    OP
       2020-11-16 12:51:26 +08:00
    这是在 powershell 运行的效果
    milkpuff
        4
    milkpuff  
       2020-11-16 16:09:03 +08:00   ❤️ 1
    新建一个 Sublime Text 3\Data\Packages\Python\Python.sublime-build 文件,写入如下内容,ctrl+shift+b,就可以调用 cmd 运行脚本了
    {
    "shell_cmd": "python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
    {
    "name": "Run in Cmd",
    "shell_cmd": "start cmd /c \"python \"${file}\" & pause\" ",
    },
    ]
    }
    floyda
        5
    floyda  
       2020-11-17 13:45:19 +08:00
    @brucmao
    安装一个叫`Terminal`的插件, 可以在 Sublime 中快速打开 cmd 终端.
    有专业的为什么不用, 嵌入的总是不尽如人意的.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2804 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:41 · PVG 22:41 · LAX 07:41 · JFK 10:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.