futurize stage2 vui

This commit is contained in:
Simon Forman 2020-04-23 23:44:29 -07:00
parent f949efe1a4
commit d2785f6f68
9 changed files with 113 additions and 91 deletions

View File

@ -29,6 +29,7 @@ a mainloop class that manages the, uh, main loop (the PyGame event queue.)
''' '''
from __future__ import print_function from __future__ import print_function
from builtins import object, str, range
from sys import stderr from sys import stderr
from traceback import format_exc from traceback import format_exc
import pygame import pygame

View File

@ -30,6 +30,9 @@ There is a Display object that manages a pygame surface and N vertical
tracks each of which manages zero or more viewers. tracks each of which manages zero or more viewers.
''' '''
from __future__ import print_function from __future__ import print_function
from __future__ import division
from builtins import next, object
from past.utils import old_div
from copy import copy from copy import copy
from sys import stderr from sys import stderr
from traceback import format_exc from traceback import format_exc
@ -80,7 +83,7 @@ class Display(object):
if not track_ratios: track_ratios = 1, 4 if not track_ratios: track_ratios = 1, 4
x, total = 0, sum(track_ratios) x, total = 0, sum(track_ratios)
for ratio in track_ratios[:-1]: for ratio in track_ratios[:-1]:
track_width = self.w * ratio / total track_width = old_div(self.w * ratio, total)
assert track_width >= 10 # minimum width 10 pixels assert track_width >= 10 # minimum width 10 pixels
self._open_track(x, track_width) self._open_track(x, track_width)
x += track_width x += track_width
@ -307,6 +310,8 @@ class Display(object):
elif event.type == pygame.KEYDOWN: elif event.type == pygame.KEYDOWN:
self.focused_viewer.key_down( self.focused_viewer.key_down(
self, event.unicode, event.key, event.mod) self, event.unicode, event.key, event.mod)
# This is not UnicodeType. TODO does this need to be fixed?
# self, event.str, event.key, event.mod)
def _mouse_event(self, event): def _mouse_event(self, event):
V, x, y = self.at(*event.pos) V, x, y = self.at(*event.pos)

View File

@ -18,7 +18,9 @@
# along with Thun. If not see <http://www.gnu.org/licenses/>. # along with Thun. If not see <http://www.gnu.org/licenses/>.
# #
from __future__ import print_function from __future__ import print_function
from StringIO import StringIO from future import standard_library
standard_library.install_aliases()
from io import StringIO
import base64, zlib import base64, zlib

View File

@ -29,7 +29,9 @@ file, so you can just do, e.g.:
''' '''
from __future__ import print_function from __future__ import print_function
import base64, os, StringIO, zipfile from future import standard_library
standard_library.install_aliases()
import base64, os, io, zipfile
def initialize(joy_home): def initialize(joy_home):
@ -37,7 +39,7 @@ def initialize(joy_home):
def create_data(from_dir='./default_joy_home'): def create_data(from_dir='./default_joy_home'):
f = StringIO.StringIO() f = io.StringIO()
z = zipfile.ZipFile(f, mode='w') z = zipfile.ZipFile(f, mode='w')
for fn in os.listdir(from_dir): for fn in os.listdir(from_dir):
from_fn = os.path.join(from_dir, fn) from_fn = os.path.join(from_dir, fn)
@ -46,7 +48,7 @@ def create_data(from_dir='./default_joy_home'):
return base64.encodestring(f.getvalue()) return base64.encodestring(f.getvalue())
Z = zipfile.ZipFile(StringIO.StringIO(base64.decodestring('''\ Z = zipfile.ZipFile(io.StringIO(base64.decodestring('''\
UEsDBBQAAAAAAORmeE794BlRfgMAAH4DAAAPAAAAZGVmaW5pdGlvbnMudHh0c2VlX3N0YWNrID09 UEsDBBQAAAAAAORmeE794BlRfgMAAH4DAAAPAAAAZGVmaW5pdGlvbnMudHh0c2VlX3N0YWNrID09
IGdvb2Rfdmlld2VyX2xvY2F0aW9uIG9wZW5fc3RhY2sNCnNlZV9yZXNvdXJjZXMgPT0gbGlzdF9y IGdvb2Rfdmlld2VyX2xvY2F0aW9uIG9wZW5fc3RhY2sNCnNlZV9yZXNvdXJjZXMgPT0gbGlzdF9y
ZXNvdXJjZXMgZ29vZF92aWV3ZXJfbG9jYXRpb24gb3Blbl92aWV3ZXINCm9wZW5fcmVzb3VyY2Vf ZXNvdXJjZXMgZ29vZF92aWV3ZXJfbG9jYXRpb24gb3Blbl92aWV3ZXINCm9wZW5fcmVzb3VyY2Vf

View File

@ -26,6 +26,10 @@ Pulls everything together.
''' '''
from __future__ import print_function from __future__ import print_function
from __future__ import division
from past.builtins import execfile
from builtins import object
from past.utils import old_div
import os, sys, traceback import os, sys, traceback
import pygame import pygame
from joy.library import initialize, DefinitionWrapper, SimpleFunctionWrapper from joy.library import initialize, DefinitionWrapper, SimpleFunctionWrapper
@ -102,8 +106,8 @@ def init_context(screen, clock, pt):
*((144 - 89, 144, 89) if FULLSCREEN else (89, 144)) *((144 - 89, 144, 89) if FULLSCREEN else (89, 144))
) )
log = d.init_text(pt, 0, 0, 'log.txt') log = d.init_text(pt, 0, 0, 'log.txt')
tho = d.init_text(pt, 0, d.h / 3, 'menu.txt') tho = d.init_text(pt, 0, old_div(d.h, 3), 'menu.txt')
t = d.init_text(pt, d.w / 2, 0, 'scratch.txt') t = d.init_text(pt, old_div(d.w, 2), 0, 'scratch.txt')
loop = core.TheLoop(d, clock) loop = core.TheLoop(d, clock)
stack_id, stack_holder = pt.open('stack.pickle') stack_id, stack_holder = pt.open('stack.pickle')
world = core.World(stack_id, stack_holder, D, d.broadcast, log) world = core.World(stack_id, stack_holder, D, d.broadcast, log)

View File

@ -27,6 +27,7 @@ stack) to the git repo in the ``JOY_HOME`` directory.
''' '''
from __future__ import print_function from __future__ import print_function
from builtins import object
import os, pickle, traceback import os, pickle, traceback
from collections import Counter from collections import Counter
from dulwich.errors import NotGitRepository from dulwich.errors import NotGitRepository

