Skip to content

Commit 3f4ee3a

Browse files
committed
CLI updates, wip
1 parent 254e458 commit 3f4ee3a

File tree

1 file changed

+140
-54
lines changed

1 file changed

+140
-54
lines changed

tmuxp/cli.py

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

139139

140+
141+
def subcommand_load(args):
142+
if args.list_configs:
143+
startup(config_dir)
144+
configs_in_user = config.in_dir(config_dir)
145+
configs_in_cwd = config.in_cwd()
146+
147+
output = ''
148+
149+
if not configs_in_user:
150+
output += '# %s: \n\tNone found.\n' % config_dir
151+
else:
152+
output += '# %s: \n\t%s\n' % (
153+
config_dir, ', '.join(configs_in_user)
154+
)
155+
156+
if configs_in_cwd:
157+
output += '# current directory:\n\t%s' % (
158+
', '.join(configs_in_cwd)
159+
)
160+
161+
print(output)
162+
163+
elif args.configs:
164+
if '.' in args.configs:
165+
args.configs.remove('.')
166+
if config.in_cwd():
167+
args.configs.append(config.in_cwd()[0])
168+
else:
169+
print('No tmuxp configs found in current directory.')
170+
171+
for configfile in args.configs:
172+
file_user = os.path.join(config_dir, configfile)
173+
file_cwd = os.path.join(cwd_dir, configfile)
174+
if os.path.exists(file_cwd) and os.path.isfile(file_cwd):
175+
build_workspace(file_cwd, args)
176+
elif os.path.exists(file_user) and os.path.isfile(file_user):
177+
build_workspace(file_user, args)
178+
else:
179+
logger.error('%s not found.' % configfile)
180+
else:
181+
parser.print_help()
182+
183+
184+
def subcommand_attach_session(args):
185+
print('attac session')
186+
for session_name in args.session_name:
187+
print(session_name)
188+
189+
def session_complete(command, commands, ctext):
190+
if ctext.startswith(command + ' '):
191+
commands[:] = []
192+
ctext_attach = ctext.replace(command + ' ', '')
193+
194+
sessions = [s.get('session_name') for s in t._sessions]
195+
commands.extend([c for c in sessions if ctext_attach in c])
196+
197+
def subcommand_kill_session(args):
198+
print('kill session')
199+
print(args)
200+
print(type(args.session_name))
201+
print(args.session_name)
202+
203+
for session_name in args.session_name:
204+
print(session_name)
205+
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])
213+
214+
140215
def cli_parser():
141216

142217
parser = argparse.ArgumentParser(
@@ -145,7 +220,32 @@ def cli_parser():
145220
''',
146221
)
147222

148-
parser.add_argument(
223+
parser = argparse.ArgumentParser()
224+
subparsers = parser.add_subparsers(title='subcommands',
225+
description='valid subcommands',
226+
help='additional help')
227+
228+
kill_session = subparsers.add_parser('kill-session')
229+
kill_session.set_defaults(callback=subcommand_kill_session)
230+
231+
kill_session.add_argument(
232+
dest='session_name',
233+
nargs='*',
234+
type=str,
235+
default=None,
236+
)
237+
238+
239+
attach_session = subparsers.add_parser('attach-session')
240+
attach_session.set_defaults(callback=subcommand_attach_session)
241+
242+
load = subparsers.add_parser('load')
243+
244+
load.add_argument(
245+
'-l', '--list', dest='list_configs', action='store_true',
246+
help='List config files available')
247+
248+
load.add_argument(
149249
dest='configs',
150250
nargs='*',
151251
type=str,
@@ -160,23 +260,23 @@ def cli_parser():
160260
will check launch a ~/.pullv.yaml / ~/.pullv.json from the cwd.
161261
''' % (cwd_dir + '/', config_dir)
162262
)
263+
load.set_defaults(callback=subcommand_load)
264+
265+
266+
parser.add_argument('--log-level', dest='log_level', default='INFO',
267+
metavar='log-level',
268+
help='Log level e.g. INFO, DEBUG, ERROR')
163269

164270
parser.add_argument('-L', dest='socket_name', default=None,
165271
metavar='socket-name')
166272

167273
parser.add_argument('-S', dest='socket_path', default=None,
168274
metavar='socket-path')
169275

