How do I implement cmd's auto-tab-completion to python?

25 Views Asked by At
import os
import subprocess
import tkinter.colorchooser as colorchooser
import tkinter as tk
import sys
import decimal
import time

<...code...>

def execute_command(command):
    print(command)
    global numbers, operators, integer1, integer2, operator, operated
    inputs = command.split()
    current_directory = os.getcwd()
    current_directory_files = os.listdir(current_directory)

    if command in current_directory_files:
        return os.path.abspath(command)

    path_dirs = os.environ["PATH"].split(os.pathsep)
    for path_dir in path_dirs:
        try:
            path_dir_files = os.listdir(path_dir)
            for file in path_dir_files:
                if file.lower() == command.lower():
                    return os.path.abspath(os.path.join(path_dir, file))
        except FileNotFoundError:
            pass

    for extension in [".exe", ".bat"]:
        executable = f"{command}{extension}"
        if executable in current_directory_files:
            return os.path.abspath(executable)
        for path_dir in path_dirs:
            try:
                path_dir_files = os.listdir(path_dir)
                for file in path_dir_files:
                    if file.lower() == executable.lower():
                        return os.path.abspath(os.path.join(path_dir, file))
            except FileNotFoundError:
                pass

    if len(inputs) == 3 and inputs[1] in operators:
        try:
            first = int(inputs[0])
            second = int(inputs[2])
            operation = inputs[1]
            if operation == "+":
                print(first + second)
                operated = True
            elif operation == "-":
                print(first - second)
                operated = True
            elif operation == "*":
                print(first * second)
                operated = True
            elif operation == "/":
                if second != 0:
                    print(first / second)
                    operated = True
                else:
                    print("Undefined.")
                    operated = False
            else:
                operated = False
        except ValueError:
            operated = False
    elif len(inputs) == 2 and "+" in inputs[1]:
        try:
            first, second = map(int, inputs[1].split("+"))
            print(first + second)
            operated = True
        except ValueError:
            operated = False

    elif "+" in command:
        try:
            first, second = map(int, command.split("+"))
            print(first + second)
            operated = True
        except ValueError:
            operated = False

    if len(inputs) == 1 and inputs[0].isnumeric():
        print(inputs[0])
        operated = True
        return None  # Added to prevent further processing

    return None


def listdirectory():
    for item in os.listdir():
        print(item)

while True:
    current_directory = os.getcwd()
    command = input(f"{current_directory}> ")
    commandlist = command.split()
    executable = execute_command(commandlist[0])
    if command.lower() == "exit":
        sys.exit(0)
    elif operated:
        operated = False
    elif executable:
        subprocess.run([executable] + commandlist[1:])
    else:
        print(f'"{command}" is an unknown command, executable, path file, or a math operation.')
    print("")

Sorry for messy code, I have written an "Alternative Command Prompt" with some fun tools in addition to some really simple commands. I have searched for days trying to find a way to implement cmd's tab completion to my code. Can someone please help?

I tried multiple things such as using pyreadline or readline but none of them seemed to be able to replicate the behavior in Windows's cmd. There are some other modules that kinda seemed to work, but it wasn't really functioning the way I wanted.

0

There are 0 best solutions below