Move init_text() to Text method.

This commit is contained in:
Simon Forman 2018-07-14 19:11:49 -07:00
parent a84966cf9b
commit 6fe83256b1
2 changed files with 27 additions and 42 deletions

View File

@ -10,9 +10,6 @@ Joypy - Copyright © 2018 Simon Forman
import os, pickle, sys import os, pickle, sys
from textwrap import dedent from textwrap import dedent
from dulwich.errors import NotGitRepository
from dulwich.repo import Repo
from joy.gui.textwidget import TextViewerWidget, tk, get_font, TEXT_BINDINGS from joy.gui.textwidget import TextViewerWidget, tk, get_font, TEXT_BINDINGS
from joy.gui.utils import init_home, FileFaker from joy.gui.utils import init_home, FileFaker
from joy.gui.world import World from joy.gui.world import World
@ -50,30 +47,17 @@ class StackDisplayWorld(World):
with open(STACK_FN, 'wb') as f: with open(STACK_FN, 'wb') as f:
os.chmod(STACK_FN, 0600) os.chmod(STACK_FN, 0600)
pickle.dump(self.stack, f) pickle.dump(self.stack, f)
f.flush() f.flush()from dulwich.errors import NotGitRepository
from dulwich.repo import Repo
os.fsync(f.fileno()) os.fsync(f.fileno())
repo.stage([self.relative_STACK_FN]) repo.stage([self.relative_STACK_FN])
commit_id = repo.do_commit( commit_id = repo.do_commit(
'message', 'message',
committer='Simon Forman <forman.simon@gmail.com>', committer='Simon Forman <forman.simon@gmail.com>',
) )
#print >> sys.stderr, commit_id print >> sys.stderr, commit_id
def init_text(t, title, filename):
t.winfo_toplevel().title(title)
if os.path.exists(filename):
with open(filename) as f:
data = f.read()
t.insert(tk.END, data)
# Prevent this from triggering a git commit.
t.update()
t._cancelSave()
t.pack(expand=True, fill=tk.BOTH)
t.filename = filename
t.repo_relative_filename = repo_relative_path(filename)
t.repo = repo
t['font'] = FONT # See below.
def key_bindings(*args): def key_bindings(*args):
@ -148,8 +132,6 @@ tb.update({
# '<F-->': lambda tv: tv.pastecut, # '<F-->': lambda tv: tv.pastecut,
'<F6>': lambda tv: tv.copyto, '<F6>': lambda tv: tv.copyto,
}) })
defaults = dict(text_bindings=tb, width=80, height=25) defaults = dict(text_bindings=tb, width=80, height=25)
@ -162,30 +144,18 @@ for func in (
mouse_bindings, mouse_bindings,
): ):
D[func.__name__] = func D[func.__name__] = func
stack = load_stack() stack = load_stack()
if stack is None: if stack is None:
w = StackDisplayWorld(dictionary=D) world = StackDisplayWorld(dictionary=D)
else: else:
w = StackDisplayWorld(stack=stack, dictionary=D) world = StackDisplayWorld(stack=stack, dictionary=D)
t = TextViewerWidget(world, **defaults)
t = TextViewerWidget(w, **defaults)
log_window = tk.Toplevel() log_window = tk.Toplevel()
log_window.protocol("WM_DELETE_WINDOW", log_window.withdraw) log_window.protocol("WM_DELETE_WINDOW", log_window.withdraw)
log = TextViewerWidget(w, log_window, **defaults) log = TextViewerWidget(world, log_window, **defaults)
FONT = get_font('Iosevka', size=14) # Requires Tk root already set up. FONT = get_font('Iosevka', size=14) # Requires Tk root already set up.
log.init('Log', LOG_FN, repo_relative_path(LOG_FN), repo, FONT)
t.init('Joy - ' + JOY_HOME, JOY_FN, repo_relative_path(JOY_FN), repo, FONT)
init_text(log, 'Log', LOG_FN)
init_text(t, 'Joy - ' + JOY_HOME, JOY_FN)
GLOBAL_COMMANDS = { GLOBAL_COMMANDS = {
@ -207,7 +177,7 @@ GLOBAL_COMMANDS = {
'<Control-Shift-Delete>': 'popd', '<Control-Shift-Delete>': 'popd',
} }
for event, command in GLOBAL_COMMANDS.items(): for event, command in GLOBAL_COMMANDS.items():
t.bind_all(event, lambda _, _command=command: w.interpret(_command)) t.bind_all(event, lambda _, _command=command: world.interpret(_command))
def main(): def main():

View File

@ -399,6 +399,21 @@ class TextViewerWidget(tk.Text, MouseBindingsMixin, SavingMixin):
return 'break' return 'break'
def init(self, title, filename, repo_relative_filename, repo, font):
self.winfo_toplevel().title(title)
if os.path.exists(filename):
with open(filename) as f:
data = f.read()
self.insert(tk.END, data)
# Prevent this from triggering a git commit.
self.update()
self._cancelSave()
self.pack(expand=True, fill=tk.BOTH)
self.filename = filename
self.repo_relative_filename = repo_relative_filename
self.repo = repo
self['font'] = font # See below.
def popupTB(self, tb): def popupTB(self, tb):
top = tk.Toplevel() top = tk.Toplevel()
T = TextViewerWidget( T = TextViewerWidget(