Code Monkey home page Code Monkey logo

Comments (11)

abiosoft avatar abiosoft commented on May 28, 2024 3

Gonna bring this up the priority list. No ETA but working on the fix.

from ishell.

abiosoft avatar abiosoft commented on May 28, 2024

Ideally, shell must have started before prompt shows up. And that's why feed(c) outputs before the prompt.

Just to be clear, what is the expected behaviour ?

from ishell.

synw avatar synw commented on May 28, 2024

I want to permanently listen to a channel that will print some info in the shell from time to time.

package main

import (
    "time"
    "github.com/abiosoft/ishell"
)


func feed(c chan string) {
	time.Sleep(2*time.Second)
    for i := 0; i<3; i++ {
        c <- "Feedback"
        time.Sleep(time.Second)
    }
}

func main(){
    shell := ishell.New()
    c := make(chan string)
    
    shell.AddCmd(&ishell.Cmd{
        Name: "hi",
        Help: "hello",
        Func: func(ctx *ishell.Context) {
            ctx.Println("Hello")
            go feed(c)
        },
    })
    
    // listen
    go func() {
        for msg := range(c) {
            shell.Println(msg)
        }
    }()
    
    shell.Start()
}

This outputs:

>>> hi
Hello
>>> Feedback
Feedback
Feedback

The desired behavior is:

>>> hi
Hello
>>> 
Feedback
Feedback
Feedback
>>>

from ishell.

synw avatar synw commented on May 28, 2024

Ideally I would like to have a shell print channel to where I can throw output in from goroutines

from ishell.

abiosoft avatar abiosoft commented on May 28, 2024

You think a call to Println should not print on the same line as the prompt ?

from ishell.

synw avatar synw commented on May 28, 2024

Yes: if it can do the job it would be fine. It should preserve the text being typed:

>>> typing someth
Feedback
>>> typing someth

from ishell.

abiosoft avatar abiosoft commented on May 28, 2024

I would not totally agree with always printing on the next line as the password prompt in the example shows a use case. https://github.com/abiosoft/ishell/blob/master/example/main.go#L118.

I will try to find a middle ground and will also try to make it safe to have calls to Print from multiple goroutines.

from ishell.

synw avatar synw commented on May 28, 2024

Hi, any progress on this? Do you have plans for resolving this?

It could benefit to my user case: a client that permanently receives some messages and print them to the screen. The fact that printing them with ctx.Println messes up with the prompt make it uncomfortable to use when lots of messages are coming in.

from ishell.

danopia avatar danopia commented on May 28, 2024

I tried putting together a chat-style app and had real problems trying to print an incoming message without interrupting the prompt. Any in-progress input got printed multiple times in the terminal and it wasn't lining up. It seems there should be some way to print output that is independent of whatever input is happening.

from ishell.

matthijskooijman avatar matthijskooijman commented on May 28, 2024

I'm also running into this issue for printing background log lines, which now mess up the prompt.

I can see that repurposing the current Print/Prinln functions might not what is needed (though they are a bit weird now: AFAICS Print replaces the prompt content, but appends to the prompt on screen, while Println empties the prompt but not reshows the prompt).

Ideally, when printing a log line or similar, you would:

  • Erase the current prompt
  • Print the log line, with a newline
  • Print the prompt again, including anything typed already

I guess this would warrant a new function, something like shell.PrintlnBeforePrompt()? This is probably fairly easy to do as above for full lines.

Implementing something like shell.PrintBeforePrompt() (so without a newline) would be more tricky, since that should (I guess) append to the existing line before the prompt, which probably requires keeping track of the current cursor position (or erasing the newline before the prompt, but I'm not sure if terminals can actually do this). In any case, just printing full lines, with a newline, are probably sufficient for most usecases?

from ishell.

b3r1ch avatar b3r1ch commented on May 28, 2024

You can fix this problem like this:

package main

import (
    "time"
    "github.com/abiosoft/readline"
    "github.com/abiosoft/ishell"
)


func feed(c chan string) {
    for i := 0; i<3; i++ {
        c <- "Feedback"
        time.Sleep(time.Second)
    }
}

func main(){
      rl, _ := readline.NewEx(&readline.Config{
		      Prompt:          "\033[31m»\033[0m ",
		      InterruptPrompt: "^C",
		      EOFPrompt:       "exit",
      
		      HistorySearchFold: true,
	      })
    shell = ishell.NewWithReadline(rl)
    c := make(chan string)
    
    // listen
    go func() {
        for msg := range(c) {
            rl.Write([]byte(msg))
        }
    }()
    
    // this does output correctly
    //feed(c)
     this does not
    go feed(c)

    shell. Start()
}

from ishell.

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.