Giter Club home page Giter Club logo

Comments (9)

ben-lab avatar ben-lab commented on May 22, 2024 2

I have found an ugly workaround using go:linkname and SIGWINCH.
Probably good enough for pet projects, but be wary of the required import of "unsafe".

import (
	"syscall"
	_ "unsafe" // Required for go:linkname
	"github.com/c-bata/go-prompt"
)

// Map the local variable "consoleWriter" to the one of go-prompt
//go:linkname consoleWriter github.com/c-bata/go-prompt.consoleWriter
var consoleWriter prompt.ConsoleWriter

func AsyncWrite(str string) {
	consoleWriter.EraseLine() // Erase current line
	consoleWriter.EraseDown() // Required to remove the completions menu
	consoleWriter.WriteRawStr("\r" + str) // 'r' to go back to the start of line
	syscall.Kill(syscall.Getpid(), syscall.SIGWINCH) // Required to force the re-render of the prompt
}

Note: rather than using syscall.Kill, it is possible to use the reflection API, but it doesn't really make it cleaner.

from go-prompt.

BitonicEelis avatar BitonicEelis commented on May 22, 2024 1

I noticed that I can achieve the desired functionality by raising SIGWINCH after printing my output, since that will cause go-prompt to re-render the prompt. Bit crude solution, but it works, so feel free to close this issue if this solution is deemed adequate.

from go-prompt.

juanmera avatar juanmera commented on May 22, 2024

I'm also interested in this. The SIGWINCH workaround re-renders the prompt but the first line of the output still has the previous prompt prefixed (you have to add a \x0d for CR) and also part of the menu "sticks" to the output lines if the menu was open at the time of the print. Maybe there can be a channel where we could send async messages for rendering.

from go-prompt.

warpfork avatar warpfork commented on May 22, 2024

A p.GetScrollbackWriter() io.Writer method would be nice and should solve this need. Almost all programs can use a writer like that for the basis of their logging, etc, without needing internal modification to work well with go-prompt.

from go-prompt.

warpfork avatar warpfork commented on May 22, 2024

I did something like this before here (in a library that's much less cool than go-prompt)... the only thing I learned is that it's safest to always the cursor back up to the top of your footer/prompt area at the end of every render -- that means even uncontrolled prints to the terminal don't get overwritten, for the most part.

(It's still important to have the p.GetScrollbackWriter() io.Writer part and prefer to use it though; just printing to the terminal directly will both overwrite the footer/prompt, and it's preferable to do a clear before printing a new line in case the footer/prompt is longer than the new line.)

from go-prompt.

FrontMage avatar FrontMage commented on May 22, 2024

I think you can pause the executor by waitGroup or select to prevent new prompt pops up.

from go-prompt.

isacikgoz avatar isacikgoz commented on May 22, 2024

I was looking for this feature too. Bumping the issue.

from go-prompt.

bgospodaru avatar bgospodaru commented on May 22, 2024

I too am looking for a feature that can do this. I was hoping maybe there was a solution that works for Windows as well :(

from go-prompt.

malmod avatar malmod commented on May 22, 2024

Hitting this right now as well!

Sending SIGWINCH partially solves the problem but the main problem then is the upper line of the completions stays and is never cleared. That's what @juanmera said too.

There should really be a channel to send log messages as part of prompt.

from go-prompt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.