Beispiel #1
0
def rm(ctx, alias):
    """Removes a command from the tomb.

    Arguments:
        alias (str): The alias of the command to remove.
    """
    if tomb_handler.remove_command(ctx, alias):
        formatter.print_success(constants.CMD_RM_OK.format(alias))
    else:
        formatter.print_warning(constants.WARN_CMD_NOT_FOUND.format(alias))
Beispiel #2
0
def edit(ctx, alias):
    """Edits a command if it's present in the active tomb.

    Opens an editor for the user with the details for the specified command,
    allowing edits to the alias, command and description.

    Arguments:
        alias (str): The alias of the command to edit.
    """
    if not tomb_handler.is_existing_command(ctx, alias):
        helpers.exit(constants.WARN_CMD_NOT_FOUND.format(alias))

    editor = os.environ.get("EDITOR", helpers.default_editor())
    cmd_info = tomb_handler.get_command(ctx, alias, True)
    fname = helpers.random_fname()

    # Creates a temporary file with the current command information, allowing
    # edits.
    with open(fname, "w") as temp_file:
        # Append the initial message to the file.
        temp_file.write(constants.CMD_EDIT_INIT_MSG.format(
            alias, cmd_info[0], cmd_info[1]))

    # Open the file within an editor.
    call([editor, temp_file.name])

    # Reads the changes to the file after the user is done with it.
    with open(fname, "r") as temp_file:
        edits = read_edits(temp_file)

    # Clean up.
    os.remove(fname)

    # The edited alias already exists in the active tomb.
    if edits["alias"] != alias and tomb_handler.is_existing_command(
            ctx, edits["alias"]):
        update = click.prompt(
            constants.CMD_EDIT_OVERWRITE_PROMPT.format(alias))

        if update.lower() != "y":
            # Abort the action.
            helpers.exit(constants.WARN_ACTION_ABORTED)
        else:
            # We need to remove the old command if we're not overwriting it.
            tomb_handler.remove_command(ctx, alias)
    elif not tomb_handler.is_existing_command(ctx, edits["alias"]):
        # Remember to remove the old command if we're adding a new one.
        tomb_handler.remove_command(ctx, alias)

    # Update the command.
    tomb_handler.add_command(
        ctx, edits["command"], edits["alias"], edits["description"])
    formatter.print_success(constants.CMD_EDIT_OK)
Beispiel #3
0
def create(ctx, tomb_name, force):
    """Creates a new tomb.

    Arguments:
        tomb_name (str): The name/alias of the tomb.
        force (bool): Option to disable prompting the user for confirmation.
    """
    # Check if there currently exists a tomb that has the same name as the
    # one specified before creating a new one.
    if not catacomb_handler.is_existing_tomb(ctx, tomb_name) or force:
        description = click.prompt(constants.CMD_CREATE_DESC_PROMPT)
        catacomb_handler.create_tomb(ctx, tomb_name, description)
        formatter.print_success(constants.CMD_CREATE_OK.format(tomb_name))
    else:
        formatter.print_warning(constants.WARN_TOMB_EXISTS.format(tomb_name))
Beispiel #4
0
def clean(ctx, force):
    """Prompts the user to confirm cleaning of the current tomb. If the user
    confirms the action, the tomb will be reset to it's original empty state.

    Arguments:
        force (bool): Option to disable prompting the user for confirmation.
    """
    if not force:
        # Prompt the user for command details.
        confirm = click.prompt(constants.CMD_CLEAN_PROMPT)

    if force or confirm.lower() == "y":
        tomb_handler.clean_tomb(ctx)
        formatter.print_success(constants.CMD_CLEAN_OK)
    else:
        formatter.print_warning(constants.WARN_ACTION_ABORTED)
Beispiel #5
0
def open(ctx, tomb_name, new):
    """Opens the tomb specified by the user, granting access to all the
    commands stored within it.

    Arguments:
        tomb_name (str): The name/alias of the tomb.
        new (bool): If True, create a new tomb (if it doesn't exist) and then
            switch to it.
    """
    if catacomb_handler.get_current_tomb_name(ctx).lower() == tomb_name:
        # Don't do anything if the specified tomb is already open.
        if new:
            formatter.print_warning(
                constants.WARN_TOMB_EXISTS.format(tomb_name))
        else:
            formatter.print_warning(
                constants.CMD_OPEN_SELF_WARN.format(tomb_name))
    elif new:
        # Create a new tomb and switch to it.
        if not catacomb_handler.is_existing_tomb(ctx, tomb_name):
            description = click.prompt(constants.CMD_OPEN_NEW_DESC_PROMPT)
            catacomb_handler.create_tomb(ctx, tomb_name, description)
            catacomb_handler.open_tomb(ctx, tomb_name)
            formatter.print_success(
                constants.CMD_OPEN_NEW_OK.format(tomb_name))
        else:
            # Do nothing if a tomb with the provided alias already exists.
            formatter.print_warning(
                constants.WARN_TOMB_EXISTS.format(tomb_name))
    elif catacomb_handler.is_existing_tomb(ctx, tomb_name):
        # Otherwise open a new tomb if it exists.
        catacomb_handler.open_tomb(ctx, tomb_name)
        formatter.print_success(constants.CMD_OPEN_OK.format(tomb_name))
    else:
        formatter.print_warning(
            constants.WARN_TOMB_NOT_FOUND.format(tomb_name))
Beispiel #6
0
def bury(ctx, tomb_name, force):
    """Removes the specified tomb from the catacomb.

    Arguments:
        tomb_name (str): The name/alias of the tomb.
        force (bool): Option to disable prompting the user for confirmation.
    """
    if catacomb_handler.get_current_tomb_name(ctx).lower() == tomb_name:
        # Can't bury a tomb if it's currently being used.
        formatter.print_warning(constants.CMD_BURY_SELF_WARN.format(tomb_name))
    elif not catacomb_handler.is_existing_tomb(ctx, tomb_name):
        # If the tomb, specified by the user, can not be found, do nothing.
        formatter.print_warning(
            constants.WARN_TOMB_NOT_FOUND.format(tomb_name))
    else:
        if not force:
            # Prompt the user for confirmation on burying the tomb.
            confirm = click.prompt(constants.CMD_BURY_PROMPT.format(tomb_name))

        if force or confirm.lower() == "y":
            catacomb_handler.remove_tomb(ctx, tomb_name)
            formatter.print_success(constants.CMD_BURY_OK.format(tomb_name))
        else:
            formatter.print_warning(constants.WARN_ACTION_ABORTED)