ee

 EE - Easy Editor

#!/b:n/bash

# EE : Easy Editor.
# A bash script to create, open, edit, and save plain text files.
# Run chmod +x ee.sh to make it executable.
# Run sudo mv ee.sh /usr/local/bin/ee so it can be run from anywhere.
# Run ee to launch EE : Easy Editor.

# Configuration
TEXT_FILES_DIR="."

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Display usage information
show_help() {
    echo "EE : Easy Editor"
    echo "Usage: $0 [COMMAND] [FILE_NAME]"
    echo ""
    echo "Commands:"
    echo "  create <name>   Create a new text file"
    echo "  open <name>     Open an existing text file for viewing"
    echo "  edit <name>     Edit an existing text file"
    echo "  list            List all text files"
    echo "  delete <name>   Delete a text file"
    echo "  search <term>   Search for text within files"
    echo "  help            Show this help message"
    echo ""
    echo "If no command is provided, you'll be prompted to choose an action."
    echo "Text files are stored in the current directory: $(pwd)"
}

# List all text files
list_text_files() {
    echo -e "${BLUE}Available text files in current directory:${NC}"
    if ls *.txt 2>/dev/null | grep -q .; then
        ls -1 *.txt 2>/dev/null | sed 's/\.txt$//' | while read -r file; do
            echo "  - $file"
        done
    else
        echo "  No text files found."
    fi
}

# Built-in editor function
edit_text() {
    local file_path="$1"
    local temp_file=$(mktemp)

    # Copy existing content to temp file if file exists
    if [[ -f "$file_path" ]]; then
        cp "$file_path" "$temp_file"
    fi

    echo -e "${BLUE}Built-in Text Editor${NC}"
    echo "======================================"
    echo "Commands:"
    echo "  :w    - Save and continue editing"
    echo "  :wq   - Save and quit"
    echo "  :q    - Quit without saving"
    echo "  :q!   - Force quit without saving"
    echo "======================================"

    # Show current content
    if [[ -s "$temp_file" ]]; then
        echo -e "${YELLOW}Current content:${NC}"
        cat -n "$temp_file"
        echo "======================================"
    fi

    echo "Enter your text (type :commands on new lines to control editor):"
    echo ""

    # Clear the temp file for new content
    > "$temp_file"

    while true; do
        read -r line

        case "$line" in
            ":w")
                cp "$temp_file" "$file_path"
                echo -e "${GREEN}Saved!${NC}"
                ;;
            ":wq")
                cp "$temp_file" "$file_path"
                echo -e "${GREEN}Saved and closing editor.${NC}"
                break
                ;;
            ":q")
                echo "Quit without saving? (y/n):"
                read -n 1 -r
                echo
                if [[ $REPLY =~ ^[Yy]$ ]]; then
                    echo "Exited without saving."
                    break
                fi
                ;;
            ":q!")
                echo "Forced quit without saving."
                break
                ;;
            *)
                echo "$line" >> "$temp_file"
                ;;
        esac
    done

    rm -f "$temp_file"
}

# Create a new text file
create_text_file() {
    local file_name="$1"
    local file_path="${file_name}.txt"

    if [[ -z "$file_name" ]]; then
        echo -e "${RED}Error: Please provide a text file name${NC}"
        return 1
    fi

    if [[ -f "$file_path" ]]; then
        echo -e "${YELLOW}Text file '$file_name' already exists.${NC}"
        read -p "Do you want to edit it instead? (y/n): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            edit_text_file "$file_name"
        fi
        return 0
    fi

    echo -e "${GREEN}Creating new text file: $file_name${NC}"

    # Create initial content
    echo "# $file_name" > "$file_path"
    echo "Created: $(date)" >> "$file_path"
    echo "" >> "$file_path"

    # Use built-in editor
    edit_text "$file_path"
    echo -e "${GREEN}Text file '$file_name' created.${NC}"
}

