forked from erikrose/blessings
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/jquast/blessed
- Loading branch information
Showing
8 changed files
with
301 additions
and
102 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,35 @@ | ||
#!/usr/bin/env python | ||
""" | ||
This is an example application for the 'blessed' Terminal library for python. | ||
Window size changes are caught by the 'on_resize' function using a traditional | ||
signal handler. Meanwhile, blocking keyboard input is displayed to stdout. | ||
If a resize event is discovered, an empty string is returned by term.inkey() | ||
when _intr_continue is False, as it is here. | ||
""" | ||
import signal | ||
from blessed import Terminal | ||
|
||
term = Terminal() | ||
|
||
|
||
def on_resize(sig, action): | ||
print('height={t.height}, width={t.width}'.format(t=term)) | ||
# Its generally not a good idea to put blocking functions (such as print) | ||
# within a signal handler -- if another SIGWINCH is recieved while this | ||
# function blocks, an error will occur. In most programs, you'll want to | ||
# set some kind of 'dirty' flag, perhaps by a Semaphore or global variable. | ||
print('height={t.height}, width={t.width}\r'.format(t=term)) | ||
|
||
signal.signal(signal.SIGWINCH, on_resize) | ||
|
||
with term.cbreak(): | ||
while True: | ||
print(repr(term.inkey(_intr_continue=False))) | ||
# note that, a terminal driver actually writes '\r\n' when '\n' is found, but | ||
# in raw mode, we are allowed to write directly to the terminal without the | ||
# interference of such driver -- so we must write \r\n ourselves; as python | ||
# will append '\n' to our print statements, we simply end our statements with | ||
# \r. | ||
with term.raw(): | ||
print("press 'X' to stop.\r") | ||
inp = None | ||
while inp != 'X': | ||
inp = term.inkey(_intr_continue=False) | ||
print(repr(inp) + u'\r') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
""" | ||
This is an example application for the 'blessed' Terminal library for python. | ||
This isn't a real progress bar, just a sample "animated prompt" of sorts | ||
that demonstrates the separate move_x() and move_y() functions, made | ||
mainly to test the `hpa' compatibility for 'screen' terminal type which | ||
fails to provide one, but blessed recognizes that it actually does, and | ||
provides a proxy. | ||
""" | ||
from __future__ import print_function | ||
from blessed import Terminal | ||
import sys | ||
|
||
|
||
def main(): | ||
term = Terminal() | ||
assert term.hpa(1) != u'', ( | ||
'Terminal does not support hpa (Horizontal position absolute)') | ||
|
||
col, offset = 1, 1 | ||
with term.cbreak(): | ||
inp = None | ||
print("press 'X' to stop.") | ||
sys.stderr.write(term.move(term.height, 0) + u'[') | ||
sys.stderr.write(term.move_x(term.width) + u']' + term.move_x(1)) | ||
while inp != 'X': | ||
if col >= (term.width - 2): | ||
offset = -1 | ||
elif col <= 1: | ||
offset = 1 | ||
sys.stderr.write(term.move_x(col) + u'.' if offset == -1 else '=') | ||
col += offset | ||
sys.stderr.write(term.move_x(col) + u'|\b') | ||
sys.stderr.flush() | ||
inp = term.inkey(0.04) | ||
print() | ||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.