Python诅咒Git子进程的输出具有意外间距

问题描述 投票:1回答:1

当我在python-curses屏幕中调用git时,我观察到一个奇怪的间距问题。在下面的最小工作示例中,我该怎么做,导致间距很小且不与屏幕侧面齐平?

最小工作示例:

import curses, subprocess

class MyApp(object):

    def __init__(self, stdscreen):
        self.screen = stdscreen
        self.screen.addstr("Loading..." + '\n')
        self.screen.refresh()

        url = 'http://github.com/octocat/Hello-World/'

        process = subprocess.Popen(['git', 'clone', url], stdout=subprocess.PIPE)

        self.screen.addstr("Press any key to continue.")

        self.screen.getch()

if __name__ == '__main__':
    curses.wrapper(MyApp)

输出:

Loading...
Press any key to continue.Cloning into 'Hello-World'...
                                                       warning: redirecting to https://github.com/octocat/Hello-World/
              remote: Enumerating objects: 13, done.
                                                    remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Unpacking objects: 100% (13/13), done.3)

预期输出:

Loading...
Cloning into 'Hello-World'...
warning: redirecting to https://github.com/octocat/Hello-World/
remote: Enumerating objects: 13, done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Unpacking objects: 100% (13/13), done.3)
Press any key to continue.
python git curses
1个回答
1
投票

[git clone将其信息性消息写入stderr,而不是stdout。

[From the documentation(加强调):

-verbose

详细运行。不影响向[[标准错误流。]的进度状态报告。


考虑到此更新的MWE:

import curses, subprocess class MyApp(object): def __init__(self, stdscreen): self.screen = stdscreen self.screen.addstr("Loading..." + '\n') self.screen.refresh() url = 'http://github.com/octocat/Hello-World/' #process = subprocess.Popen(['git', 'clone', url], stdout=subprocess.PIPE) # additional code from: https://stackoverflow.com/a/39564562/6924364 dloutput = subprocess.Popen(['git', 'clone', '--progress', url], stderr=subprocess.PIPE, stdout=subprocess.PIPE) dloutput = dloutput.communicate()[1].decode('utf-8') self.screen.addstr(str(dloutput)) self.screen.addstr("Press any key to continue.") self.screen.getch() if __name__ == '__main__': curses.wrapper(MyApp)

© www.soinside.com 2019 - 2024. All rights reserved.