I am implementing a CLI application using golang.[Need cross platform compatibility]
My requirement is that when a user is being prompted for an input and after the user has finished typing if the user presses Ctrl+D without pressing Enter then some data processing should take place and user must get their response.
I tired looking for libraries like 'term-box' and 'tcell' for keyboard events but they are good at detecting keyboard keys and there is no option for getting any kind of user input.
I would like to have a standard input prompt along with keyboard events in golang for my CLI application.
Below is some reference code:
reader := bufio.NewReader(os.Stdin)
fmt.Println("#Please press CTRL+D to process data")
for {
fmt.Print("\n your-input>")
text, _ := reader.ReadString('\n')
if strings.HasSuffix(text, "\\\n") {
text1, _ := reader.ReadString('\n')
text += text1
}
//would like to have some kind of keyboard event here which will handle (Ctrl+D)
// if(key == Ctrl+D){
//then do some data processing
} else {
//let the user keep typing their text
}
text = strings.TrimSpace(strings.Replace(text, "\n", "", -1))
fmt.Print("\n response> " + getProcessedData(text))
}