Search

Create happy new year message on the linux terminal

We can use the following python script to generate happy new year using the diamond symbol on the linux terminal.

import curses as cur
from curses import wrapper
import locale
import sys, termios, atexit
from select import select
import curses as cur
from curses import wrapper
import time
import datetime
import random
# save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)
# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
# switch to normal terminal
def set_normal_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)
# switch to unbuffered terminal
def set_curses_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)
def putch(ch):
sys.stdout.write(ch)
def getch():
return sys.stdin.read(1)
def getche():
ch = getch()
putch(ch)
return ch
def kbhit():
dr,dw,de = select([sys.stdin], [], [], 0)
return dr
def horizontal(row,cols,num):
for x in range(0,num):
stdscr.addch(row,cols+x,cur.ACS_DIAMOND,cur.color_pair(1))
def vertical(row,cols,num):
for x in range(0,num):
stdscr.addch(row+x,cols,cur.ACS_DIAMOND,cur.color_pair(1))
def forDiagonal(row,cols,num):
for x in range(0,num):
stdscr.addch(row+x,cols+x,cur.ACS_DIAMOND,cur.color_pair(1))
def forUpDiagonal(row,cols,num):
for x in range(0,num):
stdscr.addch(row-x,cols+x,cur.ACS_DIAMOND,cur.color_pair(1))
def backDiagonal(row,cols,num):
for x in range(0,num):
stdscr.addch(row-x,cols-x,cur.ACS_DIAMOND,cur.color_pair(1))
def h(beginRow,beginCols):
vertical(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,4)
vertical(beginRow-2,beginCols+4,5)
def a(beginRow,beginCols):
forUpDiagonal(beginRow+2,beginCols,5)
backDiagonal(beginRow+2,beginCols+8,5)
stdscr.addch(beginRow+1,beginCols+4,cur.ACS_DIAMOND,cur.color_pair(1))
def n(beginRow,beginCols):
vertical(beginRow-2,beginCols,5)
backDiagonal(beginRow+2,beginCols+4,5)
vertical(beginRow-2,beginCols+4,5)
def p(beginRow,beginCols):
horizontal(beginRow-2,beginCols,4)
vertical(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,5)
vertical(beginRow-2,beginCols+4,2)
def y(beginRow,beginCols):
backDiagonal(beginRow,beginCols+1,3)
forUpDiagonal(beginRow,beginCols+3,3)
vertical(beginRow+1,beginCols+2,2)
def e(beginRow,beginCols):
vertical(beginRow-2,beginCols,5)
horizontal(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,5)
horizontal(beginRow+2,beginCols,5)
def w(beginRow,beginCols):
backDiagonal(beginRow+2,beginCols+1,5)
forUpDiagonal(beginRow+2,beginCols+1,3)
backDiagonal(beginRow+2,beginCols+5,3)
forUpDiagonal(beginRow+2,beginCols+5,5)
def r(beginRow,beginCols):
horizontal(beginRow-2,beginCols,4)
vertical(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,5)
vertical(beginRow-2,beginCols+4,2)
backDiagonal(beginRow+2,beginCols+4,3)
if __name__ == '__main__':
atexit.register(set_normal_term)
set_curses_term()
stdscr = cur.initscr()
cur.start_color()
cur.noecho()
cur.cbreak()
locale.setlocale(locale.LC_ALL, '')
stdscr = cur.initscr()
cur.start_color()
cur.curs_set(0)
stdscr.keypad(True)
cur.init_pair(1,cur.COLOR_GREEN, cur.COLOR_BLACK)
cols=lines=0
cols=(cur.COLS)
startRow=(cur.LINES)//2
startCols=2
brow=4
bcols=startCols
while 1:
brow=4
bcols=startCols
stdscr.clear()
if kbhit():
ch = getch()
break
h(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+5
a(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+9
#N
#n()
p(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+6
p(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+7
y(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+9
n(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+6
time.sleep(1)
stdscr.refresh()
e(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+9
w(brow,bcols)
time.sleep(1)
stdscr.refresh()
brow=10
bcols=14
y(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+7
e(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+6
a(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+10
r(brow,bcols)
time.sleep(1)
stdscr.refresh()
stdscr.clear()
cur.nocbreak()
stdscr.keypad(False)
cur.echo()
cur.curs_set(1)
cur.endwin()

Save the file as happynewyear.py and execute it using



You should see the output as shown below.It can be stopped by pressing any key to exit,note that it will exit only after the display of the message.




1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete