Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions pkg/ui/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/ui/html/htmlui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions pkg/ui/html/templates/input_text_block.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{{ if .Editable }}
<div>
<input type="text" name="q" hx-post="/send-message" placeholder="How can I help?">
</div>
{{ else }}
<div>
<span>{{ .Text }}</span>
</div>
{{ end }}
7 changes: 7 additions & 0 deletions pkg/ui/observable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading