Skip to content
This repository has been archived by the owner on May 17, 2023. It is now read-only.

Commit

Permalink
Added plain mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Di Antonio committed Mar 3, 2023
1 parent 7898f91 commit f4707ba
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 33 deletions.
5 changes: 3 additions & 2 deletions docs.md
Expand Up @@ -57,7 +57,8 @@ Note that, although every function has a default handler, it is recommended to p
As of version 1.2.1, CLIbrary has some "global options" to allow even more personalization.
Available options are:

1. `CLIbrary.style.setting_darkMode`, bool: Enables dark mode.
1. `CLIbrary.style.setting_darkMode`, bool: Enables global dark mode.
2. `CLIbrary.style.setting_plainMode`, bool: Disables styling.

### Import CLIbrary

Expand Down Expand Up @@ -262,6 +263,6 @@ The handler for this function makes use of the following parameters:
* `"warning"`,
* `"verbose"`,
* `"custom"`: Lets you define a custom output style.
* customStyle, str.
* style, str.
* before, str: A string that gets printed before the output and is unaffected by the output styling.
* after, str: A string that gets printed after the output and is unaffected by the output styling.
17 changes: 14 additions & 3 deletions src/CLIbrary/inputs.py
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime

from .outputs import *
from .settings import *

# INPUT HANDLING

Expand All @@ -11,7 +12,6 @@ def strIn(stringHandler={}) -> str: # String input.
handler["request"] = "Insert a string"
handler["addedChars"] = ": "
handler["allowedChars"] = []

handler["allowedAnswers"] = []
handler["allowedStyle"] = Fore.CYAN
handler["blockedAnswers"] = []
Expand All @@ -24,6 +24,9 @@ def strIn(stringHandler={}) -> str: # String input.

handler.update(stringHandler)

if style.setting_plainMode:
handler["allowedStyle"] = ""

charactersRange = list(range(0, 48)) + list(range(58, 65)) + list(range(91, 97)) + list(range(123, 256))
if not handler["noSpace"]:
charactersRange.remove(32)
Expand All @@ -47,7 +50,11 @@ def strIn(stringHandler={}) -> str: # String input.

try:
if handler["fixedLength"] > 0:
lengthString = Back.GREEN + Fore.MAGENTA + "[" + str(handler["fixedLength"]) + "]" + Style.RESET_ALL + " "
if style.setting_plainMode:
lengthString = "[" + str(handler["fixedLength"]) + "] "

else:
lengthString = Back.GREEN + Fore.MAGENTA + "[" + str(handler["fixedLength"]) + "]" + Style.RESET_ALL + " "
except:
handler["fixedLength"] = 0

Expand Down Expand Up @@ -189,7 +196,11 @@ def numIn(numberHandler={}) -> "int, float": # Number input.
handler["allowedRange"] = []

else:
rangeString = Fore.GREEN + "[" + str(handler["allowedRange"][0]) + ", " + str(handler["allowedRange"][1]) + "] " + Style.RESET_ALL
if style.setting_plainMode:
rangeString = "[" + str(handler["allowedRange"][0]) + ", " + str(handler["allowedRange"][1]) + "] "

else:
rangeString = Fore.GREEN + "[" + str(handler["allowedRange"][0]) + ", " + str(handler["allowedRange"][1]) + "] " + Style.RESET_ALL

except(IndexError, TypeError):
handler["allowedRange"] = []
Expand Down
75 changes: 51 additions & 24 deletions src/CLIbrary/interface.py
Expand Up @@ -12,17 +12,16 @@ def cmdIn(commandHandler={}) -> dict: # Command input.

handler["request"] = "enter a command"
handler["addedChars"] = ": "

handler["style"] = ""

handler["verbose"] = False

handler["allowedCommands"] = ["exit"]

handler["helpPath"] = ""

handler.update(commandHandler)

if style.setting_plainMode:
handler["style"] = ""

if "exit" not in handler["allowedCommands"]: # "exit" must be in the allowed commands.
handler["allowedCommands"].append("exit")

Expand Down Expand Up @@ -105,33 +104,61 @@ def helpPrint(handler={}) -> None:
font = Fore.WHITE
back = Back.WHITE

for key in helpJson:
helpString = ""

if key not in handler["allowedCommands"]:
helpString += Back.YELLOW + font + " UNAVAILABLE " + Style.RESET_ALL
if not style.setting_plainMode:
for key in helpJson:
helpString = ""

if key not in handler["allowedCommands"]:
helpString += Back.YELLOW + font + " UNAVAILABLE " + Style.RESET_ALL

