<commit_before>#!/bin/bash
##
## Copyright (C) 2014 Y.Morikawa <http://moya-notes.blogspot.jp/>
##
## License: MIT License  (See LICENSE.md)
##
########################################
## Settings
########################################
TMUX_YAML_PATH="${HOME}/.tmux-load-conf"

##### Functions #####
reset_files(){
    for file in $*; do
        rm -f ${file} ; touch ${file}
    done
}

##### Internal Settings #####
tmpd="/tmp/tmux-load.$$"

load_file_tmp="${tmpd}/load_file"
err_file="${tmpd}/error"
session_file="${tmpd}/session"
window_file="${tmpd}/window"
pane_file="${tmpd}/pane"
pane_sync_file="${tmpd}/pane-sync"
pane_layout_file="${tmpd}/pane-layout"
window_cmd_file="${tmpd}/window-command"
pane_cmd_file="${tmpd}/pane-command"

tmp_files="${load_file_tmp} ${err_file} ${session_file} ${window_file} ${pane_file} ${pane_sync_file} ${pane_layout_file} ${window_cmd_file} ${pane_cmd_file}"

##### Main Routine #####

if [ -z "$1" ]; then
    echo 
    echo "  Usage: ./$(basename $0) file"
    echo
    exit 1
fi

load_file="${TMUX_YAML_PATH}/$1"

if [ ! -r "${load_file}" ]; then
    echo 
    echo "  ERROR:  \"${load_file}\" is not found or not readable."
    echo
    exit 1
fi

test -d $tmpd || mkdir $tmpd
reset_files ${tmp_files}

cat ${load_file} > ${load_file_tmp}
echo "" >> ${load_file_tmp}

window_1st=1
cat ${load_file_tmp} | while read line; do

    if [[ "$line" =~ ^# ]]; then
        continue
    fi

    if [[ "$line" =~ ^$ ]]; then
        continue
    fi
    
    case "$line" in
        session:*) 
            session=$(echo $line | awk -F: '{print $2}')
            if [ -z "$session" ]; then
                cnt=0
                dup=1

                while [ -n "$dup" ]; do
                    session="anon${cnt}"
                    tmux has-session -t $session > /dev/null 2>&1
                    if [ $? -ne 0 ];then
                        dup=
                        break
                    fi
                    cnt=$(expr $cnt + 1)
                done
            fi
            
            echo $session > $session_file
            
            tmux has-session -t $session > /dev/null 2>&1
            if [ $? -eq 0 ];then
                break
            fi

            ;;
        window-command:*)
            window_command="$(echo $line | sed 's/^window-command: *//g')"
            #echo $window_command | tr ';' '\n' > $window_cmd_file
            echo $window_command >> $window_cmd_file
            ;;
        window:*)
            session=$(cat $session_file)
            if [ -z "$session" ]; then
                echo ""
                echo "  ERROR: \"session\" is not found. Please write \"session:\" on the file."
                echo ""
                echo "1" > ${err_file}
                exit 1
            fi
            
            windows="$(echo $line | sed 's/^window: *//g')"
            if [ -z "$windows" ]; then
                windows="$session"
            fi

            echo $windows > $window_file

            for window in $(eval echo $windows); do
                tmux has-session -t $session > /dev/null 2>&1
                if [ $? -eq 0 ];then
                    tmux new-window -n $window
                else
                    tmux new-session -d -n $window -s $session
                fi
                cat ${window_cmd_file} | while read cmd; do
                    tmux send-keys "eval $(echo ${cmd} | sed "s/\$window/$window/g")" C-m
                done
            done
            reset_files ${window_cmd_file}

            window_1st=1
            ;;
        pane-command:*)
            pane_command="$(echo $line | sed 's/^pane-command: *//g')"
            #echo $pane_command | tr ';' '\n'  > $pane_cmd_file
            echo $pane_command >> $pane_cmd_file
            ;;
        pane-sync:*)
            echo 1 >> $pane_sync_file
            ;;
        pane-layout:*)
            pane_layout="$(echo $line | sed 's/^pane-layout: *//g')"
            echo $pane_layout >> $pane_layout_file
            ;;
        pane:*)
            panes="$(echo $line | sed 's/^pane: *//g')"
            echo $pane > $pane_file
            windows=$(cat $window_file)

            pane_layout=$(cat ${pane_layout_file})
            if [ -z "$pane_layout" ]; then<commit_msg>Command line arguments can be used as $argv<commit_after>#!/bin/bash
##
## Copyright (C) 2014 Y.Morikawa <http://moya-notes.blogspot.jp/>
##
## License: MIT License  (See LICENSE.md)
##
########################################
## Settings (Please modify for your environment)
########################################
TMUX_YAML_PATH="${HOME}/.tmux-load-conf"



