From 03863ce1a0d9b03356c0e64267c81971227ef4cb Mon Sep 17 00:00:00 2001 From: justinsb Date: Mon, 16 Jun 2025 13:52:53 -0400 Subject: [PATCH] html: better formatting for input text --- cmd/main.go | 1 + pkg/ui/blocks.go | 17 +++++++++++++++++ pkg/ui/html/htmlui.go | 1 + pkg/ui/html/templates/input_text_block.html | 6 ++++++ pkg/ui/observable.go | 7 +++++++ 5 files changed, 32 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index fd7f0067..3ba885b7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -531,6 +531,7 @@ func (s *session) repl(ctx context.Context, initialQuery string, initialBlocks [ for { if query == "" { input := ui.NewInputTextBlock() + input.SetEditable(true) s.doc.AddBlock(input) userInput, err := input.Observable().Wait() diff --git a/pkg/ui/blocks.go b/pkg/ui/blocks.go index 4569d826..4ffc0213 100644 --- a/pkg/ui/blocks.go +++ b/pkg/ui/blocks.go @@ -139,6 +139,9 @@ type InputTextBlock struct { // text is populated when we have input from the user text Observable[string] + + // editable is true if the input text block is editable + editable bool } func NewInputTextBlock() *InputTextBlock { @@ -157,6 +160,20 @@ func (b *InputTextBlock) Observable() *Observable[string] { return &b.text } +func (b *InputTextBlock) SetEditable(editable bool) *InputTextBlock { + b.editable = editable + b.doc.blockChanged(b) + return b +} + +func (b *InputTextBlock) Editable() bool { + return b.editable +} + +func (b *InputTextBlock) Text() (string, error) { + return b.text.Get() +} + // InputOptionBlock is used to prompt for a selection from multiple choices type InputOptionBlock struct { doc *Document diff --git a/pkg/ui/html/htmlui.go b/pkg/ui/html/htmlui.go index fb0e6ae9..9a6f2d10 100644 --- a/pkg/ui/html/htmlui.go +++ b/pkg/ui/html/htmlui.go @@ -138,6 +138,7 @@ func (u *HTMLUserInterface) handlePOSTSendMessage(w http.ResponseWriter, req *ht } inputBlock.Observable().Set(q, nil) + inputBlock.SetEditable(false) var bb bytes.Buffer bb.WriteString("ok") diff --git a/pkg/ui/html/templates/input_text_block.html b/pkg/ui/html/templates/input_text_block.html index 5eb791ef..0cfb3a2e 100644 --- a/pkg/ui/html/templates/input_text_block.html +++ b/pkg/ui/html/templates/input_text_block.html @@ -1,3 +1,9 @@ +{{ if .Editable }}
+{{ else }} +
+ {{ .Text }} +
+{{ end }} diff --git a/pkg/ui/observable.go b/pkg/ui/observable.go index 87554cbf..4ae96aa3 100644 --- a/pkg/ui/observable.go +++ b/pkg/ui/observable.go @@ -75,6 +75,13 @@ func (o *Observable[T]) Set(t T, err error) { o.sendChangeHoldingLock() } +func (o *Observable[T]) Get() (T, error) { + o.mutex.Lock() + defer o.mutex.Unlock() + + return o.value, o.err +} + func (o *Observable[T]) sendChangeHoldingLock() { for i, s := range o.subscriptions { if s == nil {