helpString += Back.GREEN + font + " " + key + " " + back + Fore.GREEN + " " + helpJson[key]["description"] + " " + Style.RESET_ALL
helpString += Back.GREEN + font + " " + key + " " + back + Fore.GREEN + " " + helpJson[key]["description"] + " " + Style.RESET_ALL

if "options" in helpJson[key]:
helpString += Back.GREEN + font + " " + str(len(helpJson[key]["options"])) + " option(s) " + Style.RESET_ALL

for optionKey in helpJson[key]["options"]:
if "#" in optionKey:
helpString += "\n\t" + Back.RED + font + " " + optionKey.replace("#", "") + " "

if "--" not in optionKey:
helpString += back + Fore.RED + " " + helpJson[key]["options"][optionKey] + " "

else:
helpString += "\n\t" + Back.CYAN + font + " " + optionKey + " "

if "--" not in optionKey:
helpString += back + Fore.CYAN + " " + helpJson[key]["options"][optionKey] + " "

helpString += Style.RESET_ALL

helpElements.append(helpString)

if "options" in helpJson[key]:
helpString += Back.GREEN + font + " " + str(len(helpJson[key]["options"])) + " option(s) " + Style.RESET_ALL
else:
for key in helpJson:
helpString = ""

for optionKey in helpJson[key]["options"]:
if "#" in optionKey:
helpString += "\n\t" + Back.RED + font + " " + optionKey.replace("#", "") + " "
if key not in handler["allowedCommands"]:
helpString += "[UNAVAILABLE] "

if "--" not in optionKey:
helpString += back + Fore.RED + " " + helpJson[key]["options"][optionKey] + " "

else:
helpString += "\n\t" + Back.CYAN + font + " " + optionKey + " "
helpString += key + ": " + helpJson[key]["description"]

if "options" in helpJson[key]:
helpString += " [" + str(len(helpJson[key]["options"])) + " option(s)]"

for optionKey in helpJson[key]["options"]:
if "#" in optionKey:
helpString += "\n\t[MANDATORY] " + optionKey.replace("#", "")

else:
helpString += "\n\t" + optionKey

if "--" not in optionKey:
helpString += back + Fore.CYAN + " " + helpJson[key]["options"][optionKey] + " "

helpString += Style.RESET_ALL

helpElements.append(helpString)
helpString += ": " + helpJson[key]["options"][optionKey]
helpString += Style.RESET_ALL
helpElements.append(helpString)

print("\n\n".join(helpElements)) if len(helpElements) else output({"type": "warning", "string": "NO HELP FOR CURRENTLY AVAILABLE COMMANDS", "before": "\n"})

Expand Down
14 changes: 11 additions & 3 deletions src/CLIbrary/outputs.py
Expand Up @@ -8,6 +8,9 @@ def output(outputHandler: dict) -> None:
# Output type.
handler["type"] = ""

# Custom style
handler["style"] = ""

# Prints these style-unaffected strings before and after the main part.
handler["before"] = ""
handler["after"] = ""
Expand All @@ -28,8 +31,6 @@ def output(outputHandler: dict) -> None:
warningStyle = Back.YELLOW + Fore.WHITE + " \u25B2 " + Back.WHITE + Fore.YELLOW + " "
verboseStyle = Back.CYAN + Fore.WHITE + " \u25CF " + Back.WHITE + Fore.CYAN + " "

customStyle = ""

if handler["type"] == "error":
outputStyle = errorStyle

Expand All @@ -40,10 +41,17 @@ def output(outputHandler: dict) -> None:
outputStyle = verboseStyle

elif handler["type"] == "custom":
outputStyle = customStyle
outputStyle = handler["style"]

else:
output({"type": "warning", "string": "OUTPUT MISCONFIGURED. PLEASE REFER TO THE DOCUMENTATION.", "before": handler["before"], "after": handler["after"]})
outputStyle = ""

if style.setting_plainMode:
if handler["type"] in ["error", "warning", "verbose"]:
outputStyle = "[" + handler["type"].upper() + "] "

else:
outputStyle = ""

print(handler["before"] + outputStyle + handler["string"] + " " + Style.RESET_ALL + handler["after"])
3 changes: 2 additions & 1 deletion src/CLIbrary/settings.py
@@ -1,4 +1,5 @@
# Global settings for CLIbrary.

class style:
setting_darkMode = False
setting_darkMode = False # Enables global dark mode.
setting_plainMode = False # Disables styling.

0 comments on commit f4707ba

Please sign in to comment.