github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/shell/editor.go (about)

     1  package shell
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  
     9  	"src.elv.sh/pkg/parse"
    10  	"src.elv.sh/pkg/strutil"
    11  )
    12  
    13  // This type is the interface that the line editor has to satisfy. It is needed so that this package
    14  // does not depend on the edit package.
    15  type editor interface {
    16  	ReadCode() (string, error)
    17  	RunAfterCommandHooks(src parse.Source, duration float64, err error)
    18  }
    19  
    20  type minEditor struct {
    21  	in  *bufio.Reader
    22  	out io.Writer
    23  }
    24  
    25  func newMinEditor(in, out *os.File) *minEditor {
    26  	return &minEditor{bufio.NewReader(in), out}
    27  }
    28  
    29  // RunAfterCommandHooks is a no-op in the minimum editor since it doesn't support
    30  // `edit:after-command` hooks. The method is needed to satisfy the `editor` interface.
    31  func (ed *minEditor) RunAfterCommandHooks(src parse.Source, duration float64, err error) {
    32  }
    33  
    34  func (ed *minEditor) ReadCode() (string, error) {
    35  	wd, err := os.Getwd()
    36  	if err != nil {
    37  		wd = "?"
    38  	}
    39  	fmt.Fprintf(ed.out, "%s> ", wd)
    40  	line, err := ed.in.ReadString('\n')
    41  	return strutil.ChopLineEnding(line), err
    42  }