mirror of
https://github.com/SantaSpeen/CLI-in-Python.git
synced 2025-07-01 15:25:31 +00:00
Minor fixes
This commit is contained in:
parent
56c4212391
commit
069a7d9356
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
# Developed by Ahegao Devs
|
# Developed by Ahegao Devs
|
||||||
# Written by: SantaSpeen
|
# Written by: SantaSpeen
|
||||||
# Version 1.1
|
# Version 2.0
|
||||||
# Licence: MIT
|
# Licence: MIT
|
||||||
# (c) ahegao.ovh 2022
|
# (c) ahegao.su 2022
|
||||||
|
|
||||||
import builtins
|
import builtins
|
||||||
import logging
|
import logging
|
||||||
@ -33,11 +33,12 @@ class ConsoleIO:
|
|||||||
class Console:
|
class Console:
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
prompt_in: str = ">",
|
prompt_in=">",
|
||||||
prompt_out: str = "]:",
|
prompt_out="]:",
|
||||||
not_found: str = "Command \"%s\" not found in alias.",
|
not_found="Command \"%s\" not found in alias.",
|
||||||
file: str or None = ConsoleIO,
|
file=ConsoleIO,
|
||||||
debug: bool = False) -> None:
|
debug=False,
|
||||||
|
async_loop=None) -> None:
|
||||||
"""
|
"""
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
prompt_in: str = ">",
|
prompt_in: str = ">",
|
||||||
@ -78,13 +79,14 @@ class Console:
|
|||||||
|
|
||||||
print(item)
|
print(item)
|
||||||
|
|
||||||
def __get_max_len(self, arg) -> int:
|
@staticmethod
|
||||||
|
def __get_max_len(arg) -> int:
|
||||||
i = 0
|
i = 0
|
||||||
arg = list(arg)
|
arg = list(arg)
|
||||||
for a in arg:
|
for a in arg:
|
||||||
l = len(str(a))
|
ln = len(str(a))
|
||||||
if l > i:
|
if ln > i:
|
||||||
i = l
|
i = ln
|
||||||
return i
|
return i
|
||||||
|
|
||||||
def __create_man_message(self, argv: list) -> AnyStr:
|
def __create_man_message(self, argv: list) -> AnyStr:
|
||||||
@ -101,6 +103,7 @@ class Console:
|
|||||||
""" Print help message and alias of console commands"""
|
""" Print help message and alias of console commands"""
|
||||||
self.__debug("creating help message")
|
self.__debug("creating help message")
|
||||||
raw = False
|
raw = False
|
||||||
|
max_len_v = 0
|
||||||
if "--raw" in argv:
|
if "--raw" in argv:
|
||||||
max_len_v = self.__get_max_len(self.__alias.values())
|
max_len_v = self.__get_max_len(self.__alias.values())
|
||||||
print()
|
print()
|
||||||
@ -120,7 +123,6 @@ class Console:
|
|||||||
doc = v['f'].__doc__
|
doc = v['f'].__doc__
|
||||||
|
|
||||||
if raw:
|
if raw:
|
||||||
|
|
||||||
message += f"%-{max_len}s; %-{max_len_v}s; %s\n" % (k, v, doc)
|
message += f"%-{max_len}s; %-{max_len_v}s; %s\n" % (k, v, doc)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -210,7 +212,7 @@ class Console:
|
|||||||
cls.flush()
|
cls.flush()
|
||||||
except RecursionError:
|
except RecursionError:
|
||||||
raise
|
raise
|
||||||
except Exception:
|
except Exception as e:
|
||||||
cls.handleError(record)
|
cls.handleError(record)
|
||||||
|
|
||||||
logging.StreamHandler.emit = emit
|
logging.StreamHandler.emit = emit
|
||||||
@ -260,9 +262,28 @@ class Console:
|
|||||||
output = command()
|
output = command()
|
||||||
self.log(str(output))
|
self.log(str(output))
|
||||||
|
|
||||||
else:
|
async def async_run(self) -> None:
|
||||||
self.log(self.__not_found % cmd)
|
"""
|
||||||
except Exception as e:
|
def async_run(self) -> None:
|
||||||
ConsoleIO.write_err("\rDuring the execution of the command, an error occurred:\n\n" +
|
:return: None
|
||||||
str(traceback.format_exc()) +
|
"""
|
||||||
"\nType Enter to continue.")
|
self.is_run = True
|
||||||
|
await self.async_run_while(True)
|
||||||
|
|
||||||
|
async def async_run_while(self, whl) -> None:
|
||||||
|
"""
|
||||||
|
async def async_run_while(self, whl) -> None:
|
||||||
|
:param whl: run while what?
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
if ainput is None:
|
||||||
|
print("Install aioconsole for async! pip install aioconsole")
|
||||||
|
exit(1)
|
||||||
|
if self.async_loop is None:
|
||||||
|
self.async_loop = asyncio.get_event_loop()
|
||||||
|
self.__debug(f"async run while {whl}")
|
||||||
|
reader = asyncio.StreamReader()
|
||||||
|
while whl:
|
||||||
|
ConsoleIO.write("\r" + self.__prompt_in + " ")
|
||||||
|
res = await ainput()
|
||||||
|
self._cli_logic(res)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user