|
| 1 | +import argparse |
| 2 | +import sys |
| 3 | + |
| 4 | +import colorama |
| 5 | + |
| 6 | +from aoi._misc import process_commands |
| 7 | +from aoi.sqlite import connect, run_sql |
| 8 | + |
| 9 | +parser = argparse.ArgumentParser("aoi", usage="aoi *[options]", description="SQLITE3 in CLI.", add_help=True) |
| 10 | + |
| 11 | +parser.add_argument("-c", "--connect", help="The sqlite file to connect.") |
| 12 | +parser.add_argument("-v", "--version", help="Check version of the application.", action="store_true") |
| 13 | + |
| 14 | + |
| 15 | +class Args(argparse.Namespace): |
| 16 | + connect: str | None |
| 17 | + version: str | None |
| 18 | + |
| 19 | + |
| 20 | +class Session: |
| 21 | + recent_queries: list[str] = [] |
| 22 | + running: bool |
| 23 | + |
| 24 | + def __init__(self, file: str) -> None: |
| 25 | + self.file = file |
| 26 | + |
| 27 | + connect(file) |
| 28 | + |
| 29 | + def start(self) -> None: |
| 30 | + self.running = True |
| 31 | + print( |
| 32 | + colorama.Fore.BLUE, |
| 33 | + colorama.Style.BRIGHT, |
| 34 | + "Welcome to ", |
| 35 | + colorama.Fore.CYAN, |
| 36 | + "\U0001f365 aoi", |
| 37 | + colorama.Fore.BLUE, |
| 38 | + "!", |
| 39 | + colorama.Style.RESET_ALL, |
| 40 | + f" [connected to: \033[1;33m{self.file}\033[0m]", |
| 41 | + sep="", |
| 42 | + end="\n", |
| 43 | + ) |
| 44 | + print( |
| 45 | + colorama.Fore.LIGHTBLUE_EX, |
| 46 | + "type :h for list of inbuilt commands.", |
| 47 | + colorama.Style.RESET_ALL, |
| 48 | + sep="", |
| 49 | + ) |
| 50 | + |
| 51 | + while self.running is True: |
| 52 | + data = input("> ") |
| 53 | + if data.startswith(":"): |
| 54 | + process_commands(data, self) |
| 55 | + continue |
| 56 | + while not data.strip().endswith(";"): |
| 57 | + data += f" {input('- ')}" |
| 58 | + |
| 59 | + run_sql(data, self) |
| 60 | + raise KeyboardInterrupt |
| 61 | + |
| 62 | + def stop(self) -> None: |
| 63 | + self.running = False |
| 64 | + |
| 65 | + |
| 66 | +def main() -> None: |
| 67 | + args: Args = parser.parse_args() |
| 68 | + file = args.connect or ":memory:" |
| 69 | + if args.version and args.connect is None: |
| 70 | + print( |
| 71 | + colorama.Fore.BLUE, |
| 72 | + colorama.Style.BRIGHT, |
| 73 | + "aoi\U0001f365 version: ", |
| 74 | + colorama.Fore.GREEN, |
| 75 | + colorama.Style.NORMAL, |
| 76 | + "0.1.0\n", |
| 77 | + colorama.Fore.BLUE, |
| 78 | + colorama.Style.BRIGHT, |
| 79 | + "Python version: ", |
| 80 | + colorama.Style.NORMAL, |
| 81 | + colorama.Fore.GREEN, |
| 82 | + sys.version, |
| 83 | + colorama.Style.RESET_ALL, |
| 84 | + sep="", |
| 85 | + ) |
| 86 | + return |
| 87 | + try: |
| 88 | + (session := Session(file)).start() |
| 89 | + except KeyboardInterrupt: |
| 90 | + print( |
| 91 | + "\n" if session.running else "", |
| 92 | + colorama.Fore.BLUE, |
| 93 | + colorama.Style.BRIGHT, |
| 94 | + "Thank you for using ", |
| 95 | + colorama.Fore.CYAN, |
| 96 | + "\U0001f365 aoi", |
| 97 | + colorama.Fore.BLUE, |
| 98 | + "!", |
| 99 | + sep="", |
| 100 | + ) |
0 commit comments