Move StackDisplayWorld into world.py

This commit is contained in:
Simon Forman 2018-07-14 19:51:31 -07:00
parent ea11fbebda
commit da03f60dca
2 changed files with 37 additions and 28 deletions

View File

@ -12,7 +12,7 @@ from textwrap import dedent
from joy.gui.textwidget import TextViewerWidget, tk, get_font, TEXT_BINDINGS
from joy.gui.utils import init_home, FileFaker
from joy.gui.world import World
from joy.gui.world import StackDisplayWorld
from joy.library import initialize
from joy.utils.stack import stack_to_string
@ -58,35 +58,11 @@ def repo_relative_path(path):
STACK_FN = os.path.join(JOY_HOME, 'stack.pickle')
REL_STACK_FN = repo_relative_path(STACK_FN)
JOY_FN = os.path.join(JOY_HOME, 'scratch.txt')
LOG_FN = os.path.join(JOY_HOME, 'log.txt')
class StackDisplayWorld(World):
relative_STACK_FN = repo_relative_path(STACK_FN)
def interpret(self, command):
print '\njoy?', command
super(StackDisplayWorld, self).interpret(command)
def print_stack(self):
print '\n%s <-' % stack_to_string(self.stack)
def save(self):
with open(STACK_FN, 'wb') as f:
os.chmod(STACK_FN, 0600)
pickle.dump(self.stack, f)
f.flush()
os.fsync(f.fileno())
repo.stage([self.relative_STACK_FN])
commit_id = repo.do_commit(
'message',
committer='Simon Forman <forman.simon@gmail.com>',
)
print >> sys.stderr, commit_id
def key_bindings(*args):
print dedent('''
Ctrl-Enter - Run the selection as Joy code.
@ -152,11 +128,13 @@ for func in (
mouse_bindings,
):
D[func.__name__] = func
stack = load_stack()
if stack is None:
world = StackDisplayWorld(dictionary=D)
world = StackDisplayWorld(repo, STACK_FN, REL_STACK_FN, dictionary=D)
else:
world = StackDisplayWorld(stack=stack, dictionary=D)
world = StackDisplayWorld(repo, STACK_FN, REL_STACK_FN, stack=stack, dictionary=D)
t = TextViewerWidget(world, **defaults)
log_window = tk.Toplevel()
log_window.protocol("WM_DELETE_WINDOW", log_window.withdraw)

View File

@ -17,6 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with joy.py. If not see <http://www.gnu.org/licenses/>.
#
import os, pickle, sys
from inspect import getdoc
from joy.joy import run
@ -95,3 +96,33 @@ class World(object):
self.text_widget.see(stack_out_index)
s = stack_to_string(self.stack) + '\n'
self.text_widget.insert(stack_out_index, s)
class StackDisplayWorld(World):
def __init__(self, repo, filename, rel_filename, stack=(), dictionary=None, text_widget=None):
World.__init__(self, stack, dictionary, text_widget)
self.repo = repo
self.filename = filename
self.relative_STACK_FN = rel_filename
def interpret(self, command):
print '\njoy?', command
super(StackDisplayWorld, self).interpret(command)
def print_stack(self):
print '\n%s <-' % stack_to_string(self.stack)
def save(self):
with open(self.filename, 'wb') as f:
os.chmod(self.filename, 0600)
pickle.dump(self.stack, f)
f.flush()
os.fsync(f.fileno())
self.repo.stage([self.relative_STACK_FN])
commit_id = self.repo.do_commit(
'message',
committer='Simon Forman <forman.simon@gmail.com>',
)
print >> sys.stderr, commit_id