Skip to content

Instantly share code, notes, and snippets.

@emasaka
Created February 14, 2010 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emasaka/303778 to your computer and use it in GitHub Desktop.
Save emasaka/303778 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import curses
import locale
import signal
import unicodedata
HATO = (
u"   _,,_",
u"  /´o ヽ",
u",.ィゝ     l   ",
u" ̄ヽ     l",
u"    l     ヽ___",
u"  /  ,,...---`ニニニ==、,,__",
u"  l  / ヽ ヽ ヽ ヽ ヽ ヽ ヽ l三三三>",
u"   |  iヽ ヽ ヽ ヽ ヽ ヽ ヽ/三三/''ー- 、",
u"  ヽ. ヽ、ヽ ヽ ヽ ヽ ヽ.∠三=‐''´>‐--‐'",
u"    ヽ、`'''ー‐---‐'''´_,,...--‐'''´",
u"      `''ーッ--t_,r'''´",
u"     _/._/ " )
def uni_char_width(c):
w = unicodedata.east_asian_width(c)
if w == 'F' or w == 'W' or w == "A":
return 2
else:
return 1
def uni_str_width(str):
len = 0
for c in str:
len = len + uni_char_width(c)
return len
def hato_width(hato):
mx = 0
for s in hato:
sz = uni_str_width(s)
if sz > mx: mx = sz
return mx
def take_col(str, beg, end):
s_len = len(str)
col = 0
beg_i = 0
while True:
if beg_i >= s_len: return ""
col = col + uni_char_width(str[beg_i])
if col >= beg: break
beg_i = beg_i + 1
end_i = beg_i
while True:
if end_i >= s_len: break
col = col + uni_char_width(str[end_i])
if col >= end: break
end_i = end_i + 1
return str[beg_i:end_i].encode('utf-8')
def hato(win, hato):
rows, cols = win.getmaxyx()
y = (rows - len(HATO)) / 2
for i in xrange(1, cols + hato_width(hato)):
if i < cols:
x = cols - i
beg = 0
end = i
else:
x = 1
beg = i - cols
end = cols
for j in xrange(0, len(hato)):
win.addstr(y + j, x, take_col(hato[j], beg, end))
win.clrtoeol()
win.refresh()
curses.napms(50)
signal.signal(signal.SIGINT, signal.SIG_IGN)
locale.setlocale(locale.LC_ALL, "")
scrn = curses.initscr()
scrn.leaveok(1)
hato(scrn, HATO)
curses.endwin
signal.signal(signal.SIGINT, signal.SIG_DFL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment