Skip to content

Commit

Permalink
env: Add tmxrun
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Chancellor <[email protected]>
  • Loading branch information
nathanchance committed Dec 8, 2024
1 parent f07a6ff commit d3ed5a2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions fish/functions/tmxrun.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env fish
# SPDX-License-Identifier: MIT
# Copyright (C) 2024 Nathan Chancellor

function tmxrun -d "Wrapper for tmxrun.py"
# The duplication is sad but it is so much better to handle adding -- in the wrapper function
for arg in $argv
switch $arg
case -c -d -H --container --detach --host
if not set -q pos_args
set -a py_args $arg
else
set -a pos_args $arg
end
case '*'
set -a pos_args $arg
end
end
$PYTHON_SCRIPTS_FOLDER/tmxrun.py $py_args -- $pos_args
end
43 changes: 43 additions & 0 deletions python/scripts/tmxrun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3

from argparse import ArgumentParser
from pathlib import Path
import subprocess
import sys

sys.path.append(str(Path(__file__).resolve().parents[1]))
# pylint: disable=wrong-import-position
import lib.utils
# pylint: enable=wrong-import-position

parser = ArgumentParser(
description=
'Run command in "tmux new-window" with proper quoting to run in either host or container')
parser.add_argument('-c',
'--container',
action='store_const',
const='container',
dest='mode',
help='Run command in default distrobox container')
parser.add_argument('-H',
'--host',
action='store_const',
const='host',
dest='mode',
help='Run command on the host')
parser.add_argument('-d', '--detach', action='store_true', help='Do not switch to created window')
parser.add_argument('args', nargs='+', help='Command and any arguments to run')
args = parser.parse_args()

mode = args.mode if args.mode else 'container' if lib.utils.in_container() else 'host'

tmx_cmd = ['tmux', 'new-window']
if args.detach:
tmx_cmd.append('-d')

CMD_STR = ' '.join(map(str, args.args))
if mode == 'container':
CMD_STR = f"dbxe -- fish -c '{CMD_STR}'"
tmx_cmd.append(CMD_STR)

subprocess.run(tmx_cmd, check=True)

0 comments on commit d3ed5a2

Please sign in to comment.