简体   繁体   English

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

[英]Python Curses Git Subprocess Output with unexpected spacing

I am observing a weird spacing issue when I call git within a python-curses screen.当我在 python-curses 屏幕中调用 git 时,我观察到一个奇怪的间距问题。 What am I doing in my minimal working example below that causes the spacing to be ajar and not flush with the side of the screen?在下面的最小工作示例中,我在做什么导致间距半开而不与屏幕侧面齐平?

Minimal Working Example:最小工作示例:

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)

Output:输出:

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)

Expected Output:预期输出:

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.

git clone writes its informative messages to stderr, not stdout. git clone将其信息性消息写入 stderr,而不是 stdout。

From the documentation (emphasis added): 从文档(强调):

--verbose --详细

Run verbosely.详细运行。 Does not affect the reporting of progress status to the standard error stream .不影响向标准错误流报告进度状态。


(the code below is by Justapigeon ) (下面的代码是由Justapigeon 提供的

Updated MWE with this consideration:基于此考虑更新了 MWE:

import curses, subprocess
import os, re

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)

        fd = dloutput.stderr.fileno()
        gitmsg = [] 
        while True:
            lines = os.read(fd,1000).decode('utf-8')
            lines = re.split('\n|\r', lines)
            for l in lines:
                self.screen.erase()
                for g in gitmsg:
                    self.screen.addstr(g + '\n')
                if l != '':
                    self.screen.addstr(str(l) + '\n')
                    self.screen.refresh()
                if 'Cloning' in l:
                    gitmsg.append(l)
                if 'done' in l:
                    gitmsg.append(l)
            if len(lines) == 1:
                break

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

        self.screen.getch()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM