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

Expose methods for performing raw print request #91

Closed
typfel opened this issue Mar 22, 2023 · 6 comments · Fixed by #103
Closed

Expose methods for performing raw print request #91

typfel opened this issue Mar 22, 2023 · 6 comments · Fixed by #103

Comments

@typfel
Copy link

typfel commented Mar 22, 2023

Problem

I'm implementing a full screen interactive tool which needs to manipulate the cursor position. My first attempt was using the animation feature to update the screen but I realised it doesn't fit my purpose because it is build to be rendered inline with other widgets by hooking into the print requests, which makes it not compatible with manipulating the cursor.

Ok, since my use case is much simpler than what Animation attempts to do I decided draw my animation on my own. Unfortunately this was not possible without changes the Mordant library since I need to send raw print requests to refresh the screen.

Suggestion

Expose sending raw print requests:

fun rawPrintln(message: String) {
    sendPrintRequest(PrintRequest(message, true))
}

fun rawPrint(message: String) {
    sendPrintRequest(PrintRequest(message, false))
}
@ajalt
Copy link
Owner

ajalt commented Mar 23, 2023

Can you give a little more context on what you're trying to do? Maybe we can come up with a solution that doesn't require you to deal with raw printing.

@typfel
Copy link
Author

typfel commented Mar 23, 2023

I'm building a chat client where I provide auto completion when typing commands so I'm capturing the keyboard input directly and is therefore managing the input field the cursor position in my code.

Chat title                             






                                               
      <Alice> Hello                                                  
        <Bob> Hello Alice                                                             
─────────────────────────────────────────────────────────────────────────────────────────────────────────
>  draft message                                                                                    

Today using rawPrint I do this:

terminal.rawPrint(buildString {
    append(terminal.cursor.getMoves {
        setPosition(0, 0)
    })
    append(terminal.render(chat(viewState, terminal.info.height)))
    append(terminal.cursor.getMoves {
        startOfLine()
        right(viewState.cursorPosition)
    })
})

So essentially

  1. Move cursor to top left of screen
  2. Render app which always covers the whole screen
  3. Update cursor position

I suppose I could hide the cursor and draw my own cursor.

Another issue had with the Animation class was that it was always inserting a new line at the end which causes issues when you try draw something which covers the whole screen.

@ajalt
Copy link
Owner

ajalt commented Apr 9, 2023

I just released a new version that adds a stop function to Animation, so you can stop it before you print your cursor movements. So you shouldn't need to do raw prints. Let me know if that works for you.

@typfel
Copy link
Author

typfel commented Apr 17, 2023

Cool, I'll try it out!

@typfel
Copy link
Author

typfel commented May 3, 2023

So tried it out and it kinda works with some issues:

  • I need to override and disable trailingLinebreak in Animation.kt to correctly render my full screen content.
  • There's some flickering because the screen is now updated using multiple calls to print

@ajalt
Copy link
Owner

ajalt commented Jun 14, 2023

I decided that there isn't any reasonable way for Animation to work out of the box if the cursor is moved while it's running, so I went ahead and exposed the rawPrint function so you can manage the cursor yourself.

I also added an option to omit the trailing line break from Animation if you still want to use it. It's kind of an odd option; i'd rather just never have a trailing line break, but without one you wouldn't be able to have multiple animations running at the same time.

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 a pull request may close this issue.

2 participants