Skip to content

Commit

Permalink
Merge pull request #127 from richrd/refactoring
Browse files Browse the repository at this point in the history
Lots of new goodies from refactoring branch
  • Loading branch information
richrd committed Mar 13, 2016
2 parents 63ee84a + 1ef4cde commit e5db54c
Show file tree
Hide file tree
Showing 19 changed files with 850 additions and 606 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ http://github.com/richrd/suplemon
![Suplemon in action](http://bittemple.org/misc/suplemon/suplemon-demo.gif)


## Get it!
## Try it!

You can just clone the repo, and try Suplemon, or also install it system wide.

Expand Down Expand Up @@ -97,15 +97,18 @@ Suplemon is licensed under the MIT license.

## Configuration

### Main Config
The suplemon config file is stored at ```~/.config/suplemon/suplemon-config.json```.

The best way to edit it is to run the ```config``` command (Run commands via ```Ctrl+E```).
That way Suplemon will automatically reload the configuration when you save the file.
To view the default configuration and see what options are available run ```config defaults``` via ```Ctrl+E```.


### Keymap Config

## Keyboard shortcuts
Below are the default key mappings used in suplemon. They can be edited by running the ```keymap``` command.
To view the default keymap file run ```keymap default```

* <kbd>Ctrl</kbd> + <kbd>Q</kbd>
> Exit
Expand Down
93 changes: 74 additions & 19 deletions suplemon/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
import logging

from . import helpers
from . import suplemon_module


class Config:
def __init__(self, app):
self.app = app
self.logger = logging.getLogger(__name__)
self.default_filename = "defaults.json"
self.default_config_filename = "defaults.json"
self.default_keymap_filename = "keymap.json"
self.config_filename = "suplemon-config.json"
self.keymap_filename = "suplemon-keymap.json"
self.home_dir = os.path.expanduser("~")
self.fpath = os.path.join(self.home_dir, ".config", "suplemon")

self.defaults = {}
self.keymap = {}
self.config = {}

def init(self):
Expand All @@ -29,6 +33,9 @@ def init(self):
def path(self):
return os.path.join(self.fpath, self.config_filename)

def keymap_path(self):
return os.path.join(self.fpath, self.keymap_filename)

def set_path(self, path):
parts = os.path.split(path)
self.fpath = parts[0]
Expand All @@ -48,17 +55,43 @@ def load(self):
self.logger.info("Failed to load config file '{0}'.".format(path))
self.config = dict(self.defaults)
return False
self.load_keys()
return config

def load_defaults(self):
path = os.path.join(self.app.path, "config", self.default_filename)
def load_keys(self):
path = self.keymap_path()
keymap = False
if not os.path.exists(path):
self.logger.debug("Keymap file '{0}' doesn't exist.".format(path))
return False
keymap = self.load_config_file(path)
if not keymap:
self.logger.info("Failed to load keymap file '{0}'.".format(path))
return False
defaults = self.load_config_file(path)
if not defaults:
self.logger.warning("Failed to load default config file! ('{0}')".format(path))
self.keymap += keymap # Append the user key map
return True

def load_defaults(self):
if not self.load_default_config() or not self.load_default_keys():
return False
return True

def load_default_config(self):
path = os.path.join(self.app.path, "config", self.default_config_filename)
config = self.load_config_file(path)
if not config:
self.logger.error("Failed to load default config file '{0}'!".format(path))
return False
self.defaults = config
return True

def load_default_keys(self):
path = os.path.join(self.app.path, "config", self.default_keymap_filename)
config = self.load_config_file(path)
if not config:
self.logger.error("Failed to load default keymap file '{0}'!".format(path))
return False
self.defaults = defaults
self.keymap = config
return True

def reload(self):
Expand All @@ -82,20 +115,8 @@ def merge_defaults(self, config):
for sec_key in curr_item.keys():
if sec_key not in config[prim_key].keys():
config[prim_key][sec_key] = curr_item[sec_key]
self.merge_keys(config)
return config

def merge_keys(self, config):
"""Fill in config with default keys."""
# Do merge for app and editor keys
for dest in ["app", "editor"]:
key_config = config[dest]["keys"]
key_defaults = self.defaults[dest]["keys"]
for key in key_defaults.keys():
# Fill in each key that's not defined yet
if key not in key_config.keys():
key_config[key] = key_defaults[key]

def load_config_file(self, path):
try:
f = open(path)
Expand Down Expand Up @@ -147,3 +168,37 @@ def __str__(self):
def __len__(self):
"""Return length of top level config variables."""
return len(self.config)


class ConfigModule(suplemon_module.Module):
"""Helper for shortcut for openning config files."""
def init(self):
self.conf_name = "defaults.json"
self.conf_default_path = os.path.join(self.app.path, "config", self.conf_name)
self.conf_user_path = self.app.config.path()

def run(self, app, editor, args):
if args == "defaults":
# Open the default config in a new file only for viewing
self.open(app, self.conf_default_path, read_only=True)
else:
self.open(app, self.conf_user_path)

def open(self, app, path, read_only=False):
if read_only:
f = open(path)
data = f.read()
f.close()
file = app.new_file()
file.set_name(self.conf_name)
file.set_data(data)
app.switch_to_file(app.last_file_index())
else:
# Open the user config file for editing
f = app.file_is_open(path)
if f:
app.switch_to_file(app.get_file_index(f))
else:
if not app.open_file(path):
app.new_file(path)
app.switch_to_file(app.last_file_index())
69 changes: 7 additions & 62 deletions suplemon/config/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,7 @@
// How long curses will wait to detect ESC key
"escdelay": 50,
// Wether to use special unicode symbols for decoration
"use_unicode_symbols": true,
// Key bindings for app functions
"keys": {
"ctrl+h": "help",
"ctrl+s": "save_file",
"ctrl+e": "run_command",
"ctrl+f": "find",
"ctrl+g": "go_to",
"ctrl+o": "open",
"ctrl+w": "close_file",
"ctrl+n": "new_file",
"ctrl+q": "ask_exit",
"ctrl+p": "comment",
"ctrl+pageup": "next_file",
"ctrl+pagedown": "prev_file",
"f1": "save_file_as",
"f2": "reload_file",
"f7": "toggle_whitespace",
"f8": "toggle_mouse",
"f11": "toggle_fullscreen"
}
"use_unicode_symbols": true
},
// Editor settings
"editor": {
Expand Down Expand Up @@ -78,6 +58,10 @@
},
// Wether to visually show white space chars
"show_white_space": false,
// Show tab indicators in whitespace
"show_tab_indicators": true,
// Tab indicator charatrer
"tab_indicator_character": "\u203A",
// Line numbering
"show_line_nums": true,
// Naive line highlighting
Expand All @@ -91,46 +75,7 @@
// Wether to use copy/paste across multiple files
"use_global_buffer": true,
// Find with regex by default
"regex_find": false,
// Key bindings for editor functions
"keys": {
"up": "arrow_up",
"down": "arrow_down",
"left": "arrow_left",
"right": "arrow_right",
"enter": "enter",
"backspace": "backspace",
"delete": "delete",
"insert": "insert",
"tab": "tab",
"shift+tab": "untab",
"home": "home",
"end": "end",
"escape": "escape",
"pageup": "page_up",
"pagedown": "page_down",
"f5": "undo",
"f6": "redo",
"f9": "toggle_line_nums",
"f10": "toggle_line_ends",
"f11": "toggle_highlight",
"alt+up": "new_cursor_up",
"alt+down": "new_cursor_down",
"alt+left": "new_cursor_left",
"alt+right": "new_cursor_right",
"alt+pageup": "push_up",
"alt+pagedown": "push_down",
"ctrl+c": "copy",
"ctrl+x": "cut",
"ctrl+k": "duplicate_line",
"ctrl+v": "insert",
"ctrl+d": "find_next",
"ctrl+a": "find_all",
"ctrl+left": "jump_left",
"ctrl+right": "jump_right",
"ctrl+up": "jump_up",
"ctrl+down": "jump_down"
}
"regex_find": false
},
// UI Display Settings
"display": {
Expand All @@ -148,6 +93,6 @@
// Show the bottom status bar
"show_bottom_bar": true,
// Invert status bar colors (switch text and background colors)
"invert_status_bars": true
"invert_status_bars": false
}
}
63 changes: 63 additions & 0 deletions suplemon/config/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Suplemon Default Key Map

