Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(form): implement WithTimeout #276

Merged
merged 2 commits into from
Jun 8, 2024

Conversation

Delta456
Copy link
Contributor

@Delta456 Delta456 commented Jun 8, 2024

Implements #166

Demonstration

image

@Delta456 Delta456 requested a review from maaslalani as a code owner June 8, 2024 13:20
form.go Outdated Show resolved Hide resolved
@maaslalani
Copy link
Contributor

Hey @Delta456, thanks for the good work: I don't have push access to your repo so can you cherry pick the following commit in this PR and then we can merge:

This commit just adds a specific error for timeouts and handles the case when timeout is not set.

@Delta456
Copy link
Contributor Author

Delta456 commented Jun 8, 2024

Hey @Delta456, thanks for the good work: I don't have push access to your repo so can you cherry pick the following commit in this PR and then we can merge:

* [8ebd7d6](https://github.com/charmbracelet/huh/commit/8ebd7d69b5986894df066ad6d1ad3a096b656dbf)

This commit just adds a specific error for timeouts and handles the case when timeout is not set.

Done!

Forgot to consider the latter case

@maaslalani maaslalani merged commit 5b41f0b into charmbracelet:main Jun 8, 2024
20 checks passed
@Delta456 Delta456 deleted the form_with_timeout branch June 8, 2024 17:55
@korpa
Copy link

korpa commented Jun 17, 2024

@Delta456 Thank you very much. I tested it and I really like it. I have two ideas to improve it:

  1. Show a countdown
  2. Would it be possible to change the behaviour a little bit? Stop timeout as soon as a key is pressed. Otherwise it could happen, that the form aborts while I'm still filling it out. Especially in bigger forms.

What do you think?

@Delta456
Copy link
Contributor Author

@Delta456 Thank you very much. I tested it and I really like it. I have two ideas to improve it:

  1. Show a countdown
  2. Would it be possible to change the behaviour a little bit? Stop timeout as soon as a key is pressed. Otherwise it could happen, that the form aborts while I'm still filling it out. Especially in bigger forms.

What do you think?

Hi, thanks for the appreciation! I can try adding more if @maaslalani agrees too

  1. It's a good idea to show a countdown. What would be the measured time stamp?

  2. Exiting the timeout would beat the whole purpose of having a timeout. I feel extending the timeout would be better in bigger forms.

m, err := tea.NewProgram(f, f.teaOptions...).Run()
if m.(*Form).aborted {
err = ErrUserAborted
}
if err != nil && time.Since(startTime) >= f.timeout {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be err == nil? Also, every time I abort the form, I get a timeout error instead. This is because when f.timeout is zero, time.Since(startTime) will always be greater.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be err != nil because tea.NewProgram returns ErrProgramKilled . Though good catch!

renovate bot referenced this pull request in jippi/dottie Jul 10, 2024
….mod (#60)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/charmbracelet/huh](https://togithub.com/charmbracelet/huh)
| `v0.4.2` -> `v0.5.1` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fcharmbracelet%2fhuh/v0.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fcharmbracelet%2fhuh/v0.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fcharmbracelet%2fhuh/v0.4.2/v0.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fcharmbracelet%2fhuh/v0.4.2/v0.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>charmbracelet/huh (github.com/charmbracelet/huh)</summary>

###
[`v0.5.1`](https://togithub.com/charmbracelet/huh/compare/v0.5.0...v0.5.1)

[Compare
Source](https://togithub.com/charmbracelet/huh/compare/v0.5.0...v0.5.1)

###
[`v0.5.0`](https://togithub.com/charmbracelet/huh/releases/tag/v0.5.0)

[Compare
Source](https://togithub.com/charmbracelet/huh/compare/v0.4.2...v0.5.0)

### Dynamic Forms 🪄

<img width="600"
src="https://vhs.charm.sh/vhs-6FRmBjNi2aiRb4INPXwIjo.gif" alt="Country /
State form with dynamic inputs running.">

`huh?` forms can now react to changes in other parts of the form.
Replace properties such as `Options`, `Title`, `Description` with their
dynamic counterparts: `OptionsFunc`, `TitleFunc`, and `DescriptionFunc`
to recompute properties values on changes when watched variables change.

Let’s build a simple state / province picker.

```go
var country string
var state string
```

The `country` select will be static, we’ll use this value to recompute
the
options and title for the next input.

```go
huh.NewSelect[string]().
    Options(huh.NewOptions("United States", "Canada", "Mexico")...).
    Value(&country).
    Title("Country").
```

Define your `Select` with `TitleFunc` and `OptionsFunc` and bind them to
the
`&country` value from the previous field. Whenever the user chooses a
different
country, the `TitleFunc` and `OptionsFunc` will be recomputed.

> \[!IMPORTANT]
> We have to pass `&country` as the binding to recompute the function
only when
> `country` changes, otherwise we will hit the API too often.

```go
huh.NewSelect[string]().
    Value(&state).
    Height(8).
    TitleFunc(func() string {
        switch country {
        case "United States":
            return "State"
        case "Canada":
            return "Province"
        default:
            return "Territory"
        }
    }, &country).
    OptionsFunc(func() []huh.Option[string] {
        opts := fetchStatesForCountry(country)
        return huh.NewOptions(opts...)
    }, &country),
```

Lastly, run the `form` with these inputs.

```go
err := form.Run()
if err != nil {
    log.Fatal(err)
}
```

##### Other Changes

#### What's Changed

- Form Layouts by [@&#8203;adamdottv](https://togithub.com/adamdottv) in
[https://github.com/charmbracelet/huh/pull/274](https://togithub.com/charmbracelet/huh/pull/274)
- Resolve conflict between select and filter by
[@&#8203;MikaelFangel](https://togithub.com/MikaelFangel) in
[https://github.com/charmbracelet/huh/pull/252](https://togithub.com/charmbracelet/huh/pull/252)
- Add `CursorText` style by [@&#8203;nervo](https://togithub.com/nervo)
in
[https://github.com/charmbracelet/huh/pull/259](https://togithub.com/charmbracelet/huh/pull/259)
- Adjust input width to char limit by
[@&#8203;nervo](https://togithub.com/nervo) in
[https://github.com/charmbracelet/huh/pull/260](https://togithub.com/charmbracelet/huh/pull/260)
- Implement `WithInput` by
[@&#8203;Delta456](https://togithub.com/Delta456) in
[https://github.com/charmbracelet/huh/pull/271](https://togithub.com/charmbracelet/huh/pull/271)
- Implement WithTimeout by
[@&#8203;Delta456](https://togithub.com/Delta456) in
[https://github.com/charmbracelet/huh/pull/276](https://togithub.com/charmbracelet/huh/pull/276)
- Set filtering state of select by
[@&#8203;PJGaetan](https://togithub.com/PJGaetan) in
[https://github.com/charmbracelet/huh/pull/179](https://togithub.com/charmbracelet/huh/pull/179)
- `Filtering` on `Select` and `MultiSelect` fields by
[@&#8203;maaslalani](https://togithub.com/maaslalani) in
[https://github.com/charmbracelet/huh/pull/283](https://togithub.com/charmbracelet/huh/pull/283)
- Introduce `accessor` by [@&#8203;nervo](https://togithub.com/nervo) in
[https://github.com/charmbracelet/huh/pull/263](https://togithub.com/charmbracelet/huh/pull/263)
- fix: aborting a form returning a timeout error by
[@&#8203;Sculas](https://togithub.com/Sculas) in
[https://github.com/charmbracelet/huh/pull/287](https://togithub.com/charmbracelet/huh/pull/287)

#### New Contributors

- [@&#8203;MikaelFangel](https://togithub.com/MikaelFangel) made their
first contribution in
[https://github.com/charmbracelet/huh/pull/252](https://togithub.com/charmbracelet/huh/pull/252)
- [@&#8203;nervo](https://togithub.com/nervo) made their first
contribution in
[https://github.com/charmbracelet/huh/pull/253](https://togithub.com/charmbracelet/huh/pull/253)
- [@&#8203;shedyfreak](https://togithub.com/shedyfreak) made their first
contribution in
[https://github.com/charmbracelet/huh/pull/230](https://togithub.com/charmbracelet/huh/pull/230)
- [@&#8203;Delta456](https://togithub.com/Delta456) made their first
contribution in
[https://github.com/charmbracelet/huh/pull/271](https://togithub.com/charmbracelet/huh/pull/271)
- [@&#8203;abradley2](https://togithub.com/abradley2) made their first
contribution in
[https://github.com/charmbracelet/huh/pull/280](https://togithub.com/charmbracelet/huh/pull/280)
- [@&#8203;PJGaetan](https://togithub.com/PJGaetan) made their first
contribution in
[https://github.com/charmbracelet/huh/pull/179](https://togithub.com/charmbracelet/huh/pull/179)
- [@&#8203;Sculas](https://togithub.com/Sculas) made their first
contribution in
[https://github.com/charmbracelet/huh/pull/287](https://togithub.com/charmbracelet/huh/pull/287)

**Full Changelog**:
charmbracelet/huh@v0.4.2...v0.5.0

***

<a href="https://charm.sh/"><img alt="The Charm logo"
src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>

Thoughts? Questions? We love hearing from you. Feel free to reach out on
[Twitter](https://twitter.com/charmcli), [The
Fediverse](https://mastodon.technology/@&#8203;charm), or
[Slack](https://charm.sh/slack).

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "* */8 * * *" (UTC), Automerge - At
any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/jippi/dottie).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiXX0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants