Skip to content

Instantly share code, notes, and snippets.

@drewyeaton
Created December 23, 2011 20:51
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 drewyeaton/1515325 to your computer and use it in GitHub Desktop.
Save drewyeaton/1515325 to your computer and use it in GitHub Desktop.
Simple console GUI
#!/usr/local/bin/python
# coding: utf-8
from blessings import Terminal
import time
import random
class GruiWindow(object):
"""docstring for Grui"""
def __init__(self, term):
super(GruiWindow, self).__init__()
self.term = term
self.views = []
self._clear()
def _clear(self):
for h in range(self.term.height):
with self.term.location(0, h):
print u' ' * self.term.width
def _draw(self):
for v in self.views:
v._draw((0, 0))
def close(self):
self._clear()
self.term.location(0, 0)
def add_view(self, obj):
self.views.append(obj)
def set_val(self, name, msg):
for v in self.views:
if v.name == name:
v.set_val(msg)
self._draw()
break
class GruiProgressView(object):
"""docstring for GruiProgressView"""
def __init__(self, term, name, max, pos, size):
super(GruiProgressView, self).__init__()
self.term = term
self.name = name
self.pos = pos
self.size = size
self.min = 0
self.max = max
self.value = 0
self.dirty = False
self._draw_border()
def set_val(self, value):
self.value = value
self.dirty = True
def _draw(self, origin=(0, 0)):
x, y = self.pos
w, h = self.size
# print '_draw'
with self.term.location(x + 1, y + 1):
l = int(float(w - 2) * (float(self.value) / self.max))
graph = u'▓' * l + (u'░' * (w - 2))
print graph[:(w - 2)]
self.dirty = False
def _draw_border(self):
x, y = self.pos
w, h = self.size
with self.term.location(x, y):
print '╔' + '═' * (w - 2) + '╗'
for i in range(h - 2):
with self.term.location(x, y + i + 1):
print '║'
with self.term.location(x + w - 1, y + i + 1):
print '║'
with self.term.location(x, y + h - 1):
print '╚' + '═' * (w - 2) + '╝'
class GruiTextView(object):
"""docstring for GruiWindow"""
def __init__(self, term, name, pos, size):
super(GruiTextView, self).__init__()
self.term = term
self.name = name
self.pos = pos
self.size = size
self.margin = 1
self.content = []
self.dirty = False
self._draw_border()
def set_val(self, msg):
w, h = self.size
w -= self.margin * 2
h -= self.margin * 2
c = msg + ' ' * w
self.content.append(c[:w])
self.content = self.content[-h:]
self.dirty = True
def _draw_border(self):
x, y = self.pos
w, h = self.size
with self.term.location(x, y):
print '╔' + '═' * (w - 2) + '╗'
for i in range(h - 2):
with self.term.location(x, y + i + 1):
print '║'
with self.term.location(x + w - 1, y + i + 1):
print '║'
with self.term.location(x, y + h - 1):
print '╚' + '═' * (w - 2) + '╝'
def _draw(self, origin=(0, 0)):
if not self.dirty:
return
j, k = origin
x, y = self.pos
x += self.margin + j
y += self.margin + k
for i, c in enumerate(self.content):
with self.term.location(x, y + i):
print c
self.dirty = False
if __name__ == '__main__':
term = Terminal()
g = GruiWindow(term)
g.add_view(GruiTextView(term, 'main', (0, 1), (60, 14)))
g.add_view(GruiTextView(term, 'alt', (0, 15), (60, 5)))
g.add_view(GruiTextView(term, 'side', (61, 1), (19, 19)))
g.add_view(GruiProgressView(term, 'progress', 3000, (0, 20), (80, 3)))
for i in range(3000):
g.set_val('main', '%i. This stuff goes in the main window.' % (i,))
g.set_val('progress', i)
if random.randint(0, 50) == 1:
g.set_val('alt', '%i. Something occasionally!' % (i,))
if random.randint(0, 30) == 1:
g.set_val('side', '%i. Side stuff!' % (i,))
time.sleep(.001)
time.sleep(2)
g.close()
@erikrose
Copy link

erikrose commented Feb 1, 2012

It made me sad that you had to type all those location() calls when all you really needed was to restore the cursor position one time. Much to my own surprise, I found it's possible to use location() without any args to do just that! There's a little writeup at erikrose/blessings#17. Thanks for using Blessings!

@drewyeaton
Copy link
Author

Ha! I totally forgot that I had made this. It was a little project that I made while bored this past holiday. Anyway, I see what you're talking about now. Interesting and thanks for sharing!

@erikrose
Copy link

erikrose commented Feb 3, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment