0

私は両方が欲しい:

  • 通常の端末のように次々と表示される行 (Blah 12、Blah 13、Blah 14 など)

  • 定位置情報(右):日付+定型文「Bonjour」

外観が破壊されるまで、ほぼ機能しますなんで?


(ソース: gget.it )

from sys import stdout
import time

ESC = "\x1b"
CSI = ESC+"["

def movePos(row, col):
    stdout.write("%s%d;%dH" % (CSI, row, col))
  
stdout.write("%s2J" % CSI)      # CLEAR SCREEN

for i in range(1,1000):
    movePos(i+1,60)
    print time.strftime('%H:%M:%S', time.gmtime())
    movePos(i+5,60)
    print 'Bonjour'

    movePos(24+i,0)
    print "Blah %i" % i
    time.sleep(0.01)

printANSI 端末では、通常の端末動作 (それぞれに 1 つの新しい行) + 固定位置表示の両方を行うにはどうすればよいですか?

注: Windows では、ansicon.exe を使用して、Windows の cmd.exe で ANSI をサポートしています。

4

2 に答える 2

1

ここに解決策があります:


(ソース: gget.it )

コードは次のとおりです(最新バージョンについては、こちらを確認してください):

"""
zeroterm is a light weight terminal allowing both:
* lines written one after another (normal terminal/console behaviour)
* fixed position text

Note: Requires an ANSI terminal. For Windows 7, please download https://github.com/downloads/adoxa/ansicon/ansi160.zip and run ansicon.exe -i to install it.
"""

from sys import stdout
import time

class zeroterm:
    def __init__(self, nrow=24, ncol=50):      # nrow, ncol determines the size of the scrolling (=normal terminal behaviour) part of the screen
        stdout.write("\x1b[2J")                # clear screen
        self.nrow = nrow
        self.ncol = ncol
        self.buf = []

    def write(self, s, x=None, y=None):        # if no x,y specified, normal console behaviour
        if x is not None and y is not None:    # if x,y specified, fixed text position
            self.movepos(x,y)
            print s
        else:
            if len(self.buf) < self.nrow:
                self.buf.append(s)
            else:
                self.buf[:-1] = self.buf[1:]
                self.buf[-1] = s

            for i, r in enumerate(self.buf):
                self.movepos(i+1,0)
                print r[:self.ncol].ljust(self.ncol)

    def movepos(self, row, col):
        stdout.write("\x1b[%d;%dH" % (row, col))


if __name__ == '__main__':
    # An exemple
    t = zeroterm()
    t.write('zeroterm', 1, 60)

    for i in range(1000):
        t.write(time.strftime("%H:%M:%S"), 3, 60)
        t.write("Hello %i" % i)
        time.sleep(0.1)
于 2015-12-21T14:36:34.067 に答える
0

与えられた図から: ansicon は、その作業を行うためにコンソール バッファーを割り当てているように見えます。サイズが制限されています (バッファ サイズを 64 キロバイトに制限する Windows コンソールのため)。スクリプトがバッファの末尾に到達し、カーソルを末尾を超えて移動しようとすると、ansicon によってバッファ全体が強制的に上にスクロールされます。それにより、更新のスタイルが変わります。

への呼び出しmovePosが ansicon のワークスペース内に限定されていれば、より一貫した結果が得られます。

「Bonjour」の「複数行」については、このチャンク

movePos(i+1,60)
print time.strftime('%H:%M:%S', time.gmtime())
movePos(i+5,60)
print 'Bonjour'

日付を 1 行に印刷してから 4 行進めて、同じ列に「Bonjour」を印刷しています。これを行うには、同じ行に十分なスペース (10 列) があるようです。

movePos(i+1,60)
print time.strftime('%H:%M:%S', time.gmtime())
movePos(i+1,70)
print 'Bonjour'

これにより、少なくとも右側のテキストが一貫して見えるようになります。ただし、スクロールmovePosすると、ダブルスペースが発生する場合があります。

于 2015-12-20T22:54:21.840 に答える