Skip to content

Commit

Permalink
Check for readline module instead of Windows when initializing autoco…
Browse files Browse the repository at this point in the history
…mplete in install (#1104)
  • Loading branch information
micahellison authored Dec 4, 2020
1 parent bade28a commit 48cde1b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
20 changes: 11 additions & 9 deletions jrnl/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@
from .config import load_config
from .config import verify_config_colors
from .exception import UserAbort
from .os_compat import on_windows
from .prompt import yesno
from .upgrade import is_old_version

if not on_windows:
# readline is not included in Windows Active Python
import readline

DEFAULT_CONFIG_NAME = "jrnl.yaml"
DEFAULT_JOURNAL_NAME = "journal.txt"
DEFAULT_JOURNAL_KEY = "default"
Expand Down Expand Up @@ -127,10 +122,7 @@ def load_or_install_jrnl():


def install():
if not on_windows:
readline.set_completer_delims(" \t\n;")
readline.parse_and_bind("tab: complete")
readline.set_completer(_autocomplete_path)
_initialize_autocomplete()

# Where to create the journal?
path_query = f"Path to your journal file (leave blank for {JOURNAL_FILE_PATH}): "
Expand Down Expand Up @@ -159,6 +151,16 @@ def install():
return default_config


def _initialize_autocomplete():
# readline is not included in Windows Active Python and perhaps some other distributions
if sys.modules.get("readline"):
import readline

readline.set_completer_delims(" \t\n;")
readline.parse_and_bind("tab: complete")
readline.set_completer(_autocomplete_path)


def _autocomplete_path(text, state):
expansions = glob.glob(os.path.expanduser(os.path.expandvars(text)) + "*")
expansions = [e + "/" if os.path.isdir(e) else e for e in expansions]
Expand Down
13 changes: 13 additions & 0 deletions tests/test_install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from unittest import mock
import pytest
import sys


@pytest.mark.filterwarnings(
"ignore:.*imp module is deprecated.*"
) # ansiwrap spits out an unrelated warning
def test_initialize_autocomplete_runs_without_readline():
from jrnl import install

with mock.patch.dict(sys.modules, {"readline": None}):
install._initialize_autocomplete() # should not throw exception

0 comments on commit 48cde1b

Please sign in to comment.