# Open a text file for viewing
open_text_file() {
    local file_name="$1"
    local file_path="${file_name}.txt"

    if [[ -z "$file_name" ]]; then
        echo -e "${RED}Error: Please provide a text file name${NC}"
        return 1
    fi

    if [[ ! -f "$file_path" ]]; then
        echo -e "${RED}Error: Text file '$file_name' not found${NC}"
        return 1
    fi

    echo -e "${BLUE}Opening text file: $file_name${NC}"
    echo "----------------------------------------"
    cat "$file_path"
    echo "----------------------------------------"
}

# Edit an existing text file
edit_text_file() {
    local file_name="$1"
    local file_path="${file_name}.txt"

    if [[ -z "$file_name" ]]; then
        echo -e "${RED}Error: Please provide a text file name${NC}"
        return 1
    fi

    if [[ ! -f "$file_path" ]]; then
        echo -e "${YELLOW}Text file '$file_name' doesn't exist.${NC}"
        read -p "Do you want to create it? (y/n): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            create_text_file "$file_name"
        fi
        return 0
    fi

    echo -e "${GREEN}Editing text file: $file_name${NC}"
    edit_text "$file_path"
    echo -e "${GREEN}Finished editing '$file_name'.${NC}"
}

# Delete a text file
delete_text_file() {
    local file_name="$1"
    local file_path="${file_name}.txt"

    if [[ -z "$file_name" ]]; then
        echo -e "${RED}Error: Please provide a text file name${NC}"
        return 1
    fi

    if [[ ! -f "$file_path" ]]; then
        echo -e "${RED}Error: Text file '$file_name' not found${NC}"
        return 1
    fi

    echo -e "${YELLOW}Are you sure you want to delete '$file_name'?${NC}"
    read -p "Type 'yes' to confirm: " -r
    if [[ $REPLY == "yes" ]]; then
        rm "$file_path"
        echo -e "${GREEN}Text file '$file_name' deleted.${NC}"
    else
        echo "Deletion cancelled."
    fi
}

# Search within text files
search_text_files() {
    local search_term="$1"

    if [[ -z "$search_term" ]]; then
        echo -e "${RED}Error: Please provide a search term${NC}"
        return 1
    fi

    if ! ls *.txt 2>/dev/null | grep -q .; then
        echo "No text files found to search."
        return 1
    fi

    echo -e "${BLUE}Searching for '$search_term' in text files:${NC}"
    grep -l "$search_term" *.txt 2>/dev/null | while read -r file; do
        file_name=$(basename "$file" .txt)
        echo -e "${GREEN}Found in: $file_name${NC}"
        grep -n --color=always "$search_term" "$file"
        echo ""
    done
}

# Interactive mode
interactive_mode() {
    echo "================="
    echo "EE : Easy Editor"
    echo "================="

    list_text_files
    echo ""
    echo "What would you like to do?"
    echo "c - Create a new text file."
    echo "o - Open a text file."
    echo "e - Edit a text file."
    echo "l - List all text files."
    echo "d - Delete a text file."
    echo "s - Search text files."
    echo "e - Exit."

    read -p "Choose an option: " -n 1 -r
    echo

    case $REPLY in
        c)
            read -p "Enter text file name: " file_name
            create_text_file "$file_name"
            ;;
        o)
            read -p "Enter text file name: " file_name
            open_text_file "$file_name"
            ;;
        e)
            read -p "Enter text file name: " file_name
            edit_text_file "$file_name"
            ;;
        l)
            list_text_files
            ;;
        d)
            read -p "Enter text file name: " file_name
            delete_text_file "$file_name"
            ;;
        s)
            read -p "Enter search term: " search_term
            search_text_files "$search_term"
            ;;
        e)
            echo "Goodbye!"
            exit 0
            ;;
        *)
            echo "Invalid option. Please try again."
            ;;
    esac
}

# Main script logic
main() {
    case "$1" in
        "create")
            create_text_file "$2"
            ;;
        "open")
            open_text_file "$2"
            ;;
        "edit")
            edit_text_file "$2"
            ;;
        "list")
            list_text_files
            ;;
        "delete")
            delete_text_file "$2"
            ;;
        "search")
            search_text_files "$2"
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        "")
            interactive_mode
            ;;
        *)
            echo -e "${RED}Unknown command: $1${NC}"
            show_help
            exit 1
            ;;
    esac
}

# Run EE : Easy Editor
main "$@"