########################################
## Main Routine, etc.
########################################

##### Functions #####
reset_files(){
    for file in $*; do
        rm -f ${file} ; touch ${file}
    done
}

##### Internal Settings #####
tmpd="/tmp/tmux-load.$$"

load_file_tmp="${tmpd}/load_file"
err_file="${tmpd}/error"
session_file="${tmpd}/session"
window_file="${tmpd}/window"
pane_file="${tmpd}/pane"
pane_sync_file="${tmpd}/pane-sync"
pane_layout_file="${tmpd}/pane-layout"
window_cmd_file="${tmpd}/window-command"
pane_cmd_file="${tmpd}/pane-command"

tmp_files="${load_file_tmp} ${err_file} ${session_file} ${window_file} ${pane_file} ${pane_sync_file} ${pane_layout_file} ${window_cmd_file} ${pane_cmd_file}"

##### Main Routine #####

if [ -z "$1" ]; then
    echo 
    echo "  Usage: ./$(basename $0) file"
    echo
    exit 1
fi

load_file="${TMUX_YAML_PATH}/$1"

if [ ! -r "${load_file}" ]; then
    echo 
    echo "  ERROR:  \"${load_file}\" is not found or not readable."
    echo
    exit 1
fi

shift
argv=
for i in $*;do
    argv="$argv $i"
done

test -d $tmpd || mkdir $tmpd
reset_files ${tmp_files}

cat ${load_file} > ${load_file_tmp}
echo "" >> ${load_file_tmp}

window_1st=1
cat ${load_file_tmp} | while read line; do

    if [[ "$line" =~ ^# ]]; then
        continue
    fi

    if [[ "$line" =~ ^$ ]]; then
        continue
    fi
    
    case "$line" in
        session:*) 
            session=$(echo $line | awk -F: '{print $2}' | sed "s/\$argv/$argv/g")
            if [ -z "$session" ]; then
                cnt=0
                dup=1

                while [ -n "$dup" ]; do
                    session="anon${cnt}"
                    tmux has-session -t $session > /dev/null 2>&1
                    if [ $? -ne 0 ];then
                        dup=
                        break
                    fi
                    cnt=$(expr $cnt + 1)
                done
            fi
            
            echo $session > $session_file
            
            tmux has-session -t $session > /dev/null 2>&1
            if [ $? -eq 0 ];then
                break
            fi

            ;;
        window-command:*)
            window_command="$(echo $line | sed 's/^window-command: *//g')"
            #echo $window_command | tr ';' '\n' > $window_cmd_file
            echo $window_command >> $window_cmd_file
            ;;
        window:*)
            session=$(cat $session_file)
            if [ -z "$session" ]; then
                echo ""
                echo "  ERROR: \"session\" is not found. Please write \"session:\" on the file."
                echo ""
                echo "1" > ${err_file}
                exit 1
            fi
            
            windows="$(echo $line | sed 's/^window: *//g' | sed "s/\$argv/$argv/g")"
            if [ -z "$windows" ]; then
                windows="$session"
            fi

            echo $windows > $window_file

            for window in $(eval echo $windows); do
                tmux has-session -t $session > /dev/null 2>&1
                if [ $? -eq 0 ];then
                    tmux new-window -n $window
                else
                    tmux new-session -d -n $window -s $session
                fi
                cat ${window_cmd_file} | while read cmd; do
                    tmux send-keys "eval $(echo ${cmd} | sed "s/\$window/$window/g")" C-m
                done
            done
            reset_files ${window_cmd_file}

            window_1st=1
            ;;
        pane-command:*)
            pane_command="$(echo $line | sed 's/^pane-command: *//g')"
            #echo $pane_command | tr ';' '\n'  > $pane_cmd_file
            echo $pane_command >> $pane_cmd_file
            ;;
        pane-sync:*)
            echo 1 >> $pane_sync_file
            ;;
        pane-layout:*)
            pane_layout="$(echo $line | sed 's/^pane-layout: *//g')"
            echo $pane_layout >> $pane_layout_file
            ;;
        pane:*)
            panes="$(echo $line | sed 's/^pane: *//g' | sed "s/\$argv/$argv/g")"
            echo $pane > $pane_file
            windows=$(cat $window_file)
            if [ -z "$windows" ]; then
                windows="$session"
            fi

            pane_layout=$(cat ${pane_layout_file})
            if [ -z "$pane_layout" ]; then
{'fraction_non_alphanumeric': 0.17917458897215077, 'fraction_numerical': 0.006486970137568504, 'mean_word_length': 1.6230566148430625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4495765', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2736, 'n_words': 659}