diff --git a/jrnl/install.py b/jrnl/install.py index ced4bba6..9dff5a1a 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -12,14 +12,9 @@ from . import __version__ 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" @@ -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}): " @@ -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] diff --git a/tests/test_install.py b/tests/test_install.py new file mode 100644 index 00000000..e24d3b9c --- /dev/null +++ b/tests/test_install.py @@ -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