snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit 72b289f0d0452e5667f8f92b7b977fc1171b253a
parent 19c27c565ce805726b898ccab627b4655b74eb8f
Author: Josh Moore <jxm5210@g.rit.edu>
Date:   Thu, 11 Jun 2020 03:30:02 -0400

Merge pull request #105 from callmepavo/snaketerm-hotkeys

snaketerm reads hotkey config from file
Diffstat:
Asnakewm/apps/tools/snaketerm/default_hotkeys.json | 9+++++++++
Msnakewm/apps/tools/snaketerm/term.py | 33+++++++++++++++++++++++----------
Asnakewm/apps/tools/snaketerm/user_hotkeys.json | 9+++++++++
3 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/snakewm/apps/tools/snaketerm/default_hotkeys.json b/snakewm/apps/tools/snaketerm/default_hotkeys.json @@ -0,0 +1,8 @@ +{ + "ctrl" : { + "l" : "clear_text", + "left" : "jump_left", + "right" : "jump_right", + "backspace" : "jump_backspace" + } +}+ \ No newline at end of file diff --git a/snakewm/apps/tools/snaketerm/term.py b/snakewm/apps/tools/snaketerm/term.py @@ -8,6 +8,9 @@ import pygame_gui from pygame_gui.elements import UITextBox +import os +import json + class SnakeTerm(pygame_gui.elements.UIWindow): def __init__(self, pos, manager): @@ -55,15 +58,25 @@ class SnakeTerm(pygame_gui.elements.UIWindow): # jump attributes self.jump_chars = (" ", "-", "_", "/") - # TODO: load this from a file so users can configure - self.hotkeys = { - "ctrl": { - "l": self.clear_text, - "left": self.jump_left, - "right": self.jump_right, - "backspace": self.jump_backspace, - } - } + self.hotkeys = self.get_hotkeys() + + def get_hotkeys(self): + current_dir = os.path.dirname(os.path.abspath(__file__)) + try: # first attmept to load user hotkeys + with open(current_dir + "/user_hotkeys.json", "r") as f: + raw_json = json.load(f) + except: # if that file doesn't exist use default + with open(current_dir + "/default_hotkeys.json", "r") as f: + raw_json = json.load(f) + # run through each value and add "self." then run through eval() + key_config = {} + for mod_key in raw_json.keys(): + key_config[mod_key] = {} + for normal_key in raw_json[mod_key].keys(): + key_config[mod_key][normal_key] = eval( + "self." + raw_json[mod_key][normal_key] + ) + return key_config def set_text(self, text): self.textbox.html_text = text.replace("\n", "<br>") @@ -123,7 +136,7 @@ class SnakeTerm(pygame_gui.elements.UIWindow): self.history[self.histindex + increment] self.histindex += increment except IndexError: - return self.histindex + pass return self.histindex def cache_command(self): diff --git a/snakewm/apps/tools/snaketerm/user_hotkeys.json b/snakewm/apps/tools/snaketerm/user_hotkeys.json @@ -0,0 +1,8 @@ +{ + "ctrl" : { + "l" : "clear_text", + "left" : "jump_left", + "right" : "jump_right", + "backspace" : "jump_backspace" + } +}+ \ No newline at end of file