// This file contains the default key map for Suplemon and should not be edited.
// If the file doesn't exist, or if it has errors Suplemon can't run.
// Suplemon supports single line comments in JSON as seen here.

[
// App
{"keys": ["ctrl+h"], "command": "help"},
{"keys": ["ctrl+s"], "command": "save_file"},
{"keys": ["ctrl+e"], "command": "run_command"},
{"keys": ["ctrl+f"], "command": "find"},
{"keys": ["ctrl+g"], "command": "go_to"},
{"keys": ["ctrl+o"], "command": "open"},
{"keys": ["ctrl+w"], "command": "close_file"},
{"keys": ["ctrl+n"], "command": "new_file"},
{"keys": ["ctrl+q"], "command": "ask_exit"},
{"keys": ["ctrl+p"], "command": "comment"},
{"keys": ["ctrl+pageup"], "command": "next_file"},
{"keys": ["ctrl+pagedown"], "command": "prev_file"},
{"keys": ["f1"], "command": "save_file_as"},
{"keys": ["f2"], "command": "reload_file"},
{"keys": ["f7"], "command": "toggle_whitespace"},
{"keys": ["f8"], "command": "toggle_mouse"},
{"keys": ["f11"], "command": "toggle_fullscreen"},
// Editor
{"keys": ["up"], "command": "arrow_up"},
{"keys": ["down"], "command": "arrow_down"},
{"keys": ["left"], "command": "arrow_left"},
{"keys": ["right"], "command": "arrow_right"},
{"keys": ["enter"], "command": "enter"},
{"keys": ["backspace"], "command": "backspace"},
{"keys": ["delete"], "command": "delete"},
{"keys": ["insert"], "command": "insert"},
{"keys": ["tab"], "command": "tab"},
{"keys": ["shift+tab"], "command": "untab"},
{"keys": ["home"], "command": "home"},
{"keys": ["end"], "command": "end"},
{"keys": ["escape"], "command": "escape"},
{"keys": ["pageup"], "command": "page_up"},
{"keys": ["pagedown"], "command": "page_down"},
{"keys": ["ctrl+z", "f5"], "command": "undo"},
{"keys": ["ctrl+y", "f6"], "command": "redo"},
{"keys": ["f9"], "command": "toggle_line_nums"},
{"keys": ["f10"], "command": "toggle_line_ends"},
{"keys": ["f11"], "command": "toggle_highlight"},
{"keys": ["alt+up"], "command": "new_cursor_up"},
{"keys": ["alt+down"], "command": "new_cursor_down"},
{"keys": ["alt+left"], "command": "new_cursor_left"},
{"keys": ["alt+right"], "command": "new_cursor_right"},
{"keys": ["alt+pageup"], "command": "push_up"},
{"keys": ["alt+pagedown"], "command": "push_down"},
{"keys": ["ctrl+c"], "command": "copy"},
{"keys": ["ctrl+x"], "command": "cut"},
{"keys": ["ctrl+k"], "command": "duplicate_line"},
{"keys": ["ctrl+v"], "command": "insert"},
{"keys": ["ctrl+d"], "command": "find_next"},
{"keys": ["ctrl+a"], "command": "find_all"},
{"keys": ["ctrl+left"], "command": "jump_left"},
{"keys": ["ctrl+right"], "command": "jump_right"},
{"keys": ["ctrl+up"], "command": "jump_up"},
{"keys": ["ctrl+down"], "command": "jump_down"}
]
3 changes: 3 additions & 0 deletions suplemon/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def __ne__(self, item):
def __str__(self):
return "Cursor({x},{y})".format(x=self.x, y=self.y)

def __repr__(self):
return self.__str__()

def tuple(self):
"""Return the cursor as a tuple.
Expand Down
Loading

0 comments on commit e5db54c

Please sign in to comment.