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

Added OptionBreakLineCallback, to run a callback every time there's a line break #140

Merged
merged 3 commits into from
Aug 26, 2019

Conversation

odino
Copy link
Contributor

@odino odino commented Aug 16, 2019

It's sometimes useful to run a function everytime there's a line break -- Enter as well as, for example, ControlC.

With this PR, a new option is added to assign a callback that gets
called every time renderer.BreakLine() is called.

Added a test that makes sure the renderer doesn't break if the callback is
not specified, as well as to check that it runs ok when the callback executes.

Just to give a bit more of context: in ABS
we are trying to implement ControlR (reverse search), and need to clear
the search selection every time the user "clears" the console, either by
pressing enter or by clearing the current line (eg. ControlC).

… line break

It's useful to run a function everytime there's a line break -- Enter as well as, for example, ControlC.

With this PR, a new option is added to assign a callback that gets
called every time `renderer.BreakLine()` is called.

Added a test that makes sure the renderer doesn't break if the callback is
not specified, as well as to check that it runs ok when the callback executes.

Just to give a bit more of context: in [ABS](https://github.com/abs-lang/abs)
we are trying to implement ControlR (reverse search), and need to clear
the search selection every time the user "clears" the console, either by
pressing enter or by clearing the current line (eg. ControlC).
Copy link
Owner

@c-bata c-bata left a comment

Choose a reason for hiding this comment

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

Great! It looks useful.

I confirmed your changes works fine. LGTM!

@c-bata
Copy link
Owner

c-bata commented Aug 16, 2019

Now I consider following interface (Add prompt.Document argument in the function) might be useful for more use cases:

BreakLineCallback  func(Document)

render.go Outdated
@@ -12,6 +12,7 @@ type Render struct {
out ConsoleWriter
prefix string
livePrefixCallback func() (prefix string, useLivePrefix bool)
BreakLineCallback func()
Copy link
Owner

@c-bata c-bata Aug 16, 2019

Choose a reason for hiding this comment

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

@odino Could you add an argument of prompt.Document like completer? (please tell me if you have something your opinion about my proposal.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, let me know if you'd like anything else!

As general feedback: one thing that's currently hard with go-prompt is that most members are private, so accessing them is out of the question. There could be things like changing color after the user presses a specific keybind that could be very useful, but are now impossible to do since you can't access those members. My suggestion would be to review the public interface and maybe allow users to be able to access renderer, input parser, etc etc.

Copy link
Owner

Choose a reason for hiding this comment

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

Thank you for your feedbacks.

We can make the field public without breaking API changes. But we cannot make the public field private without breaking API changes. So in my opinion, I define the field as private if it's not necessary to make public. But as you said, we might need to review the public interface. Thanks!

Copy link
Owner

Choose a reason for hiding this comment

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

@odino Could you make BreaklineCallback private If there is no advantages to make it public in your usecases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, let me know if you need anything else!

Copy link
Owner

Choose a reason for hiding this comment

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

Thank you! LGTM!!
I'll merge this after passes CI checks!

@odino
Copy link
Contributor Author

odino commented Aug 18, 2019

@c-bata let me know if anything else is needed!

@odino
Copy link
Contributor Author

odino commented Aug 26, 2019

@c-bata can we get this merged? 😄

@c-bata
Copy link
Owner

c-bata commented Aug 26, 2019

I'm so sorry for late reply. Could you check this comment?

@c-bata c-bata merged commit 0f95e1d into c-bata:master Aug 26, 2019
@c-bata
Copy link
Owner

c-bata commented Aug 26, 2019

Thank you for your contribution 👍

@odino
Copy link
Contributor Author

odino commented Aug 28, 2019 via email

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.

2 participants