Skip to content

Commit

Permalink
Merge pull request #255 from richrd/dev
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
richrd authored Jun 16, 2019
2 parents 2b83225 + 40f2e18 commit 4688543
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 25 deletions.
13 changes: 10 additions & 3 deletions suplemon/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, app):
self.defaults = {}
self.keymap = {}
self.config = {}
self.key_bindings = {}

def init(self):
self.create_config_dir()
Expand Down Expand Up @@ -66,9 +67,15 @@ def load_keys(self):
if not keymap:
self.logger.info("Failed to load keymap file '{0}'.".format(path))
return False
# Prepend the user keys to the defaults to give the user config a higher priority
keymap += self.keymap
self.keymap = self.normalize_keys(keymap)

# Build the key bindings
# User keymap overwrites the defaults in the bindings
self.keymap = self.normalize_keys(self.keymap + keymap)
self.key_bindings = {}
for binding in self.keymap:
for key in binding["keys"]:
self.key_bindings[key] = binding["command"]

return True

def normalize_keys(self, keymap):
Expand Down
2 changes: 1 addition & 1 deletion suplemon/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def move_up(self, delta=1):
:param int delta: How much to move. Defaults to 1.
"""
self.y -= 1
self.y -= delta
if self.y < 0:
self.y = 0
return
Expand Down
1 change: 1 addition & 0 deletions suplemon/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, app=None):
self.opened = time.time() # Time of last open
self.editor = None
self.writable = True
self.is_help = False

def _path(self):
"""Get the full path of the file."""
Expand Down
23 changes: 23 additions & 0 deletions suplemon/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,26 @@ def parse_path(path):
ab = os.path.abspath(path)
parts = os.path.split(ab)
return parts


def get_filename_cursor_pos(name):
default = {
"name": name,
"row": 0,
"col": 0,
}

m = re.match(r"(.*?):(\d+):?(\d+)?", name)

if not m:
return default

groups = m.groups()
if not groups[0]:
return default

return {
"name": groups[0],
"row": abs(int(groups[1])-1) if groups[1] else 0,
"col": abs(int(groups[2])-1) if groups[2] else 0,
}
52 changes: 33 additions & 19 deletions suplemon/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,7 @@ def get_file_index(self, file_obj):

def get_key_bindings(self):
"""Return the list of key bindings."""
bindings = {}
for binding in self.config.keymap:
for key in binding["keys"]:
bindings[key] = binding["command"]
return bindings
return self.config.key_bindings

def get_event_bindings(self):
"""Return the dict of event bindings."""
Expand Down Expand Up @@ -355,12 +351,23 @@ def handle_mouse(self, event):
###########################################################################

def help(self):
"""Open a new file with help text."""
f = self.default_file()
from . import help
f.set_data(help.help_text)
self.files.append(f)
self.switch_to_file(self.last_file_index())
"""Toggle the help document.
- If current document is help, close it.
- Otherwise, if help is open, switch to it.
- Otherwise, open a new file with help text.
"""
if self.get_file().is_help:
self.close_file()
else:
idx = next((i for i, f in enumerate(self.files) if f.is_help), -1)
if idx == -1:
f = self.default_file()
from . import help
f.set_data(help.help_text)
f.is_help = True
self.files.append(f)
idx = self.last_file_index()
self.switch_to_file(idx)

def new_file(self, path=None):
"""Open a new empty file.
Expand Down Expand Up @@ -596,15 +603,17 @@ def setup_editor(self, editor):

def open(self):
"""Ask for file name and try to open it."""
name = self.ui.query_file("Open file:")
if not name:
input_name = self.ui.query_file("Open file:")
if not input_name:
return False
name_row_col = helpers.get_filename_cursor_pos(input_name)
name = name_row_col["name"]
exists = self.file_is_open(name)
if exists:
self.switch_to_file(self.files.index(exists))
return True

if not self.open_file(name):
if not self.open_file(**name_row_col):
self.set_status("Failed to load '{0}'".format(name))
return False
self.switch_to_file(self.last_file_index())
Expand Down Expand Up @@ -688,32 +697,37 @@ def current_file_index(self):
"""Get index of current file."""
return self.current_file

def open_file(self, filename):
def open_file(self, name=None, row=0, col=0):
"""Open a file."""
file = File(self)
file.set_path(filename)
file.set_path(name)
file.set_editor(self.new_editor())
if not file.load():
return False
file.get_editor().set_single_cursor((col, row))
file.get_editor().scroll_to_line(row)
self.files.append(file)
return True

def load_files(self):
"""Try to load all files specified in arguments."""
if self.filenames:
for name in self.filenames:
for item in self.filenames:
name_row_col = helpers.get_filename_cursor_pos(item)
name = name_row_col["name"]
if os.path.isdir(name):
continue
# Avoid opening duplicate files
if self.file_is_open(name):
continue
if not self.open_file(name):
if not self.open_file(**name_row_col):
self.new_file(name)
# If nothing was loaded
if not self.files:
self.load_default()

def file_is_open(self, path):
"""Check if file is open. Returns the File object or False."""
"""Check if file is open. Returns a File object or False."""
for file in self.files:
if file.path() == os.path.abspath(path):
return file
Expand Down
4 changes: 4 additions & 0 deletions suplemon/modules/application_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def get_hash(self, editor):

def set_file_state(self, file, state):
"""Set the state of a file."""
cursor = file.editor.get_cursor()
# Don't set the cursor pos if it's not the default 0,0
if cursor.x or cursor.y:
return
file.editor.set_cursors(state["cursors"])
file.editor.scroll_pos = state["scroll_pos"]

Expand Down
14 changes: 13 additions & 1 deletion suplemon/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
from .line import Line


# Python 2 compatibility
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError


class Prompt(Editor):
"""An input prompt based on the Editor."""
def __init__(self, app, window):
Expand Down Expand Up @@ -298,7 +305,12 @@ def get_path_contents(self, path):
path = os.path.dirname(os.path.expanduser(path))
# If we get an empty path use the current directory
if not path:
path = os.getcwd()
try:
path = os.getcwd()
except FileNotFoundError:
# This might happen if the cwd has been
# removed after starting suplemon.
return []
# In case we don't have sufficent permissions
try:
contents = os.listdir(path)
Expand Down
2 changes: 1 addition & 1 deletion suplemon/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InputEvent:
"""Represents a keyboard or mouse event."""
def __init__(self):
self.type = None # 'key' or 'mouse'
self.key_name = None
self.key_name = ""
self.key_code = None
self.is_typeable = False
self.curses_key_name = None
Expand Down

0 comments on commit 4688543

Please sign in to comment.