1
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/env python3
ESC='\033['
def escnocursor():
    print(f"{ESC}?25l",end='',flush=True)
    return

def escdispcursor():
    print(f"{ESC}?25h",end='',flush=True)
    return

def escup(n=1):
    print(f"{ESC}{n}A",end='',flush=True)

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

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

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

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='',flush=True)

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

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

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

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

def escclraftcur():
    print(f"{ESC}0J",end='',flush=True)

def escclrline():
    print(f"{ESC}2K",end='',flush=True)


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

1
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
1
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?