View File

@ -1,75 +1,75 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright © 2019 Simon Forman # Copyright © 2019 Simon Forman
# #
# This file is part of Thun # This file is part of Thun
# #
# Thun is free software: you can redistribute it and/or modify # Thun is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or # the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# Thun is distributed in the hope that it will be useful, # Thun is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Thun. If not see <http://www.gnu.org/licenses/>. # along with Thun. If not see <http://www.gnu.org/licenses/>.
# #
''' '''
Stack Viewer Stack Viewer
================= =================
''' '''
from joy.utils.stack import expression_to_string, iter_stack from builtins import map, str
from joy.vui import core, text_viewer from joy.utils.stack import expression_to_string, iter_stack
from joy.vui import core, text_viewer
MAX_WIDTH = 64
MAX_WIDTH = 64
def fsi(item):
'''Format Stack Item''' def fsi(item):
if isinstance(item, tuple): '''Format Stack Item'''
res = '[%s]' % expression_to_string(item) if isinstance(item, tuple):
elif isinstance(item, str): res = '[%s]' % expression_to_string(item)
res = '"%s"' % item elif isinstance(item, str):
else: res = '"%s"' % item
assert not isinstance(item, unicode), repr(item) else:
res = str(item) res = str(item)
if len(res) > MAX_WIDTH: if len(res) > MAX_WIDTH:
return res[:MAX_WIDTH - 3] + '...' return res[:MAX_WIDTH - 3] + '...'
return res return res
class StackViewer(text_viewer.TextViewer): class StackViewer(text_viewer.TextViewer):
def __init__(self, surface): def __init__(self, surface):
super(StackViewer, self).__init__(surface) super(StackViewer, self).__init__(surface)
self.stack_holder = None self.stack_holder = None
self.content_id = 'stack viewer' self.content_id = 'stack viewer'
def _attach(self, display): def _attach(self, display):
if self.stack_holder: if self.stack_holder:
return return
om = core.OpenMessage(self, 'stack.pickle') om = core.OpenMessage(self, 'stack.pickle')
display.broadcast(om) display.broadcast(om)
if om.status != core.SUCCESS: if om.status != core.SUCCESS:
raise RuntimeError('stack unavailable') raise RuntimeError('stack unavailable')
self.stack_holder = om.thing self.stack_holder = om.thing
def _update(self): def _update(self):
self.lines[:] = map(fsi, iter_stack(self.stack_holder[0])) or [''] self.lines[:] = list(map(fsi, iter_stack(self.stack_holder[0]))) or ['']
def focus(self, display): def focus(self, display):
self._attach(display) self._attach(display)
super(StackViewer, self).focus(display) super(StackViewer, self).focus(display)
def handle(self, message): def handle(self, message):
if (isinstance(message, core.ModifyMessage) if (isinstance(message, core.ModifyMessage)
and message.subject is self.stack_holder and message.subject is self.stack_holder
): ):
self._update() self._update()
self.draw_body() self.draw_body()

