<commit_before>from argparse import ArgumentParser
from dodo_commands.framework import Dodo, CommandError
import os
from six.moves import input as raw_input
from dodo_commands.framework.util import filter_choices
from plumbum.cmd import tmux

session_id = os.path.expandvars('$USER')


def _args():
    parser = ArgumentParser()
    parser.add_argument('--kill-session', action='store_true')
    args = Dodo.parse_args(parser)
    args.commands = Dodo.get_config('/TMUX/commands', [])
    return args


def _create_tmux_window():
    # Create tmux session
    Dodo.run(['tmux', '-2', 'new-session', '-d', '-s', session_id], )

    # Create a tmux window
    Dodo.run(['tmux', 'new-window', '-t', '%s:1' % session_id, '-n', 'Logs'], )


if Dodo.is_main(__name__):
    args = _args()
    check_exists = Dodo.get_config('/TMUX/check_exists', '/')
    if not os.path.exists(check_exists):
        raise CommandError("Path %s does not exist" % check_exists)

    has_session = False
    try:
        sessions = tmux('ls')
        for session in sessions.split('\n'):
            has_session = has_session or session.startswith('%s:' % session_id)
    except:
        pass

    if has_session and args.kill_session:
        Dodo.run(['tmux', 'kill-session', '-t', session_id], )

    if not has_session:
        _create_tmux_window()
        Dodo.run(['tmux', 'send-keys', 'dodo tmux', 'C-m'], )
        # Attach to tmux session
        Dodo.run(['tmux', '-2', 'attach-session', '-t', session_id], )
    else:
        while True:
            commands = []
            label_size = 0
            for label in args.commands:
                label_size = max(label_size, len(label))
            label_prefix = "%0" + str(label_size) + "s"

            print()
            for label in args.commands:
                for command in args.commands[label]:
                    commands.append(command)
                    format_string = "%02s [" + label_prefix + "] - %s"
                    print(format_string % (str(len(commands)), label, command))

            raw_choice = raw_input(
                '\nSelect one or more commands (e.g. 1,3-4), or type a command: '
            )
            selected_commands, span = filter_choices(commands, raw_choice)
            if span != [0, len(raw_choice)]:
                selected_commands = [raw_choice]<commit_msg>Add dodo tmux --run and --list<commit_after>from argparse import ArgumentParser
from dodo_commands.framework import Dodo, CommandError
import os
import sys
from six.moves import input as raw_input
from dodo_commands.framework.util import filter_choices
from plumbum.cmd import tmux

session_id = os.path.expandvars('$USER')


def _args():
    parser = ArgumentParser()
    parser.add_argument('--kill-session', action='store_true')
    parser.add_argument('--list', action='store_true')
    parser.add_argument('--run', type=int)
    args = Dodo.parse_args(parser)
    args.command_map = Dodo.get_config('/TMUX/commands', [])
    return args


def _create_tmux_window():
    # Create tmux session
    Dodo.run(['tmux', '-2', 'new-session', '-d', '-s', session_id], )

    # Create a tmux window
    Dodo.run(['tmux', 'new-window', '-t', '%s:1' % session_id, '-n', 'Logs'], )


def _get_commands_and_labels(command_map):
    label_size = 0
    for label in command_map:
        label_size = max(label_size, len(label))
    label_prefix = "%0" + str(label_size) + "s"

    commands, labels = [], []
    for label in command_map:
        for command in command_map[label]:
            commands.append(command)
            format_string = "%02s [" + label_prefix + "] - %s"
            labels.append(format_string % (str(len(commands)), label, command))

    return commands, labels

if Dodo.is_main(__name__):
    args = _args()
    check_exists = Dodo.get_config('/TMUX/check_exists', '/')
    if not os.path.exists(check_exists):
        raise CommandError("Path %s does not exist" % check_exists)

    commands, labels = _get_commands_and_labels(args.command_map)

    if args.list:
        print()
        for label in labels:
            print(label)
        sys.exit(0)

    if args.run:
        Dodo.run(['bash', '-c', commands[args.run - 1]])
        sys.exit(0)

    has_session = False
    try:
        sessions = tmux('ls')
        for session in sessions.split('\n'):
            has_session = has_session or session.startswith('%s:' % session_id)
    except:
        pass

    if has_session and args.kill_session:
        Dodo.run(['tmux', 'kill-session', '-t', session_id], )

    if not has_session:
        _create_tmux_window()
        Dodo.run(['tmux', 'send-keys', 'dodo tmux', 'C-m'], )
        # Attach to tmux session
        Dodo.run(['tmux', '-2', 'attach-session', '-t', session_id], )
    else:
        while True:
            print()
            for label in labels:
                print(label)

            raw_choice = raw_input(
                '\nSelect one or more commands (e.g. 1,3-4), or type a command: '
            )
            selected_commands, span = filter_choices(commands, raw_choice)
            if span != [0, len(raw_choice)]:
                selected_commands = [raw_choice]
{'fraction_non_alphanumeric': 0.13507246376811594, 'fraction_numerical': 0.004830917874396135, 'mean_word_length': 2.499661933739013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7144280', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1614, 'n_words': 394}