diff --git a/tests/test_cli.py b/tests/test_cli.py index 2e94efa1518..1d5882fe5ce 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -13,7 +13,13 @@ import libtmux from libtmux.common import has_lt_version from tmuxp import cli, config -from tmuxp.cli import get_config_dir, is_pure_name, load_workspace, scan_config +from tmuxp.cli import ( + command_ls, + get_config_dir, + is_pure_name, + load_workspace, + scan_config, +) from .fixtures._util import curjoin, loadfixture @@ -574,3 +580,33 @@ def check_cmd(config_arg): assert str(user_config) in check_cmd(str(configdir.join('myconfig.yaml'))) assert 'file not found' in check_cmd('.tmuxp.json') + + +def test_ls_cli(monkeypatch, tmpdir): + monkeypatch.setenv("HOME", str(tmpdir)) + + filenames = [ + '.git/', + '.gitignore/', + 'session_1.yaml', + 'session_2.yaml', + 'session_3.json', + 'session_4.txt', + ] + + # should ignore: + # - directories should be ignored + # - extensions not covered in VALID_CONFIG_DIR_FILE_EXTENSIONS + ignored_filenames = ['.git/', '.gitignore/', 'session_4.txt'] + stems = [os.path.splitext(f)[0] for f in filenames if f not in ignored_filenames] + + for filename in filenames: + location = tmpdir.join('.tmuxp/{}'.format(filename)) + if filename.endswith('/'): + location.ensure_dir() + else: + location.ensure() + + runner = CliRunner() + cli_output = runner.invoke(command_ls).output + assert cli_output == '\n'.join(stems) + '\n' diff --git a/tmuxp/cli.py b/tmuxp/cli.py index 123a1f67f8e..f724a765afa 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -26,6 +26,8 @@ logger = logging.getLogger(__name__) +VALID_CONFIG_DIR_FILE_EXTENSIONS = ['.yaml', '.yml', '.json'] + def get_cwd(): return os.getcwd() @@ -326,7 +328,7 @@ def scan_config(config, config_dir=None): x for x in [ '%s%s' % (join(config_dir, config), ext) - for ext in ['.yaml', '.yml', '.json'] + for ext in VALID_CONFIG_DIR_FILE_EXTENSIONS ] if exists(x) ] @@ -924,3 +926,16 @@ def command_convert(config): buf.write(newconfig) buf.close() print('New config saved to <%s>.' % newfile) + + +@cli.command( + name='ls', short_help='List configured sessions in {}.'.format(get_config_dir()) +) +def command_ls(): + tmuxp_dir = get_config_dir() + if os.path.exists(tmuxp_dir) and os.path.isdir(tmuxp_dir): + for f in sorted(os.listdir(tmuxp_dir)): + stem, ext = os.path.splitext(f) + if os.path.isdir(f) or ext not in VALID_CONFIG_DIR_FILE_EXTENSIONS: + continue + print(stem)