0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ANSI ターミナル エスケープシーケンスのモジュール[Python]

Last updated at Posted at 2025-03-26

ANSI ターミナルのエスケープシーケンスを簡単なライブラリにしてみました。
エスケープシーケンスをそのまま書くのはあまりにも判りづらいから、ニモニックにしていただけると幸いです。
全てはカバーしていませんが、よく使うであろうと思われる主要なものを関数化しています。

リファレンス:

escseq.py
#!/usr/bin/python3
ESC='\033['
def escup(n=1):
    print(f"{ESC}{n}A",end='')

def escdown(n=1):
    print(f"{ESC}{n}B",end='')

def escright(n=1):
    print(f"{ESC}{n}C",end='')

def escleft(n=1):
    print(f"{ESC}{n}D",end='')

def esclocate(x=0,y=0):
    print(f"{ESC}{y+1};{x+1}H",end='',flush=True)

def escscrollup(n=1):
    print(f"{ESC}{n}S",end='')

def escscrolldown(n=1):
    print(f"{ESC}{n}T",end='')

def escclear():
    print(f"{ESC}2J",end='')
    esclocate()

def esccolor(col1=7,col2=0):
    print(f"{ESC}3{col1}m{ESC}4{col2}m",end='')

def escresetcolor():
    print(f"{ESC}0m",end='')

if __name__=="__main__":
    escclear()
    for i in range(8):
        for j in range(8):
            esccolor(j,i)
            print("#",end='')
    escresetcolor()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?