Skip to content

Commit d0cd0ed

Browse files
committed
attach-session will attach-session if outside tmux, and switch-client if inside. Wire in kill-session to tmuxp kill-session.
1 parent a3f12ee commit d0cd0ed

File tree

4 files changed

+86
-42
lines changed

4 files changed

+86
-42
lines changed

CHANGES

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Here you can find the recent changes to tmuxp.
88

99
- [cli] zsh/bash/tcsh completion improvements for tab-completion options
1010
- [cli] tmuxp ``kill-session`` with tab-completion.
11-
- [cli] tmuxp ``attach-session`` with tab-completion.
11+
- [cli] tmuxp ``attach-session`` with tab-completion. Attach session will
12+
``switch-client`` for you if you are inside of of a tmux client.
1213
- [cli] tmuxp ``load`` for loading configs.
1314
- [tests] unit test fixes.
1415

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
tmuxp
33
-----
44
5-
A Pythonic ORM Toolkit for managing tmux(1) workspaces.
5+
Manage tmux workspaces from JSON and YAML, pythonic API, shell completion.
66
77
88
"""
@@ -39,7 +39,8 @@ def requirements(f):
3939
license='BSD',
4040
author='Tony Narlock',
4141
author_email='[email protected]',
42-
description='Manage and build tmux workspaces.',
42+
description='Manage tmux workspaces from JSON and YAML, pythonic API, '
43+
'shell completion',
4344
long_description=open('README.rst').read(),
4445
packages=['tmuxp', 'tmuxp.testsuite'],
4546
include_package_data=True,

tmuxp/cli.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def build_workspace(config_file, args):
137137
return
138138

139139

140-
141140
def subcommand_load(args):
142141
if args.list_configs:
143142
startup(config_dir)
@@ -177,42 +176,44 @@ def subcommand_load(args):
177176
build_workspace(file_user, args)
178177
else:
179178
logger.error('%s not found.' % configfile)
180-
else:
181-
parser.print_help()
182179

183180

184181
def subcommand_attach_session(args):
185-
#print('attac session')
186-
for session_name in args.session_name:
187-
print(session_name)
182+
commands = []
183+
try:
184+
ctext = args.session_name[0]
185+
except IndexError as e:
186+
return
188187

189-
def session_complete(command, commands, ctext):
190-
if ctext.startswith(command + ' '):
191-
commands[:] = []
192-
ctext_attach = ctext.replace(command + ' ', '')
188+
t = Server()
189+
try:
190+
session = [s for s in t.sessions if s.get('session_name') == ctext][0]
191+
except IndexError as e:
192+
print('Session not found.')
193+
return
193194

194-
sessions = [s.get('session_name') for s in t._sessions]
195-
commands.extend([c for c in sessions if ctext_attach in c])
195+
if 'TMUX' in os.environ:
196+
del os.environ['TMUX']
197+
session.switch_client()
198+
print('Inside tmux client, switching client.')
199+
else:
200+
session.attach_session()
201+
print('Attaching client.')
196202

197-
def subcommand_kill_session(args):
198-
#print('kill session')
199-
#print(args)
200-
#print(type(args.session_name))
201-
#print(args.session_name)
202203

204+
def subcommand_kill_session(args):
203205
commands = []
204206
ctext = args.session_name[0]
205207

206-
def session_complete(command, commands, ctext):
207-
if ctext.startswith(command + ' '):
208-
commands[:] = []
209-
ctext_attach = ctext.replace(command + ' ', '')
210-
211-
sessions = [s.get('session_name') for s in t._sessions]
212-
commands.extend([c for c in sessions if ctext_attach in c])
208+
t = Server()
209+
sessions = [s for s in t.sessions if s.get('session_name') == ctext]
213210

214-
print(' \n'.join(commands))
211+
if (len(sessions) == 1):
212+
proc = sessions[0].kill_session()
213+
if proc.stderr:
214+
print(proc.stderr)
215215

216+
print(proc.stdout)
216217

217218

218219
def cli_parser():
@@ -225,8 +226,8 @@ def cli_parser():
225226

226227
parser = argparse.ArgumentParser()
227228
subparsers = parser.add_subparsers(title='subcommands',
228-
description='valid subcommands',
229-
help='additional help')
229+
description='valid subcommands',
230+
help='additional help')
230231

231232
kill_session = subparsers.add_parser('kill-session')
232233
kill_session.set_defaults(callback=subcommand_kill_session)
@@ -238,10 +239,16 @@ def cli_parser():
238239
default=None,
239240
)
240241

241-
242242
attach_session = subparsers.add_parser('attach-session')
243243
attach_session.set_defaults(callback=subcommand_attach_session)
244244

245+
attach_session.add_argument(
246+
dest='session_name',
247+
nargs='*',
248+
type=str,
249+
default=None,
250+
)
251+
245252
load = subparsers.add_parser('load')
246253

247254
load.add_argument(
@@ -265,7 +272,6 @@ def cli_parser():
265272
)
266273
load.set_defaults(callback=subcommand_load)
267274

268-
269275
parser.add_argument('--log-level', dest='log_level', default='INFO',
270276
metavar='log-level',
271277
help='Log level e.g. INFO, DEBUG, ERROR')
@@ -280,9 +286,9 @@ def cli_parser():
280286
'-v', '--version', dest='version', action='store_true',
281287
help='Prints the tmuxp version')
282288

283-
284289
return parser
285290

291+
286292
def main():
287293

288294
parser = cli_parser()
@@ -298,9 +304,9 @@ def main():
298304

299305
if args.callback is subcommand_load:
300306
subcommand_load(args)
301-
if args.callback is subcommand_attach_session:
307+
elif args.callback is subcommand_attach_session:
302308
subcommand_attach_session(args)
303-
if args.callback is subcommand_kill_session:
309+
elif args.callback is subcommand_kill_session:
304310
subcommand_kill_session(args)
305311
else:
306312
if args.version:
@@ -309,7 +315,6 @@ def main():
309315
parser.print_help()
310316

311317

312-
313318
def complete(cline, cpoint):
314319

315320
parser = argparse.ArgumentParser()
@@ -353,20 +358,21 @@ def session_complete(command, commands, ctext):
353358
sessions = [s.get('session_name') for s in t._sessions]
354359
commands.extend([c for c in sessions if ctext_subargs in c])
355360

356-
#commands = [c for c in commands if ctext_subcommand_args in c]
357-
#commands = [c for c in commands if c.startswith(ctext_subargs)]
361+
# commands = [c for c in commands if ctext_subcommand_args in c]
362+
# commands = [c for c in commands if c.startswith(ctext_subargs)]
358363

359364
def config_complete(command, commands, ctext):
360365
if ctext.startswith(command + ' '):
361366
commands[:] = []
362367
ctext_subargs = ctext.replace(command + ' ', '')
363368
configs = []
364369
configs += ['./' + c for c in config.in_cwd()]
365-
#configs += config.in_cwd()
366-
configs += [os.path.join(config_dir, c) for c in config.in_dir(config_dir)]
367-
#configs += config.in_dir(config_dir)
370+
# configs += config.in_cwd()
371+
configs += [os.path.join(config_dir, c)
372+
for c in config.in_dir(config_dir)]
373+
# configs += config.in_dir(config_dir)
368374
configs += ['./' + c for c in config.in_dir(cwd_dir)]
369-
#configs += config.in_dir(cwd_dir)
375+
# configs += config.in_dir(cwd_dir)
370376
commands += [c for c in configs if c.startswith(ctext_subargs)]
371377

372378
session_complete('attach-session', commands, ctext)

tmuxp/session.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,42 @@ def tmux(self, *args, **kwargs):
6868
# kwargs['-t'] = self.get['session_id']
6969
return self.server.tmux(*args, **kwargs)
7070

71+
def attach_session(self, target_session=None):
72+
'''
73+
``$ tmux attach-session`` aka alias: ``$ tmux attach``
74+
75+
:param: target_session: str. name of the session. fnmatch(3) works.
76+
'''
77+
proc = self.tmux('attach-session', '-t%s' % self.get('session_id'))
78+
79+
if proc.stderr:
80+
raise Exception(proc.stderr)
81+
82+
def kill_session(self, target_session=None):
83+
'''
84+
``$ tmux kill-session``
85+
86+
:param: target_session: str. note this accepts fnmatch(3). 'asdf' will
87+
kill asdfasd
88+
'''
89+
proc = self.tmux('kill-session', '-t%s' % self.get('session_id'))
90+
91+
if proc.stderr:
92+
raise Exception(proc.stderr)
93+
94+
def switch_client(self, target_session=None):
95+
'''
96+
``$ tmux kill-session``
97+
98+
:param: target_session: str. note this accepts fnmatch(3). 'asdf' will
99+
kill asdfasd
100+
'''
101+
proc = self.tmux('switch-client', '-t%s' % self.get('session_id'))
102+
103+
if proc.stderr:
104+
raise Exception(proc.stderr)
105+
106+
71107
def rename_session(self, new_name):
72108
'''rename session and return new :class:`Session` object
73109

0 commit comments

Comments
 (0)