def say_reverse(commands): """This is the little sister of say(), it outputs text backwards""" strings = list(commands[1:]) revstrings = [string[::-1] for string in strings] revstrings.reverse() output = " ".join(revstrings) txt.print_blue(output)
def random_number(commands): """This function generates a random number from 0 to top_int""" if commands[1].startswith("0x"): raise err.IncorrectIntegerBaseError elif commands[1].startswith("0o"): raise err.IncorrectIntegerBaseError elif commands[1].startswith("0b"): raise err.IncorrectIntegerBaseError else: top_int = int(commands[1]) + 1 random_num = random.randint(0, top_int) txt.print_blue(f"Your random number is: {random_num}")
def convert_bases(commands): """This converts base 10 integers to other bases (hex, oct, bin)""" base = commands[1] integer = commands[2] base_prefixes = ["0x", "0o", "0b"] base_10 = True for prefix in base_prefixes: if integer.startswith(prefix): base_10 = False else: continue if base_10: if int(base) == 16: integer = int(integer) txt.print_blue(f"Base 16 (Hex): {hex(integer)}") elif int(base) == 8: integer = int(integer) txt.print_blue(f"Base 8 (Octal): {oct(integer)}") elif int(base) == 2: integer = int(integer) txt.print_blue(f"Base 2 (Binary): {bin(integer)}") else: raise err.BaseNotSupportedError else: raise err.IncorrectIntegerBaseError
def to_base_10(commands): """This function converts an integer (hex, oct, bin) to base 10""" integer = commands[1] with open(invalid_digits / "invalid_bin.txt", "rt") as inv_bin: invalid_bin = [invalid.strip() for invalid in inv_bin] with open(invalid_digits / "invalid_oct.txt", "rt") as inv_oct: invalid_oct = [invalid.strip() for invalid in inv_oct] with open(invalid_digits / "invalid_hex.txt", "rt") as inv_hex: invalid_hex = [invalid.strip() for invalid in inv_hex] if integer.startswith("0x"): integer = integer.replace("0x", "") if any(invalid in integer for invalid in invalid_hex): raise err.IncorrectIntegerBaseError else: integer = "0x" + integer integer = int(integer, 16) txt.print_blue(f"Base 10: {integer}") elif integer.startswith("0o"): integer = integer.replace("0o", "") if any(invalid in integer for invalid in invalid_oct): raise err.IncorrectIntegerBaseError else: integer = "0o" + integer integer = int(integer, 8) txt.print_blue(f"Base 10: {integer}") elif integer.startswith("0b"): integer = integer.replace("0b", "") if any(invalid in integer for invalid in invalid_bin): raise err.IncorrectIntegerBaseError else: integer = "0b" + integer integer = int(integer, 2) txt.print_blue(f"Base 10: {integer}") else: raise err.BaseNotSupportedError
def say_options(): """This returns the contents of the say-options file, which are the options for the 'say' command""" with open(command_options / "say-options.txt", "rt") as options: for option in options: txt.print_blue(option.rstrip())
def say(commands): """This function is called when 'say' is typed; it outputs text using six different options""" string = " ".join(commands[2:]) if commands[1].lower() == "all-caps": txt.print_blue(string.upper()) elif commands[1].lower() == "no-caps": txt.print_blue(string.lower()) elif commands[1].lower() == "cap-first": txt.print_blue(string.capitalize()) elif commands[1].lower() == "title": txt.print_blue(string.title()) elif commands[1].lower() == "swapped-case": txt.print_blue(string.swapcase()) else: txt.print_blue(" ".join(commands[1:]))
def shapes(): """This function reads the innards of shapes.txt; supported shapes for 'draw'""" with open(command_options / "shapes.txt", "rt") as shapes_file: for shape in shapes_file: txt.print_blue(shape.rstrip())
def help_command(): """This function returns the contents of help-command.txt; contains all different commands""" with open(command_options / "help-command.txt", "rt") as commands: for command in commands: txt.print_blue(command.rstrip())
"Please enjoy! Report any bugs (Traceback errors, etc.) to [email protected]." ) txt.print_magenta("See the documentation or type 'help' for more information.") # while is_running is True, it loops while is_running: # This is the prompt at which the user enters commands prompt = input(f"{GREEN}KShell: {RESET}") if echo_mode: if prompt == "echo-mode false" or prompt == "ecm false": echo_mode = False print( "\u2192 \033[38;2;128;0;0;4mEcho mode has been set to false!\033[m" ) elif prompt == "exit" or prompt == "x!": txt.print_blue("Bye!") is_running = False else: txt.print_cyan(prompt) else: # This splits the resulting input string into an list based on # whether or not there is a ' ' character separating input commands = prompt.split(" ") # This loops through each element in the commands list and # strips all whitespace off each string element for i in range(len(commands)): commands[i] = commands[i].strip() # if the command is 'say' then try to print message based on