MCPcopy Create free account
hub / github.com/danielmiessler/Fabric / readLine

Function readLine

internal/plugins/plugin.go:394–416  ·  view source on GitHub ↗

readLine reads a single line from stdin and accepts either \n or \r as the line ending. Terminals such as kitty on macOS send a bare \r when Enter is pressed, and fmt.Scanln waited for a \n that never arrived.

()

Source from the content-addressed store, hash-verified

392// line ending. Terminals such as kitty on macOS send a bare \r when Enter is
393// pressed, and fmt.Scanln waited for a \n that never arrived.
394func readLine() (string, error) {
395 reader := stdinLineReader()
396
397 var line []rune
398 for {
399 ch, _, err := reader.ReadRune()
400 if err != nil {
401 return string(line), err
402 }
403 switch ch {
404 case '\n':
405 return string(line), nil
406 case '\r':
407 // Drop the \n of a \r\n ending so it is not read as an empty
408 // answer at the next prompt.
409 if next, _, err := reader.ReadRune(); err == nil && next != '\n' {
410 _ = reader.UnreadRune()
411 }
412 return string(line), nil
413 }
414 line = append(line, ch)
415 }
416}

Callers 2

TestReadLine_LineEndingsFunction · 0.85
AskMethod · 0.85

Calls 1

stdinLineReaderFunction · 0.85

Tested by 1

TestReadLine_LineEndingsFunction · 0.68