-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds `Func`-able properties to all fields to allow dynamic updates based on updates in other parts of the group. Note that bindings must be specified. Bindings allow the Function to know when it should update as the functoin re-evaluates only when the value of the binding changes.
- Loading branch information
1 parent
0a35040
commit c7d3231
Showing
20 changed files
with
1,228 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
# Debugging | ||
debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package huh | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/mitchellh/hashstructure/v2" | ||
) | ||
|
||
type Eval[T any] struct { | ||
val T | ||
fn func() T | ||
|
||
bindings any | ||
bindingsHash uint64 | ||
cache map[uint64]T | ||
|
||
loading bool | ||
loadingStart time.Time | ||
} | ||
|
||
const spinnerShowThreshold = 25 * time.Millisecond | ||
|
||
func hash(val any) uint64 { | ||
hash, _ := hashstructure.Hash(val, hashstructure.FormatV2, nil) | ||
return hash | ||
} | ||
|
||
func (e *Eval[T]) shouldUpdate() (bool, uint64) { | ||
if e.fn == nil { | ||
return false, 0 | ||
} | ||
newHash := hash(e.bindings) | ||
return e.bindingsHash != newHash, newHash | ||
} | ||
|
||
func (e *Eval[T]) loadFromCache() bool { | ||
val, ok := e.cache[e.bindingsHash] | ||
if ok { | ||
e.loading = false | ||
e.val = val | ||
} | ||
return ok | ||
} | ||
|
||
func (e *Eval[T]) update(val T) { | ||
e.val = val | ||
e.cache[e.bindingsHash] = val | ||
e.loading = false | ||
} | ||
|
||
type updateTitleMsg struct { | ||
id int | ||
hash uint64 | ||
title string | ||
} | ||
|
||
type updateDescriptionMsg struct { | ||
id int | ||
hash uint64 | ||
description string | ||
} | ||
|
||
type updatePlaceholderMsg struct { | ||
id int | ||
hash uint64 | ||
placeholder string | ||
} | ||
|
||
type updateSuggestionsMsg struct { | ||
id int | ||
hash uint64 | ||
suggestions []string | ||
} | ||
|
||
type updateOptionsMsg[T comparable] struct { | ||
id int | ||
hash uint64 | ||
options []Option[T] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"strconv" | ||
|
||
"github.com/charmbracelet/huh" | ||
) | ||
|
||
func main() { | ||
var value string = "Dynamic" | ||
|
||
f := huh.NewForm( | ||
huh.NewGroup( | ||
huh.NewInput().Value(&value).Title("Dynamic").Description("Dynamic"), | ||
huh.NewNote(). | ||
TitleFunc(func() string { return value }, &value). | ||
DescriptionFunc(func() string { return value }, &value), | ||
huh.NewSelect[string](). | ||
Height(7). | ||
TitleFunc(func() string { return value }, &value). | ||
DescriptionFunc(func() string { return value }, &value). | ||
OptionsFunc(func() []huh.Option[string] { | ||
var options []huh.Option[string] | ||
for i := 1; i < 6; i++ { | ||
options = append(options, huh.NewOption(value+" "+strconv.Itoa(i), value+strconv.Itoa(i))) | ||
} | ||
return options | ||
}, &value), | ||
huh.NewMultiSelect[string](). | ||
Height(7). | ||
TitleFunc(func() string { return value }, &value). | ||
DescriptionFunc(func() string { return value }, &value). | ||
OptionsFunc(func() []huh.Option[string] { | ||
var options []huh.Option[string] | ||
for i := 1; i < 6; i++ { | ||
options = append(options, huh.NewOption(value+" "+strconv.Itoa(i), value+strconv.Itoa(i))) | ||
} | ||
return options | ||
}, &value), | ||
huh.NewConfirm(). | ||
TitleFunc(func() string { return value }, &value). | ||
DescriptionFunc(func() string { return value }, &value), | ||
huh.NewText(). | ||
TitleFunc(func() string { return value }, &value). | ||
DescriptionFunc(func() string { return value }, &value). | ||
PlaceholderFunc(func() string { return value }, &value), | ||
), | ||
) | ||
err := f.Run() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.