View File

@ -24,6 +24,10 @@ Text Viewer
''' '''
from __future__ import print_function from __future__ import print_function
from __future__ import division
from builtins import object, range, str, zip
from past.builtins import basestring
from past.utils import old_div
import string import string
import pygame import pygame
from joy.utils.stack import expression_to_string from joy.utils.stack import expression_to_string
@ -96,8 +100,8 @@ class Font(object):
i = self.LOOKUP.index(ch) i = self.LOOKUP.index(ch)
except ValueError: except ValueError:
# render a lil box... # render a lil box...
r = (x + 1, self.line_h / 2 - 3, r = (x + 1, old_div(self.line_h, 2) - 3,
self.char_w - 2, self.line_h / 2) self.char_w - 2, old_div(self.line_h, 2))
pygame.draw.rect(surface, FG, r, 1) pygame.draw.rect(surface, FG, r, 1)
else: else:
iy, ix = divmod(i, 26) iy, ix = divmod(i, 26)
@ -217,8 +221,8 @@ class TextViewer(MenuViewer):
self.grow_rect = pygame.rect.Rect(1, 1, w, h) self.grow_rect = pygame.rect.Rect(1, 1, w, h)
self.body_surface = surface.subsurface(self.body_rect) self.body_surface = surface.subsurface(self.body_rect)
self.line_w = self.body_rect.w / FONT.char_w + 1 self.line_w = old_div(self.body_rect.w, FONT.char_w) + 1
self.h_in_lines = self.body_rect.h / FONT.line_h - 1 self.h_in_lines = old_div(self.body_rect.h, FONT.line_h) - 1
self.command_rect = self.command = None self.command_rect = self.command = None
self._sel_start = self._sel_end = None self._sel_start = self._sel_end = None
@ -249,7 +253,7 @@ class TextViewer(MenuViewer):
def draw_body(self): def draw_body(self):
MenuViewer.draw_body(self) MenuViewer.draw_body(self)
ys = xrange(0, self.body_rect.height, FONT.line_h) ys = range(0, self.body_rect.height, FONT.line_h)
ls = self.lines[self.at_line:self.at_line + self.h_in_lines + 2] ls = self.lines[self.at_line:self.at_line + self.h_in_lines + 2]
for y, line in zip(ys, ls): for y, line in zip(ys, ls):
self.draw_line(y, line) self.draw_line(y, line)
@ -310,7 +314,7 @@ class TextViewer(MenuViewer):
return return
r = self.command_rect = pygame.Rect( r = self.command_rect = pygame.Rect(
word_start * FONT.char_w, # x word_start * FONT.char_w, # x
y / FONT.line_h * FONT.line_h, # y old_div(y, FONT.line_h) * FONT.line_h, # y
len(word) * FONT.char_w, # w len(word) * FONT.char_w, # w
FONT.line_h # h FONT.line_h # h
) )
@ -334,7 +338,7 @@ class TextViewer(MenuViewer):
Given screen coordinates return the line, row, and column of the Given screen coordinates return the line, row, and column of the
character there. character there.
''' '''
row = self.at_line + y / FONT.line_h row = self.at_line + old_div(y, FONT.line_h)
try: try:
line = self.lines[row] line = self.lines[row]
except IndexError: except IndexError:
@ -342,7 +346,7 @@ class TextViewer(MenuViewer):
line = self.lines[row] line = self.lines[row]
column = len(line) column = len(line)
else: else:
column = min(x / FONT.char_w, len(line)) column = min(old_div(x, FONT.char_w), len(line))
return line, column, row return line, column, row
# Event Processing # Event Processing

View File

@ -24,6 +24,9 @@ Viewer
''' '''
from __future__ import print_function from __future__ import print_function
from __future__ import division
from builtins import chr, object
from past.utils import old_div
import pygame import pygame
from joy.vui.core import BACKGROUND, FOREGROUND from joy.vui.core import BACKGROUND, FOREGROUND
@ -242,5 +245,5 @@ class SomeViewer(MenuViewer):
def draw_a(surface, color=FOREGROUND, blend=False): def draw_a(surface, color=FOREGROUND, blend=False):
w, h = surface.get_width() - 2, surface.get_height() - 2 w, h = surface.get_width() - 2, surface.get_height() - 2
pygame.draw.aalines(surface, color, False, ( pygame.draw.aalines(surface, color, False, (
(1, h), (w / 2, 1), (w, h), (1, h / 2) (1, h), (old_div(w, 2), 1), (w, h), (1, old_div(h, 2))
), blend) ), blend)