170-
parser.add_argument(
171-
'-l', '--list', dest='list_configs', action='store_true',
172-
help='List config files available')
173-
174276
parser.add_argument(
175277
'-v', '--version', dest='version', action='store_true',
176278
help='Prints the tmuxp version')
177279

178-
parser.add_argument('--log-level', dest='log_level', default='INFO',
179-
help='Log level')
180280

181281
return parser
182282

@@ -193,66 +293,43 @@ def main():
193293
logger.error(e)
194294
sys.exit()
195295

196-
if args.version:
197-
print('tmuxp %s' % __version__)
198-
elif args.list_configs:
199-
startup(config_dir)
200-
configs_in_user = config.in_dir(config_dir)
201-
configs_in_cwd = config.in_cwd()
202-
203-
output = ''
204-
205-
if not configs_in_user:
206-
output += '# %s: \n\tNone found.\n' % config_dir
207-
else:
208-
output += '# %s: \n\t%s\n' % (
209-
config_dir, ', '.join(configs_in_user)
210-
)
296+
if args.callback is subcommand_load:
297+
subcommand_load(args)
298+
if args.callback is subcommand_attach_session:
299+
subcommand_attach_session(args)
300+
if args.callback is subcommand_kill_session:
301+
subcommand_kill_session(args)
302+
else:
303+
if args.version:
304+
print('tmuxp %s' % __version__)
305+
elif args.kill_session:
306+
print(args.kill_session)
211307

212-
if configs_in_cwd:
213-
output += '# current directory:\n\t%s' % (
214-
', '.join(configs_in_cwd)
215-
)
308+
parser.print_help()
216309

217-
print(output)
218310

219-
elif args.configs:
220-
if '.' in args.configs:
221-
args.configs.remove('.')
222-
if config.in_cwd():
223-
args.configs.append(config.in_cwd()[0])
224-
else:
225-
print('No tmuxp configs found in current directory.')
226311

227-
for configfile in args.configs:
228-
file_user = os.path.join(config_dir, configfile)
229-
file_cwd = os.path.join(cwd_dir, configfile)
230-
if os.path.exists(file_cwd) and os.path.isfile(file_cwd):
231-
build_workspace(file_cwd, args)
232-
elif os.path.exists(file_user) and os.path.isfile(file_user):
233-
build_workspace(file_user, args)
234-
else:
235-
logger.error('%s not found.' % configfile)
236-
else:
237-
parser.print_help()
312+
def complete(cline, cpoint):
238313

314+
# parser = argparse.ArgumentParser()
315+
# args = parser.parse_args()
316+
# parser.add_argument('-L', dest='socket_name', default=None,
317+
# metavar='socket-name')
239318

240-
def complete(cline, cpoint):
319+
# parser.add_argument('-S', dest='socket_path', default=None,
320+
# metavar='socket-path')
241321

242-
parser = cli_parser()
243-
args = parser.parse_args()
244322

245323
commands = []
246-
commands += config.in_dir(config_dir)
247-
commands += config.in_cwd()
248-
commands.extend(['attach', 'kill-session'])
324+
325+
commands.extend(['attach-session', 'kill-session', 'load'])
249326

250327
ctext = cline.replace('tmuxp ', '')
251328
commands = [c for c in commands if ctext in c]
252329

253330
t = Server(
254-
socket_name=args.socket_name,
255-
socket_path=args.socket_path
331+
# socket_name=args.socket_name or None,
332+
# socket_path=args.socket_path or None
256333
)
257334

258335
def session_complete(command, commands, ctext):
@@ -263,8 +340,17 @@ def session_complete(command, commands, ctext):
263340
sessions = [s.get('session_name') for s in t._sessions]
264341
commands.extend([c for c in sessions if ctext_attach in c])
265342

343+
def config_complete(command, commands, ctext):
344+
if ctext.startswith(command + ' '):
345+
commands[:] = []
346+
ctext_subcommand_args = ctext.replace(command + ' ', '')
347+
commands += config.in_dir(config_dir)
348+
commands += config.in_cwd()
349+
commands = [c for c in commands if ctext_subcommand_args in c]
350+
266351
session_complete('attach', commands, ctext)
267-
session_complete('list-sessions', commands, ctext)
268352
session_complete('kill-session', commands, ctext)
269353

354+
config_complete('load', commands, ctext)
355+
270356
print(' \n'.join(commands))

0 commit comments

Comments
 (0)