-
-
Notifications
You must be signed in to change notification settings - Fork 9
Handle cursor area with custom writer #17
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
Conversation
15981f0
to
83eb5c3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that you do not have a Windows environment? If so, what would be the most appropriate way for you to implement it for Windows? Let me know if you need any help with that :)
Haha no worries ^^
Sounds good. If you're stuck at some point, give me a ping :) |
Codecov Report
@@ Coverage Diff @@
## main #17 +/- ##
========================================
+ Coverage 3.84% 4.86% +1.01%
========================================
Files 4 6 +2
Lines 130 185 +55
========================================
+ Hits 5 9 +4
- Misses 124 176 +52
+ Partials 1 0 -1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I have left some comments.
Just for reference:
We need to use the internal Writer
interface everywhere, instead of io.Writer
. Windows handles cursor movement via syscalls, which means we need a file descriptor. If you would do this:
func main() {
var buffer bytes.Buffer
a := cursor.NewArea().WithWriter(&buffer)
a.Update("Hello World!")
}
We would get this panic on Windows:
We have to do this for every operating system, as otherwise, Linux / macOS users would be able to do the example above, which would then panic on Windows. By accepting the internal Writer
interface, we make sure that there has to be a file descriptor.
The only downside to this is, that the area cannot write to a buffer, but that isn't really relevant, as there is no cursor to move in a buffer anyway.
The rest looks good to me :)
// PS:
Sorry for the delay, I was on vacation.
Done. pterm also requires a PR to make use of
Great!
Nice, will do the same next week. So in case we haven't merged then, we might need some more days then 🆗 |
I ran the area tests on my virtualized Windows 10 VM with good results so far. But would be good if you give it a try. Thanks for the support here. |
Just fixed the compile error on windows, caused by a missing commit. |
Hi @lammel, sorry for the late response, I was on vacation. // Update overwrites the content of the Area.
func (area *Area) Update(content string) {
oldWriter := target
SetTarget(area.writer) // Temporary set the target to the Area's writer so we can use the cursor functions
area.Clear()
lines := strings.Split(content, "\n")
fmt.Fprintln(area.writer, strings.Repeat("\n", len(lines)-1)) // This appends space if the terminal is at the bottom
Up(len(lines))
SetTarget(oldWriter) // Reset the target to the old writer
// Workaround for buggy behavior on Windows
if runtime.GOOS == "windows" {
for _, line := range lines {
fmt.Fprint(area.writer, line)
StartOfLineDown(1)
}
} else {
for _, line := range lines {
fmt.Fprintln(area.writer, line)
}
}
height = 0
area.height = len(strings.Split(content, "\n"))
} |
Hi @lammel, there are some linting errors, and they (finally) are reported correctly. You can see them here: https://github.com/atomicgo/cursor/pull/17/files Or if you have docker installed, you can use this command locally: Btw: You don't need to update the |
I'm using a locally installed golangci-lint now.
Thanks for noting, good to know and saves some time :) |
Actually I removed the newline for linux on purpose, as single line updates would otherwise leak into the next line, although this is not required and requires more cursor movement. |
I think that should work, the linux implementation didn't had any bugs. For Windows the idea64_uDtapqB6WY.mp4Also, I couldn't reproduce the bug on Windows consistently. The select and multiselect printers were affected all the time, but I couldn't really isolate the problem. Sometimes the Windows terminal is just a little weird. |
d5f4f59
to
c886465
Compare
As this is a pterm test that might also cause unexpected behaviour I've added some tests under I could reproduce 1 windows issue and one generic issue you were facing. The second issue was reproduced by Both of these issues are resolved now. The only remaining problem seems to be moving the cursor Your tests with pterm should run a lot smoother now. |
Looks great!
Could this be fixed by preventing to call |
Should be, just ran out of time yesterday, I'll look into it soon. |
After making the linter happy again, the above issues are fixed now. So only the pterms multiline input has issues with the custom cursor positioning, |
This PR is now ready to merge. All added examples here and for pterm work as expected on Linux and Windows. |
Ah one more note... This PR may break current behavior depending on the use of the cursor area. You might consider that when merging/releasing. |
Just to clarify, you mean the position in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for contributing 🚀
PS: Your examples look awesome!
Quick addition, I have invited you to this repo as collaborator (which gives you some more rights). If you want, you can accept the invitation here: https://github.com/atomicgo/cursor/invitations. Don't feel pressured by this to do more, but take it as appreciation :) |
Thanks! I feel a little pressured ;-) |
With this merge request using a custom writer is now possible by using a instanced cursor area.
Moving the cursor will be restricted to the size of the area. Multiline is also supported.
Marked as draft, as tests have only been conducted under Linux and probably some discussion is needed.