Last active
March 6, 2023 06:03
-
-
Save mshafae/da88307f3b58b7a82bd26a8e998d6ce5 to your computer and use it in GitHub Desktop.
CPSC 386 Clearing the terminal three different ways.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Clearing the terminal three different ways. | |
# | |
# Gist https://gist.github.com/da88307f3b58b7a82bd26a8e998d6ce5 | |
# | |
def simple(): | |
# you can make it fancier by figuring out how many rows | |
# the terminal has and replace the hard-coded 24. | |
# cursor stays at the bottom. | |
print('\n' * 24) | |
def with_curses(): | |
# https://docs.python.org/3/howto/curses.html | |
import curses | |
def cdemo(scr): | |
scr.clear() | |
x = junk_strings() | |
for i in range(22): | |
scr.addstr(i, 0, x[i]) | |
# scr.addstr(0, 0, junk_string()) | |
scr.refresh() | |
scr.addstr(22, 0, 'Press any key.') | |
scr.getkey() | |
curses.wrapper(cdemo) | |
def with_tty_clear(): | |
# cursur moves to the stop | |
import curses | |
import sys | |
import os | |
if sys.stdout.isatty(): | |
curses.setupterm() | |
seq = curses.tigetstr('clear') | |
os.write(sys.stdout.fileno(), seq) | |
def junk_strings(): | |
from random import choices | |
l = list('abcdefghijklmnopqrstuvwzyz') | |
l = l + list(map(lambda x: x.upper(), l)) + list('01234567890~!@#$%^&*()_+<>?:{}') | |
x = [''.join(choices(l, k=80)) for _ in range(24)] | |
return x | |
def print_junk(): | |
from random import choices | |
l = list('abcdefghijklmnopqrstuvwzyz') | |
l = l + list(map(lambda x: x.upper(), l)) + list('01234567890~!@#$%^&*()_+<>?:{}') | |
for _ in range(24): | |
print(''.join(choices(l, k=80))) | |
def main(): | |
from time import sleep | |
print_junk() | |
sleep(1) | |
simple() | |
sleep(1) | |
print_junk() | |
sleep(1) | |
with_tty_clear() | |
sleep(1) | |
with_curses() | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment