Fix up curses stuff
This commit is contained in:
parent
ed4b5abd24
commit
a03bfaf3dc
|
@ -0,0 +1,92 @@
|
||||||
|
import sys,os
|
||||||
|
import curses
|
||||||
|
|
||||||
|
def draw_menu(stdscr):
|
||||||
|
k = 0
|
||||||
|
cursor_x = 0
|
||||||
|
cursor_y = 0
|
||||||
|
|
||||||
|
# Clear and refresh the screen for a blank canvas
|
||||||
|
stdscr.clear()
|
||||||
|
stdscr.refresh()
|
||||||
|
|
||||||
|
# Start colors in curses
|
||||||
|
curses.start_color()
|
||||||
|
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
||||||
|
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
|
||||||
|
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
|
||||||
|
|
||||||
|
# Loop where k is the last character pressed
|
||||||
|
while (k != ord('q')):
|
||||||
|
|
||||||
|
# Initialization
|
||||||
|
stdscr.clear()
|
||||||
|
height, width = stdscr.getmaxyx()
|
||||||
|
|
||||||
|
if k == curses.KEY_DOWN:
|
||||||
|
cursor_y = cursor_y + 1
|
||||||
|
elif k == curses.KEY_UP:
|
||||||
|
cursor_y = cursor_y - 1
|
||||||
|
elif k == curses.KEY_RIGHT:
|
||||||
|
cursor_x = cursor_x + 1
|
||||||
|
elif k == curses.KEY_LEFT:
|
||||||
|
cursor_x = cursor_x - 1
|
||||||
|
|
||||||
|
cursor_x = max(0, cursor_x)
|
||||||
|
cursor_x = min(width-1, cursor_x)
|
||||||
|
|
||||||
|
cursor_y = max(0, cursor_y)
|
||||||
|
cursor_y = min(height-1, cursor_y)
|
||||||
|
|
||||||
|
# Declaration of strings
|
||||||
|
title = "Curses example"[:width-1]
|
||||||
|
subtitle = "Written by Clay McLeod"[:width-1]
|
||||||
|
keystr = "Last key pressed: {}".format(k)[:width-1]
|
||||||
|
statusbarstr = "Press 'q' to exit | STATUS BAR | Pos: {}, {}".format(cursor_x, cursor_y)
|
||||||
|
if k == 0:
|
||||||
|
keystr = "No key press detected..."[:width-1]
|
||||||
|
|
||||||
|
# Centering calculations
|
||||||
|
start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
|
||||||
|
start_x_subtitle = int((width // 2) - (len(subtitle) // 2) - len(subtitle) % 2)
|
||||||
|
start_x_keystr = int((width // 2) - (len(keystr) // 2) - len(keystr) % 2)
|
||||||
|
start_y = int((height // 2) - 2)
|
||||||
|
|
||||||
|
# Rendering some text
|
||||||
|
whstr = "Width: {}, Height: {}".format(width, height)
|
||||||
|
stdscr.addstr(0, 0, whstr, curses.color_pair(1))
|
||||||
|
|
||||||
|
# Render status bar
|
||||||
|
stdscr.attron(curses.color_pair(3))
|
||||||
|
stdscr.addstr(height-1, 0, statusbarstr)
|
||||||
|
stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
|
||||||
|
stdscr.attroff(curses.color_pair(3))
|
||||||
|
|
||||||
|
# Turning on attributes for title
|
||||||
|
stdscr.attron(curses.color_pair(2))
|
||||||
|
stdscr.attron(curses.A_BOLD)
|
||||||
|
|
||||||
|
# Rendering title
|
||||||
|
stdscr.addstr(start_y, start_x_title, title)
|
||||||
|
|
||||||
|
# Turning off attributes for title
|
||||||
|
stdscr.attroff(curses.color_pair(2))
|
||||||
|
stdscr.attroff(curses.A_BOLD)
|
||||||
|
|
||||||
|
# Print rest of text
|
||||||
|
stdscr.addstr(start_y + 1, start_x_subtitle, subtitle)
|
||||||
|
stdscr.addstr(start_y + 3, (width // 2) - 2, '-' * 4)
|
||||||
|
stdscr.addstr(start_y + 5, start_x_keystr, keystr)
|
||||||
|
stdscr.move(cursor_y, cursor_x)
|
||||||
|
|
||||||
|
# Refresh the screen
|
||||||
|
stdscr.refresh()
|
||||||
|
|
||||||
|
# Wait for next input
|
||||||
|
k = stdscr.getch()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
curses.wrapper(draw_menu)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -0,0 +1,17 @@
|
||||||
|
from dashing import *
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
ui = HSplit(
|
||||||
|
VSplit(
|
||||||
|
Log(title='logs', border_color=5),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# access a tile by index
|
||||||
|
log = ui.items[0].items[0]
|
||||||
|
log.append("0 -----")
|
||||||
|
log.append("1 Hello")
|
||||||
|
log.append("2 -----")
|
||||||
|
|
||||||
|
# display/refresh the ui
|
||||||
|
ui.display()
|
|
@ -1,40 +1,73 @@
|
||||||
from blessed import Terminal
|
from time import sleep, time
|
||||||
|
import math
|
||||||
|
|
||||||
class Graphics:
|
from dashing import *
|
||||||
def __init__(self):
|
|
||||||
self.bottom_left_corner = u"└"
|
|
||||||
self.bottom_right_corner = u"┘"
|
|
||||||
self.top_left_corner = u"┌"
|
|
||||||
self.top_right_corner = u"┐"
|
|
||||||
self.border_horizontal = u"─"
|
|
||||||
self.border_vertical = u"│"
|
|
||||||
|
|
||||||
class Window:
|
if __name__ == '__main__':
|
||||||
def __init__(self, term, height, width, xpos, ypos, title=None):
|
|
||||||
self.term = term
|
|
||||||
|
|
||||||
self.title = title
|
ui = HSplit(
|
||||||
|
VSplit(
|
||||||
self.height = height
|
HGauge(val=50, title="only title", border_color=5),
|
||||||
self.width = width
|
HGauge(label="only label", val=20, border_color=5),
|
||||||
self.xpos = xpos
|
HGauge(label="only label", val=30, border_color=5),
|
||||||
self.ypos = ypos
|
HGauge(label="only label", val=50, border_color=5),
|
||||||
|
HGauge(label="only label", val=80, border_color=5),
|
||||||
def draw_borders(self):
|
HGauge(val=20),
|
||||||
# Print the top
|
HGauge(label="label, no border", val=55),
|
||||||
print(
|
HSplit(
|
||||||
self.term.move_xy(self.xpos, self.ypos) +
|
VGauge(val=0, border_color=2),
|
||||||
u"┌" +
|
VGauge(val=5, border_color=2),
|
||||||
u"─" * (self.width - 2) +
|
VGauge(val=30, border_color=2),
|
||||||
u"┐"
|
VGauge(val=50, border_color=2),
|
||||||
|
VGauge(val=80, border_color=2, color=4),
|
||||||
|
VGauge(val=95, border_color=2, color=3),
|
||||||
|
ColorRangeVGauge(
|
||||||
|
val=100,
|
||||||
|
border_color=2,
|
||||||
|
colormap=(
|
||||||
|
(33, 2),
|
||||||
|
(66, 3),
|
||||||
|
(100, 1),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
VSplit(
|
||||||
|
Text('Hello World,\nthis is dashing.', border_color=2),
|
||||||
|
Log(title='logs', border_color=5),
|
||||||
|
VChart(border_color=2, color=2),
|
||||||
|
HChart(border_color=2, color=2),
|
||||||
|
HBrailleChart(border_color=2, color=2),
|
||||||
|
# HBrailleFilledChart(border_color=2, color=2),
|
||||||
|
),
|
||||||
|
title='Dashing',
|
||||||
)
|
)
|
||||||
|
log = ui.items[1].items[1]
|
||||||
|
vchart = ui.items[1].items[2]
|
||||||
|
hchart = ui.items[1].items[3]
|
||||||
|
bchart = ui.items[1].items[4]
|
||||||
|
# bfchart = ui.items[1].items[5]
|
||||||
|
log.append("0 -----")
|
||||||
|
log.append("1 Hello")
|
||||||
|
log.append("2 -----")
|
||||||
|
prev_time = time()
|
||||||
|
for cycle in range(0, 200):
|
||||||
|
ui.items[0].items[0].value = int(50 + 49.9 * math.sin(cycle / 80.0))
|
||||||
|
ui.items[0].items[1].value = int(50 + 45 * math.sin(cycle / 20.0))
|
||||||
|
ui.items[0].items[2].value = int(50 + 45 * math.sin(cycle / 30.0 + 3))
|
||||||
|
|
||||||
# Print the middle
|
vgauges = ui.items[0].items[-1].items
|
||||||
# We exclude the top and bottom rows since we'll draw them with other chars
|
for gaugenum, vg in enumerate(vgauges):
|
||||||
for dx in range (1, self.height - 1):
|
vg.value = 50 + 49.9 * math.sin(cycle / 12.0 + gaugenum)
|
||||||
print(self.term.move_xy(self.xpos + dx, self.ypos) + u'│')
|
|
||||||
print(self.term.move_xy(self.xpos + dx, self.ypos + self.width - 1) + u'│')
|
|
||||||
|
|
||||||
terminal = Terminal()
|
t = int(time())
|
||||||
my_window = Window(terminal, 4, 20, 10, 10)
|
if t != prev_time:
|
||||||
my_window.draw_borders()
|
log.append("%s" % t)
|
||||||
|
prev_time = t
|
||||||
|
vchart.append(50 + 50 * math.sin(cycle / 16.0))
|
||||||
|
hchart.append(99.9 * abs(math.sin(cycle / 26.0)))
|
||||||
|
bchart.append(50 + 50 * math.sin(cycle / 6.0))
|
||||||
|
# bfchart.append(50 + 50 * math.sin(cycle / 16.0))
|
||||||
|
ui.display()
|
||||||
|
|
||||||
|
sleep(1.0/25)
|
Loading…
Reference in New Issue