add tui module and example, add contex manager cd and the method run_file in helper
import vendor modules only once during import change text color for the method cad_wireframe in geometry_plot_pylab and add hide key
This commit is contained in:
193
examples/tui.py
Executable file
193
examples/tui.py
Executable file
@@ -0,0 +1,193 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Example tui.
|
||||
|
||||
:Date: 2020-01-12
|
||||
|
||||
.. module:: tui
|
||||
:platform: *nix, Windows
|
||||
:synopsis: Example tui.
|
||||
|
||||
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
|
||||
"""
|
||||
|
||||
import curses
|
||||
import locale
|
||||
from pylib.data import seq
|
||||
from pylib.function import sine_wave, cosine_wave, hypotrochoid, to_str
|
||||
from pylib.tui import TUI
|
||||
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
CODE = locale.getpreferredencoding()
|
||||
|
||||
def standard_text(window):
|
||||
window.text(
|
||||
"1 - Show test page\n" +
|
||||
"2 - Show Hypotrochoid example\n" +
|
||||
"h - Show help page\n" +
|
||||
"q - Exit", 1, 2)
|
||||
|
||||
def help_text(window, tui, str_width_height):
|
||||
screen_height, screen_width = window.getmaxyx()
|
||||
last_screen_row = screen_height - 1
|
||||
|
||||
tui.color_table() # print colors
|
||||
|
||||
window.text("screen width and height", 1, last_screen_row-3)
|
||||
window.text("\u2502", 3, last_screen_row-2) # \u2502
|
||||
window.text("\u2502", 3, last_screen_row-1) # \u2193 v
|
||||
|
||||
window.text("last pressed key as Unicode code point", len(str_width_height)+2, last_screen_row-2)
|
||||
window.text("\u2502", 1+len(str_width_height)+1, last_screen_row-1)
|
||||
|
||||
num_reloads = "number of screen reloads\u256e"
|
||||
# screen_width-1-len(num_reloads.encode('utf-16'))//2
|
||||
window.text(num_reloads, screen_width-1-len(num_reloads), last_screen_row-1)
|
||||
|
||||
def test_text(window):
|
||||
window.text("Test page", 1, 2)
|
||||
|
||||
def full_text(window, t):
|
||||
#window.clear()
|
||||
screen_height, screen_width = window.getmaxyx()
|
||||
|
||||
# Sine
|
||||
f = sine_wave(A=0.5, k=0.1, D=0.5)
|
||||
frame = to_str(f, x_0=0, x_1=1, h=screen_height-2, w=screen_width, t=t)
|
||||
|
||||
# Turtle
|
||||
import drawille
|
||||
turtle = drawille.Turtle()
|
||||
for _ in range(36):
|
||||
turtle.right(10 + t)
|
||||
for _ in range(0, 36):
|
||||
turtle.right(10)
|
||||
turtle.forward(8)
|
||||
frame = turtle.frame()
|
||||
# size for forward 1: width 11+1, height 6
|
||||
# size for forward 2: width 23+1, height 12
|
||||
# size for forward 5: width 57+1, height 30
|
||||
# size for forward 6: width 69+1, height 36
|
||||
# size for forward 10: width 114+1, height 58
|
||||
# n 12*n 6*n
|
||||
|
||||
# Hypotrochoid
|
||||
demo = 2
|
||||
c = drawille.Canvas()
|
||||
R = 20
|
||||
r = 6
|
||||
d = 30
|
||||
if demo == 1:
|
||||
r = int(t/50)%19+1
|
||||
x, y, theta = hypotrochoid(R, r, d)
|
||||
for i in seq(0, theta[1]*0.98, theta[1]/10000):
|
||||
c.set(x(i+t), y(i+t))
|
||||
a = R+d+1 # R-r+d+1
|
||||
frame = c.frame(min_x=-a, max_x=a, min_y=-a, max_y=a)
|
||||
if demo == 2:
|
||||
d = int(t)%21
|
||||
x, y, theta = hypotrochoid(R, r, d)
|
||||
for i in seq(0, theta[1]*0.98, theta[1]/5000):
|
||||
c.set(x(i+t), y(i+t))
|
||||
a = 2*R # R-r+d+1
|
||||
frame = c.frame(min_x=-a, max_x=a, min_y=-a, max_y=a)
|
||||
|
||||
window.text(frame, padding_top=1, color_pair=10)
|
||||
window.text("R=%2s r=%2s d=%2s" % (R, r, d), 1, int(a/2)+2)
|
||||
|
||||
def win_charts_line(window, f_list, x_0, x_1, w, t, color_pairs, densities):
|
||||
win_chart_line, _ = window.textbox(height=8, width=52, y=9, x=5, borders=True)
|
||||
# width 100 pixel / 2 pixels/column = 50 columns
|
||||
for f, color_pair, density in zip(f_list, color_pairs, densities):
|
||||
frame = to_str(f, x_0=x_0, x_1=x_1, h=6, w=w, t=t, density=density)
|
||||
win_chart_line.text(frame, color_pair=color_pair)
|
||||
|
||||
def win_chart_histogram(window, f, x_0, x_1, w, t):
|
||||
newwin2, _ = window.textbox(height=4, width=52, y=9+8, x=5, borders=True)
|
||||
|
||||
#frame2 = function_frame(f, x_0=x_0, x_1=x_1, h=2, w=w, t=t, char_set="histogram")
|
||||
frame2 = to_str(f, x_0=x_0, x_1=x_1, h=2, w=w, t=t, char_set="histogram")
|
||||
newwin2.text(frame2, color_pair=3)
|
||||
frame3 = to_str(f, x_0=x_0, x_1=x_1, h=2, w=w, t=t, density=0.5, char_set="histogram")
|
||||
newwin2.text(frame3, color_pair=4)
|
||||
|
||||
def main(delay=1):
|
||||
"""
|
||||
no window change for mouse events and resize events.
|
||||
"""
|
||||
tui = TUI(delay)
|
||||
initscr = tui.initscr
|
||||
try:
|
||||
tui.last_pressed_ch = 0 # getch() Note that the integer returned does not have to be in ASCII range: function keys, keypad keys and so on return numbers higher than 256.
|
||||
tui.last_pressed_mouse_ch = 0
|
||||
i = 1
|
||||
while True:
|
||||
tui.clear()
|
||||
|
||||
screen_height, screen_width = initscr.getmaxyx()
|
||||
str_width_height = str(screen_width) + 'x' + str(screen_height) # width and height of the screen
|
||||
footer_left = [str_width_height, str(tui.last_pressed_ch)] # and last pressed key as char integer ( Unicode code point)
|
||||
i += 1 # number of screen reloads
|
||||
initscr.border("Python curses in action!", footer_left, str(i))
|
||||
|
||||
if tui.last_pressed_ch == ord('1'):
|
||||
test_text(initscr)
|
||||
|
||||
elif tui.last_pressed_ch == ord('2'):
|
||||
full_text(initscr, i)
|
||||
|
||||
elif tui.last_pressed_ch == ord('h'):
|
||||
help_text(initscr, tui, str_width_height)
|
||||
|
||||
elif tui.last_pressed_ch == ord('q'):
|
||||
break
|
||||
|
||||
else:
|
||||
standard_text(initscr)
|
||||
|
||||
x_0 = 0
|
||||
x_1 = 1 # TODO
|
||||
w = 50
|
||||
t = i/100
|
||||
|
||||
# define function normed to the range of [0, 1]
|
||||
y_1 = sine_wave(A=.5, k=.1, D=.5)
|
||||
y_1_2 = sine_wave(A=.5, k=.1, f=1+1/3, phi=30, D=.5, degree=True)
|
||||
y_1_3 = sine_wave(A=.5, k=.1, f=1+2/3, phi=60, D=.5, degree=True)
|
||||
y_2 = cosine_wave(A=.5, k=.1, f=2, D=.5)
|
||||
win_charts_line(initscr, [y_1, y_1_2, y_1_3, y_2], x_0, x_1, w, t, [209, 215, 221, 227], [5, 5, 5, 5])
|
||||
#win_charts_line(initscr, [y_1, y_1_2, y_1_3, y_2], x_0, x_1, w, t, [10, 7, 7, 7], [5, 1, 1, 1])
|
||||
#y_2 = cosine_wave(A=0.5, k=0.2, f=2, D=0.5)
|
||||
#win_charts_line(initscr, [y_1, y_2], x_0, x_1, w, t, [10, 7], [5, 1])
|
||||
win_chart_histogram(initscr, y_1, x_0, x_1, w, t)
|
||||
|
||||
# mouse event has occurred
|
||||
if tui.last_pressed_mouse_ch == curses.KEY_MOUSE:
|
||||
try:
|
||||
_, mx, my, _, _ = curses.getmouse()
|
||||
initscr.text("Mouse pos " + str(mx) + " " + str(my), 1, 6)
|
||||
initscr.text(initscr.instr(my, mx, 5), mx, my, color_pair=5)
|
||||
except curses.error:
|
||||
pass
|
||||
|
||||
# std content
|
||||
# cursor position (last addstr is cursor position)
|
||||
if tui.cursor_visibility > 0:
|
||||
initscr.text("", 1, 6)
|
||||
|
||||
tui.refresh() # update the screen
|
||||
tui.getch()
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print('\n'.join(traceback.format_exception(*sys.exc_info())))
|
||||
|
||||
finally:
|
||||
tui.end()
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
if len(sys.argv) > 1:
|
||||
main(sys.argv[1])
|
||||
else:
|
||||
main()
|
||||
Reference in New Issue
Block a user