Skip to content

Add ls option #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'
17 changes: 16 additions & 1 deletion tmuxp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

logger = logging.getLogger(__name__)

VALID_CONFIG_DIR_FILE_EXTENSIONS = ['.yaml', '.yml', '.json']


def get_cwd():
return os.getcwd()
Expand Down Expand Up @@ -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)
]
Expand Down Expand Up @@ -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)