diff --git a/.bin/Scripts/ddrescue-tui b/.bin/Scripts/ddrescue-tui index e41c6bb1..6ee8ad57 100755 --- a/.bin/Scripts/ddrescue-tui +++ b/.bin/Scripts/ddrescue-tui @@ -2,42 +2,10 @@ # ## Wizard Kit: ddrescue TUI Launcher +source launch-in-tmux + SESSION_NAME="ddrescue-tui" WINDOW_NAME="ddrescue TUI" -MENU="ddrescue-tui-menu" - -function ask() { - while :; do - read -p "$1 " -r answer - if echo "$answer" | egrep -iq '^(y|yes|sure)$'; then - return 0 - elif echo "$answer" | egrep -iq '^(n|no|nope)$'; then - return 1 - fi - done -} - -die () { - echo "$0:" "$@" >&2 - exit 1 -} - -# Check for running session -if tmux list-session | grep -q "$SESSION_NAME"; then - echo "WARNING: tmux session $SESSION_NAME already exists." - echo "" - if ask "Kill current session?"; then - tmux kill-session -t "$SESSION_NAME" || \ - die "Failed to kill session: $SESSION_NAME" - else - echo "Aborted." - echo "" - echo -n "Press Enter to exit... " - read -r - exit 0 - fi -fi - -# Start session -tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" $* +TMUX_CMD="ddrescue-tui-menu" +launch_in_tmux "$@" diff --git a/.bin/Scripts/ddrescue-tui-menu b/.bin/Scripts/ddrescue-tui-menu index f65e24e1..eab8cd3f 100755 --- a/.bin/Scripts/ddrescue-tui-menu +++ b/.bin/Scripts/ddrescue-tui-menu @@ -45,6 +45,7 @@ if __name__ == '__main__': # Done print_standard('\nDone.') pause("Press Enter to exit...") + tmux_switch_client() exit_script() except GenericAbort: abort() @@ -55,6 +56,7 @@ if __name__ == '__main__': print_error(msg) abort() except SystemExit as sys_exit: + tmux_switch_client() exit_script(sys_exit.code) except: major_exception() diff --git a/.bin/Scripts/functions/ddrescue.py b/.bin/Scripts/functions/ddrescue.py index b276d52b..b507b127 100644 --- a/.bin/Scripts/functions/ddrescue.py +++ b/.bin/Scripts/functions/ddrescue.py @@ -281,6 +281,7 @@ class RecoveryState(): raise GenericError('Unsupported mode') self.get_smart_source() self.set_working_dir() + os.makedirs(global_vars['LogDir'], exist_ok=True) def add_block_pair(self, source, dest): """Run safety checks and append new BlockPair to internal list.""" @@ -930,7 +931,7 @@ def menu_ddrescue(source_path, dest_path, run_mode): pause('Press Enter to exit... ') # Done - run_program(['tmux', 'kill-window']) + tmux_kill_all_panes() exit_script() diff --git a/.bin/Scripts/functions/hw_diags.py b/.bin/Scripts/functions/hw_diags.py index da347082..35da53e3 100644 --- a/.bin/Scripts/functions/hw_diags.py +++ b/.bin/Scripts/functions/hw_diags.py @@ -1277,7 +1277,9 @@ def run_hw_tests(state): print_standard(' ') # Upload for review - if ENABLED_UPLOAD_DATA and ask('Upload results for review?'): + if (ENABLED_UPLOAD_DATA + and DEBUG_MODE + and ask('Upload results for review?')): try_and_print( message='Saving debug reports...', function=save_debug_reports, diff --git a/.bin/Scripts/functions/tmux.py b/.bin/Scripts/functions/tmux.py index 81522268..8c6ad327 100644 --- a/.bin/Scripts/functions/tmux.py +++ b/.bin/Scripts/functions/tmux.py @@ -141,6 +141,18 @@ def tmux_split_window( return result.stdout.decode().strip() +def tmux_switch_client(target_session=None): + """Switch to target tmux session, or previous if none specified.""" + cmd = ['tmux', 'switch-client'] + if target_session: + cmd.extend(['-t', target_session]) + else: + # Switch to previous instead + cmd.append('-p') + + run_program(cmd, check=False) + + def tmux_update_pane( pane_id, command=None, working_dir=None, text=None, watch=None, watch_cmd='cat'): diff --git a/.bin/Scripts/hw-diags b/.bin/Scripts/hw-diags index d3a1cb21..70f84db4 100755 --- a/.bin/Scripts/hw-diags +++ b/.bin/Scripts/hw-diags @@ -1,46 +1,11 @@ #!/bin/bash # -## Wizard Kit: HW Diagnostics - Menu Launcher +## Wizard Kit: HW Diagnostics Launcher + +source launch-in-tmux SESSION_NAME="hw-diags" WINDOW_NAME="Hardware Diagnostics" -MENU="hw-diags-menu" - -function ask() { - while :; do - read -p "$1 [Y/N] " -r answer - if echo "$answer" | egrep -iq '^(y|yes|sure)$'; then - return 0 - elif echo "$answer" | egrep -iq '^(n|no|nope)$'; then - return 1 - fi - done -} - -die () { - echo "$0:" "$@" >&2 - exit 1 -} - -# Check for running session -if tmux list-session | grep -q "$SESSION_NAME"; then - echo "WARNING: tmux session $SESSION_NAME already exists." - echo "" - if ask "Connect to current session?"; then - # Do nothing, the command below will attach/connect - echo "" - elif ask "Kill current session and start new session?"; then - tmux kill-session -t "$SESSION_NAME" || \ - die "Failed to kill session: $SESSION_NAME" - else - echo "Aborted." - echo "" - echo -n "Press Enter to exit... " - read -r - exit 0 - fi -fi - -# Start session -tmux new-session -A -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" $* +TMUX_CMD="hw-diags-menu" +launch_in_tmux "$@" diff --git a/.bin/Scripts/hw-diags-menu b/.bin/Scripts/hw-diags-menu index fc95e04a..7a122ae7 100755 --- a/.bin/Scripts/hw-diags-menu +++ b/.bin/Scripts/hw-diags-menu @@ -23,6 +23,7 @@ if __name__ == '__main__': sleep(1) pause('Press Enter to exit...') except SystemExit as sys_exit: + tmux_switch_client() exit_script(sys_exit.code) except: # Cleanup @@ -59,6 +60,7 @@ if __name__ == '__main__': # Done tmux_kill_all_panes() + tmux_switch_client() exit_script() # vim: sts=2 sw=2 ts=2 diff --git a/.bin/Scripts/launch-in-tmux b/.bin/Scripts/launch-in-tmux new file mode 100755 index 00000000..e737b574 --- /dev/null +++ b/.bin/Scripts/launch-in-tmux @@ -0,0 +1,64 @@ +#!/bin/bash +# +## Wizard Kit: TMUX Launcher + +function ask() { + while :; do + read -p "$1 [Y/N] " -r answer + if echo "$answer" | grep -Eiq '^(y|yes|sure)$'; then + return 0 + elif echo "$answer" | grep -Eiq '^(n|no|nope)$'; then + return 1 + fi + done +} + +die () { + echo "$0:" "$@" >&2 + exit 1 +} + +function launch_in_tmux() { + # Check for required vars + [[ -n "${SESSION_NAME:-}" ]] || die "Required variable missing (SESSION_NAME)" + [[ -n "${WINDOW_NAME:-}" ]] || die "Required variable missing (WINDOW_NAME)" + [[ -n "${TMUX_CMD:-}" ]] || die "Required variable missing (TMUX_CMD)" + + # Check for running session + if tmux list-session | grep -q "$SESSION_NAME"; then + echo "WARNING: tmux session $SESSION_NAME already exists." + echo "" + if ask "Connect to current session?"; then + if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, switch to session + tmux switch-client -t "$SESSION_NAME" + else + # Running outside TMUX, attach to session + tmux attach-session -t "$SESSION_NAME" + fi + exit 0 + elif ask "Kill current session and start new session?"; then + tmux kill-session -t "$SESSION_NAME" || \ + die "Failed to kill session: $SESSION_NAME" + else + echo "Aborted." + echo "" + echo -n "Press Enter to exit... " + read -r + exit 0 + fi + fi + + # Start/Rename session + if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, rename session/window and open the menu + tmux rename-session "$SESSION_NAME" + tmux rename-window "$WINDOW_NAME" + "$TMUX_CMD" "$@" + tmux rename-session "${SESSION_NAME}_DONE" + tmux rename-window "${WINDOW_NAME}_DONE" + else + # Running outside TMUX, start/attach to session + tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$TMUX_CMD" "$@" + fi +}