1
0

create new project

This commit is contained in:
falsycat 2025-01-05 16:10:46 +09:00
parent ee2c95ce6c
commit db59bfa114
7 changed files with 88 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/.dev/
__pycache__/
*.swp

3
sqle/__init__.py Normal file
View File

@ -0,0 +1,3 @@
# TODO

10
sqle/__main__.py Normal file
View File

@ -0,0 +1,10 @@
from imgui_bundle import imgui, imgui_ctx, immapp
import sqle.win
term = sqle.win.Terminal()
immapp.run(
window_title = "SQLE",
gui_function = lambda: term.gui(),
)

1
sqle/utl/__init__.py Normal file
View File

@ -0,0 +1 @@
from .future import Future, Promise

26
sqle/utl/future.py Normal file
View File

@ -0,0 +1,26 @@
from typing import Generic, TypeVar
from threading import Event
T = TypeVar("T")
class Future(Generic[T]):
_event: Event = Event()
_data : T|None = None
def get(self) -> T|None:
return self._data if self.filled() else None
def filled(self) -> bool:
return self._event.is_set()
class Promise(Future[T]):
def fill(self, data: T):
self._data = data
self._event.set()
return self
def is_filled(fut: Future[T]|None):
return fut.filled() if fut is not None else False
def is_filling(fut: Future[T]|None):
return (not fut.filled()) if fut is not None else False

1
sqle/win/__init__.py Normal file
View File

@ -0,0 +1 @@
from .terminal import Terminal

44
sqle/win/terminal.py Normal file
View File

@ -0,0 +1,44 @@
from imgui_bundle import imgui, imgui_ctx
from abc import ABCMeta, abstractmethod
from sqle.utl.future import Future, Promise, is_filling, is_filled
class Terminal:
def __init__(self):
self._sql = ""
self._io = _IO()
self._req_sql = None
self._req_fut = None
def gui(self) -> None:
with imgui_ctx.begin("my first window"):
modified, self._sql = imgui.input_text(
"##sql",
self._sql,
)
if imgui.is_item_deactivated_after_edit():
accept_enter = (self._req_sql != self._sql) or (not is_filling(self._req_fut))
if accept_enter:
# TODO: abort prev future
self._req_fut = self._io.try_exec(self._sql)
if is_filled(self._req_fut):
msg = self._req_fut.get()
imgui.text(msg if msg is not None else "OK")
# TODO: display table
# an interface the Terminal window wants for handling SQL
class IO(metaclass = ABCMeta):
@abstractmethod
def try_exec(self, sql: str) -> Promise[str|None]: # TODO: return also SQL result
pass
# an impl of IO for testing
class _IO(IO):
def try_exec(self, sql):
print("exec: "+sql)
return Promise().fill(None)