Fix up curses stuff

This commit is contained in:
Joe S 2021-02-14 15:47:09 -05:00
parent ed4b5abd24
commit a03bfaf3dc
3 changed files with 176 additions and 34 deletions

View File

@ -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()

View File

@ -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()

View File

@ -1,40 +1,73 @@
from blessed import Terminal
from time import sleep, time
import math
class Graphics:
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""
from dashing import *
class Window:
def __init__(self, term, height, width, xpos, ypos, title=None):
self.term = term
if __name__ == '__main__':
self.title = title
self.height = height
self.width = width
self.xpos = xpos
self.ypos = ypos
def draw_borders(self):
# Print the top
print(
self.term.move_xy(self.xpos, self.ypos) +
u"" +
u"" * (self.width - 2) +
u""
ui = HSplit(
VSplit(
HGauge(val=50, title="only title", border_color=5),
HGauge(label="only label", val=20, border_color=5),
HGauge(label="only label", val=30, border_color=5),
HGauge(label="only label", val=50, border_color=5),
HGauge(label="only label", val=80, border_color=5),
HGauge(val=20),
HGauge(label="label, no border", val=55),
HSplit(
VGauge(val=0, border_color=2),
VGauge(val=5, border_color=2),
VGauge(val=30, border_color=2),
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
# We exclude the top and bottom rows since we'll draw them with other chars
for dx in range (1, self.height - 1):
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'')
vgauges = ui.items[0].items[-1].items
for gaugenum, vg in enumerate(vgauges):
vg.value = 50 + 49.9 * math.sin(cycle / 12.0 + gaugenum)
terminal = Terminal()
my_window = Window(terminal, 4, 20, 10, 10)
my_window.draw_borders()
t = int(time())
if t != prev_time:
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)