Skip to content

Commit

Permalink
fix: fix urwid display error (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiko Nickerl authored Aug 6, 2021
1 parent c4bf70e commit eb89c43
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 63 deletions.
6 changes: 3 additions & 3 deletions sodalite/core/action.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Callable

import sodalite.ui.mmode
from sodalite.core import config, action_def
from sodalite.core.action_def import ActionName
from sodalite.ui.mmode import Mode
from sodalite.ui import viewmodel
from sodalite.ui.viewmodel import Mode


class Action:
Expand All @@ -17,7 +17,7 @@ def __init__(self, name: ActionName, action: Callable[[], None], modes: list[Mod

def handle(self, key: str) -> bool:
if key == self.keybinding:
if self.is_global or sodalite.ui.mmode.global_mode in self.modes:
if self.is_global or viewmodel.global_mode in self.modes:
self.action()
return True
return False
Expand Down
2 changes: 1 addition & 1 deletion sodalite/core/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from sodalite.core import config
from sodalite.core.config import HooksConfig
from sodalite.ui import notify

if TYPE_CHECKING:
from sodalite.core.entry import Entry
Expand Down Expand Up @@ -38,6 +37,7 @@ def trigger(self, entry: 'Entry') -> None:
result = os.system(f"( {self.action} ) > /dev/tty < /dev/tty")
logger.info(f"Result is {result}")
if result != 0:
from sodalite.ui import notify
notify.show_error(f"command failed with exit code {result}")
from sodalite.ui import graphics
if self.finally_exit:
Expand Down
24 changes: 11 additions & 13 deletions sodalite/ui/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@

import pyperclip

import sodalite.ui.mmode
from sodalite.core import key as key_module, hook, buffer, operate, Navigator
from sodalite.core.action import Action
from sodalite.core.action_def import ActionName
from sodalite.core.entry import Entry
from sodalite.core.key import Key
from sodalite.ui import graphics, notify
from sodalite.ui import graphics, viewmodel, notify
from sodalite.ui.entrylist import EntryList
from sodalite.ui.mmode import Mode
from sodalite.ui.viewmodel import ViewModel
from sodalite.ui.viewmodel import Mode, ViewModel
from sodalite.util import pubsub

if TYPE_CHECKING:
Expand Down Expand Up @@ -118,17 +116,17 @@ def abort(self) -> None:
graphics.exit()

def enter_navigate_mode(self) -> None:
sodalite.ui.mmode.global_mode.mode = Mode.NAVIGATE
viewmodel.global_mode.mode = Mode.NAVIGATE
self.list.render(self.list_size, True)

def enter_assign_mode(self) -> None:
if self.model.current_entry.is_dir:
sodalite.ui.mmode.global_mode.mode = Mode.ASSIGN_CHOOSE_ENTRY
viewmodel.global_mode.mode = Mode.ASSIGN_CHOOSE_ENTRY
self.list.render(self.list_size, True)

def enter_operate_mode(self) -> None:
if self.model.current_entry.is_dir:
sodalite.ui.mmode.global_mode.mode = Mode.OPERATE
viewmodel.global_mode.mode = Mode.OPERATE
self.list.render(self.list_size, True)

def trigger_filter(self) -> None:
Expand Down Expand Up @@ -244,25 +242,25 @@ def __init__(self, frame: 'MainFrame'):
self.actions = actions

def handle_key_individually(self, key):
if sodalite.ui.mmode.global_mode == Mode.ASSIGN_CHOOSE_ENTRY and self.navigator.is_navigation_key(key):
if viewmodel.global_mode == Mode.ASSIGN_CHOOSE_ENTRY and self.navigator.is_navigation_key(key):
self.choose_entry(key)
return True
elif sodalite.ui.mmode.global_mode == Mode.ASSIGN_CHOOSE_KEY and key_module.is_navigation_key(key):
elif viewmodel.global_mode == Mode.ASSIGN_CHOOSE_KEY and key_module.is_navigation_key(key):
self.assign_key(key)
return True

def exit(self) -> None:
if sodalite.ui.mmode.global_mode == Mode.ASSIGN_CHOOSE_ENTRY:
sodalite.ui.mmode.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY
if viewmodel.global_mode == Mode.ASSIGN_CHOOSE_ENTRY:
viewmodel.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY

def choose_entry(self, key: str) -> None:
if key in key_module.get_all_keys():
entry = self.navigator.current_entry.get_child_for_key(Key(key))
assert entry
self.list.select(entry)
sodalite.ui.mmode.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY
viewmodel.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY
elif key == 'enter':
sodalite.ui.mmode.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY
viewmodel.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY

def assign_key(self, key: str):
if key_module.is_navigation_key(key):
Expand Down
3 changes: 1 addition & 2 deletions sodalite/ui/entrylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import urwid
from urwid import AttrSpec, ListBox

import sodalite.ui.mmode
from sodalite.core.entry import Entry
from sodalite.core.navigate import Navigator
from sodalite.ui import theme, graphics, viewmodel
Expand Down Expand Up @@ -83,7 +82,7 @@ def render(self, size, focus=False):
caption = " " + key + " "
text = self.entry.name
display = caption + text
if focus and sodalite.ui.mmode.global_mode in viewmodel.ANY_ASSIGN_MODE:
if focus and viewmodel.global_mode in viewmodel.ANY_ASSIGN_MODE:
color = AttrSpec(self.color.foreground + ',standout', self.color.background, colors=16)
else:
color = self.color
Expand Down
3 changes: 1 addition & 2 deletions sodalite/ui/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from sodalite.ui.help import HelpLauncher
from sodalite.ui.hookbox import HookBox
from sodalite.ui.mainpane import MainPane
from sodalite.ui.viewmodel import ViewModel
from sodalite.ui.mmode import Mode
from sodalite.ui.viewmodel import ViewModel, Mode
from sodalite.util import env, pubsub

logger = logging.getLogger(__name__)
Expand Down
32 changes: 0 additions & 32 deletions sodalite/ui/mmode.py

This file was deleted.

11 changes: 4 additions & 7 deletions sodalite/ui/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import urwid
from urwid import AttrSpec

import sodalite.ui.mmode
from sodalite.ui import theme, mmode
from sodalite.ui.mmode import Mode
from sodalite.ui import graphics, theme, viewmodel
from sodalite.ui.viewmodel import Mode
from sodalite.util import pubsub

txt = urwid.AttrMap(urwid.Text('', align='center'), urwid.DEFAULT)
Expand Down Expand Up @@ -39,7 +38,6 @@ def show_error(message: str, duration: float = 1.5) -> None:
def _show(message: Union[str, Tuple[AttrSpec, str]], duration: float) -> None:
global _last_message
global _original_footer
from sodalite.ui import graphics
frame = graphics.frame
if _notify_lock.locked() and _last_message == message:
return
Expand All @@ -59,16 +57,15 @@ def _show(message: Union[str, Tuple[AttrSpec, str]], duration: float) -> None:


def clear() -> None:
from sodalite.ui import graphics
if graphics.frame and graphics.frame.footer == _notify_box:
graphics.frame.footer = None
graphics.draw_screen()


def trigger_notifications(mode: Mode) -> None:
if mmode.global_mode == Mode.ASSIGN_CHOOSE_ENTRY:
if viewmodel.global_mode == Mode.ASSIGN_CHOOSE_ENTRY:
show("choose entry", duration=0)
elif sodalite.ui.mmode.global_mode == Mode.ASSIGN_CHOOSE_KEY:
elif viewmodel.global_mode == Mode.ASSIGN_CHOOSE_KEY:
show("choose new key", duration=0)
else:
clear()
Expand Down
2 changes: 1 addition & 1 deletion sodalite/ui/theme.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import urwid
from pygments import token

from sodalite.ui.mmode import Mode
from sodalite.ui.viewmodel import Mode
from sodalite.util import pubsub

file = urwid.WHITE
Expand Down
32 changes: 31 additions & 1 deletion sodalite/ui/viewmodel.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
import logging
import re
import sre_constants
from enum import Enum
from re import Pattern
from typing import Optional

from sodalite.core.entry import Entry
from sodalite.ui import highlighting
from sodalite.ui.highlighting import HighlightedLine
from sodalite.ui.mmode import Mode
from sodalite.util import pubsub

logger = logging.getLogger(__name__)


class Mode(Enum):
NAVIGATE = 1
ASSIGN_CHOOSE_ENTRY = 2
ASSIGN_CHOOSE_KEY = 3
OPERATE = 4


ANY_ASSIGN_MODE = (Mode.ASSIGN_CHOOSE_KEY, Mode.ASSIGN_CHOOSE_ENTRY)


class GlobalMode:

def __init__(self):
super().__init__()
self.mode = Mode.NAVIGATE

@property
def mode(self):
return self._mode

@mode.setter
def mode(self, mode: Mode):
self._mode = mode
pubsub.mode_send(self._mode)

def __eq__(self, other):
return self._mode == other or super.__eq__(self, other)


global_mode = GlobalMode()


class ViewModel:

def __init__(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion sodalite/util/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

if TYPE_CHECKING:
from sodalite.core.entry import Entry
from sodalite.ui.mmode import Mode
from sodalite.ui.viewmodel import Mode
from sodalite.ui.highlighting import HighlightedLine


Expand Down

0 comments on commit eb89c43

Please sign in to comment.