Compare commits

...

8 commits

Author SHA1 Message Date
cf7ed2f66a
Merge pull request #46 from jepler/behavioral-fixes-new-textual 2025-07-24 13:21:10 -05:00
077113632b core: List available presets in --help 2025-07-24 10:31:43 -05:00
3710a5b93b Use new textual markdown streaming 2025-07-24 10:30:16 -05:00
eecfa79d29
Merge pull request #44 from jepler/behavioral-fixes-new-textual 2025-05-16 11:36:21 +02:00
5d60927395 Fix app bindings more
ctrl-c can now be copy instead of yank (yay!)
2025-04-23 20:19:37 +02:00
633c43502a fix weird sizing of SubmittableTextArea
.. by removing the top & bottom borders. For some reason these appeared
not to be accounted when sizing the widget to its content?  not sure.
anyway this improves it.
2025-04-22 20:36:36 +02:00
70cbd2f806 Fix F9 binding of textarea 2025-04-22 20:25:37 +02:00
a8ed8d08d3
Merge pull request #43 from jepler/remove-codeql
codeql is not adding value
2025-04-19 20:34:10 +02:00
4 changed files with 42 additions and 15 deletions

View file

@ -8,6 +8,6 @@ lorem-text
platformdirs platformdirs
pyperclip pyperclip
simple_parsing simple_parsing
textual[syntax] textual[syntax] >= 4
tiktoken tiktoken
websockets websockets

View file

@ -58,4 +58,4 @@ Markdown {
margin: 0 1 0 0; margin: 0 1 0 0;
} }
SubmittableTextArea { height: 3 } SubmittableTextArea { height: auto; min-height: 5; margin: 0; border: none; border-left: heavy $primary }

View file

@ -38,7 +38,7 @@ ANSI_SEQUENCES_KEYS["\x1b\n"] = (Keys.F9,) # type: ignore
class SubmittableTextArea(TextArea): class SubmittableTextArea(TextArea):
BINDINGS = [ BINDINGS = [
Binding("f9", "submit", "Submit", show=True), Binding("f9", "app.submit", "Submit", show=True),
Binding("tab", "focus_next", show=False, priority=True), # no inserting tabs Binding("tab", "focus_next", show=False, priority=True), # no inserting tabs
] ]
@ -51,10 +51,10 @@ def parser_factory() -> MarkdownIt:
class ChapMarkdown(Markdown, can_focus=True, can_focus_children=False): class ChapMarkdown(Markdown, can_focus=True, can_focus_children=False):
BINDINGS = [ BINDINGS = [
Binding("ctrl+y", "yank", "Yank text", show=True), Binding("ctrl+c", "app.yank", "Copy text", show=True),
Binding("ctrl+r", "resubmit", "resubmit", show=True), Binding("ctrl+r", "app.resubmit", "resubmit", show=True),
Binding("ctrl+x", "redraft", "redraft", show=True), Binding("ctrl+x", "app.redraft", "redraft", show=True),
Binding("ctrl+q", "toggle_history", "history toggle", show=True), Binding("ctrl+q", "app.toggle_history", "history toggle", show=True),
] ]
@ -75,7 +75,7 @@ class CancelButton(Button):
class Tui(App[None]): class Tui(App[None]):
CSS_PATH = "tui.css" CSS_PATH = "tui.css"
BINDINGS = [ BINDINGS = [
Binding("ctrl+c", "quit", "Quit", show=True, priority=True), Binding("ctrl+q", "quit", "Quit", show=True, priority=True),
] ]
def __init__( def __init__(
@ -145,7 +145,6 @@ class Tui(App[None]):
await self.container.mount_all( await self.container.mount_all(
[markdown_for_step(User(query)), output], before="#pad" [markdown_for_step(User(query)), output], before="#pad"
) )
tokens: list[str] = []
update: asyncio.Queue[bool] = asyncio.Queue(1) update: asyncio.Queue[bool] = asyncio.Queue(1)
for markdown in self.container.children: for markdown in self.container.children:
@ -166,15 +165,22 @@ class Tui(App[None]):
) )
async def render_fun() -> None: async def render_fun() -> None:
old_len = 0
while await update.get(): while await update.get():
if tokens: content = message.content
output.update("".join(tokens).strip()) new_len = len(content)
new_content = content[old_len:new_len]
if new_content:
if old_len:
await output.append(new_content)
else:
output.update(content)
self.container.scroll_end() self.container.scroll_end()
await asyncio.sleep(0.1) old_len = new_len
await asyncio.sleep(0.01)
async def get_token_fun() -> None: async def get_token_fun() -> None:
async for token in self.api.aask(session, query): async for token in self.api.aask(session, query):
tokens.append(token)
message.content += token message.content += token
try: try:
update.put_nowait(True) update.put_nowait(True)

View file

@ -44,6 +44,7 @@ else:
conversations_path = platformdirs.user_state_path("chap") / "conversations" conversations_path = platformdirs.user_state_path("chap") / "conversations"
conversations_path.mkdir(parents=True, exist_ok=True) conversations_path.mkdir(parents=True, exist_ok=True)
configuration_path = platformdirs.user_config_path("chap") configuration_path = platformdirs.user_config_path("chap")
preset_path = configuration_path / "preset"
class ABackend(Protocol): class ABackend(Protocol):
@ -363,7 +364,7 @@ def expand_splats(args: list[str]) -> list[str]:
result.append(a) result.append(a)
continue continue
if a.startswith("@:"): if a.startswith("@:"):
fn: pathlib.Path = configuration_path / "preset" / a[2:] fn: pathlib.Path = preset_path / a[2:]
else: else:
fn = pathlib.Path(a[1:]) fn = pathlib.Path(a[1:])
fn = maybe_add_txt_extension(fn) fn = maybe_add_txt_extension(fn)
@ -403,6 +404,19 @@ class MyCLI(click.Group):
except ModuleNotFoundError as exc: except ModuleNotFoundError as exc:
raise click.UsageError(f"Invalid subcommand {cmd_name!r}", ctx) from exc raise click.UsageError(f"Invalid subcommand {cmd_name!r}", ctx) from exc
def gather_preset_info(self) -> list[tuple[str, str]]:
result = []
for p in preset_path.glob("*"):
if p.is_file():
with p.open() as f:
first_line = f.readline()
if first_line.startswith("#"):
help_str = first_line[1:].strip()
else:
help_str = "(A comment on the first line would be shown here)"
result.append((f"@:{p.name}", help_str))
return result
def format_splat_options( def format_splat_options(
self, ctx: click.Context, formatter: click.HelpFormatter self, ctx: click.Context, formatter: click.HelpFormatter
) -> None: ) -> None:
@ -436,6 +450,13 @@ class MyCLI(click.Group):
the extension may be omitted.""" the extension may be omitted."""
) )
) )
formatter.write_paragraph()
if preset_info := self.gather_preset_info():
formatter.write_text("Presets found:")
formatter.write_paragraph()
formatter.indent()
formatter.write_dl(preset_info)
formatter.dedent()
def format_options( def format_options(
self, ctx: click.Context, formatter: click.HelpFormatter self, ctx: click.Context, formatter: click.HelpFormatter