2020-06-08 03:29:51 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path/filepath"
|
2022-01-29 13:30:51 +03:00
|
|
|
"runtime"
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2023-04-24 00:43:53 +03:00
|
|
|
"github.com/wader/fq/internal/mathex"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2023-04-24 00:43:53 +03:00
|
|
|
"golang.org/x/term"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
2023-04-24 00:43:53 +03:00
|
|
|
"github.com/ergochat/readline"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-08-31 15:14:12 +03:00
|
|
|
func maybeLogFile() {
|
2021-08-25 14:47:33 +03:00
|
|
|
// used during dev to redirect log to file, useful when debugging repl etc
|
2020-06-08 03:29:51 +03:00
|
|
|
if lf := os.Getenv("LOGFILE"); lf != "" {
|
2021-08-25 14:47:33 +03:00
|
|
|
if f, err := os.Create(lf); err == nil {
|
|
|
|
log.SetOutput(f)
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:51:05 +03:00
|
|
|
// function implementing readline.AutoComplete interface
|
2020-06-08 03:29:51 +03:00
|
|
|
type autoCompleterFn func(line []rune, pos int) (newLine [][]rune, length int)
|
|
|
|
|
|
|
|
func (a autoCompleterFn) Do(line []rune, pos int) (newLine [][]rune, length int) {
|
|
|
|
return a(line, pos)
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
type stdOS struct {
|
2021-11-01 14:57:55 +03:00
|
|
|
rl *readline.Instance
|
2023-03-09 08:53:05 +03:00
|
|
|
historyFile string
|
2021-11-25 15:33:16 +03:00
|
|
|
closeChan chan struct{}
|
2021-11-01 14:57:55 +03:00
|
|
|
interruptChan chan struct{}
|
2023-03-10 15:51:05 +03:00
|
|
|
completerFn interp.CompleteFn
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func newStandardOS() *stdOS {
|
2021-11-25 15:33:16 +03:00
|
|
|
closeChan := make(chan struct{})
|
2020-06-08 03:29:51 +03:00
|
|
|
interruptChan := make(chan struct{}, 1)
|
2021-11-25 15:33:16 +03:00
|
|
|
|
|
|
|
// this more or less converts a os signal chan to just a struct{} chan that
|
|
|
|
// ignores signals if forwarding it would block, also this makes sure interp
|
|
|
|
// does not know about os.
|
2020-06-08 03:29:51 +03:00
|
|
|
go func() {
|
2021-11-25 15:33:16 +03:00
|
|
|
interruptSignalChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptSignalChan, os.Interrupt)
|
2021-11-01 14:57:55 +03:00
|
|
|
defer func() {
|
|
|
|
signal.Stop(interruptSignalChan)
|
|
|
|
close(interruptSignalChan)
|
2021-11-25 15:33:16 +03:00
|
|
|
close(interruptChan)
|
2021-11-01 14:57:55 +03:00
|
|
|
}()
|
2021-11-25 15:33:16 +03:00
|
|
|
|
|
|
|
for {
|
2020-06-08 03:29:51 +03:00
|
|
|
select {
|
2021-11-25 15:33:16 +03:00
|
|
|
case <-interruptSignalChan:
|
|
|
|
// ignore if interruptChan is full
|
|
|
|
select {
|
|
|
|
case interruptChan <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
case <-closeChan:
|
|
|
|
return
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
return &stdOS{
|
2021-11-25 15:33:16 +03:00
|
|
|
closeChan: closeChan,
|
2021-11-01 14:57:55 +03:00
|
|
|
interruptChan: interruptChan,
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 13:30:51 +03:00
|
|
|
func (stdOS) Platform() interp.Platform {
|
|
|
|
return interp.Platform{
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
type fdTerminal uintptr
|
|
|
|
|
|
|
|
func (fd fdTerminal) Size() (int, int) {
|
2023-04-24 00:43:53 +03:00
|
|
|
w, h, _ := term.GetSize(int(fd))
|
|
|
|
// TODO: old version return 0 on no terminal
|
|
|
|
w, h = mathex.Max(0, w), mathex.Max(0, h)
|
2021-09-01 16:01:13 +03:00
|
|
|
return w, h
|
|
|
|
}
|
|
|
|
func (fd fdTerminal) IsTerminal() bool {
|
2023-04-24 00:43:53 +03:00
|
|
|
return term.IsTerminal(int(fd))
|
2021-09-01 16:01:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type stdinInput struct {
|
|
|
|
fdTerminal
|
|
|
|
fs.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *stdOS) Stdin() interp.Input {
|
|
|
|
return stdinInput{
|
|
|
|
fdTerminal: fdTerminal(os.Stdin.Fd()),
|
|
|
|
File: interp.FileReader{
|
|
|
|
R: os.Stdin,
|
|
|
|
FileInfo: interp.FixedFileInfo{
|
|
|
|
FName: "stdin",
|
|
|
|
FMode: fs.ModeIrregular,
|
|
|
|
},
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
type stdoutOutput struct {
|
|
|
|
fdTerminal
|
|
|
|
os *stdOS
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (o stdoutOutput) Write(p []byte) (n int, err error) {
|
|
|
|
// Let write go thru readline if it has been used. This to have ansi color emulation
|
|
|
|
// on windows thru readlins:s stdout rewriter
|
|
|
|
// TODO: check if tty instead? else only color when repl
|
2021-08-26 18:47:09 +03:00
|
|
|
if o.os.rl != nil {
|
|
|
|
return o.os.rl.Write(p)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return os.Stdout.Write(p)
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (o *stdOS) Stdout() interp.Output {
|
|
|
|
return stdoutOutput{fdTerminal: fdTerminal(os.Stdout.Fd()), os: o}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
type stderrOutput struct {
|
|
|
|
fdTerminal
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (o stderrOutput) Write(p []byte) (n int, err error) { return os.Stderr.Write(p) }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (o *stdOS) Stderr() interp.Output { return stderrOutput{fdTerminal: fdTerminal(os.Stderr.Fd())} }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-01 14:57:55 +03:00
|
|
|
func (o *stdOS) InterruptChan() chan struct{} { return o.interruptChan }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (*stdOS) Args() []string { return os.Args }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (*stdOS) Environ() []string { return os.Environ() }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (*stdOS) ConfigDir() (string, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
p, err := os.UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(p, "fq"), nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
type stdOSFS struct{}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (stdOSFS) Open(name string) (fs.File, error) { return os.Open(name) }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (*stdOS) FS() fs.FS { return stdOSFS{} }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-02-10 20:46:23 +03:00
|
|
|
func (o *stdOS) Readline(opts interp.ReadlineOpts) (string, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
if o.rl == nil {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
var historyFile string
|
|
|
|
cacheDir, err := os.UserCacheDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-08-26 11:54:08 +03:00
|
|
|
historyFile = filepath.Join(cacheDir, "fq/history")
|
2020-06-08 03:29:51 +03:00
|
|
|
_ = os.MkdirAll(filepath.Dir(historyFile), 0700)
|
|
|
|
|
2023-03-09 08:53:05 +03:00
|
|
|
cfg := &readline.Config{
|
2020-06-08 03:29:51 +03:00
|
|
|
HistoryFile: historyFile,
|
|
|
|
HistorySearchFold: true,
|
2023-04-24 00:43:53 +03:00
|
|
|
Undo: true,
|
2023-03-09 08:53:05 +03:00
|
|
|
}
|
2023-03-10 15:51:05 +03:00
|
|
|
cfg.AutoComplete = autoCompleterFn(func(line []rune, pos int) (newLine [][]rune, length int) {
|
2023-03-10 23:58:35 +03:00
|
|
|
if o.completerFn == nil {
|
2023-03-10 15:51:05 +03:00
|
|
|
return nil, 0
|
|
|
|
}
|
2023-03-09 08:53:05 +03:00
|
|
|
|
2023-03-10 15:51:05 +03:00
|
|
|
names, shared := o.completerFn(string(line), pos)
|
|
|
|
var runeNames [][]rune
|
|
|
|
for _, name := range names {
|
|
|
|
runeNames = append(runeNames, []rune(name[shared:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return runeNames, shared
|
|
|
|
})
|
2023-03-09 08:53:05 +03:00
|
|
|
o.rl, err = readline.NewEx(cfg)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-03-09 08:53:05 +03:00
|
|
|
o.historyFile = historyFile
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2023-03-10 15:51:05 +03:00
|
|
|
// inject completer to autocompleter
|
|
|
|
o.completerFn = opts.CompleteFn
|
|
|
|
|
2022-02-10 20:46:23 +03:00
|
|
|
o.rl.SetPrompt(opts.Prompt)
|
2020-06-08 03:29:51 +03:00
|
|
|
line, err := o.rl.Readline()
|
|
|
|
if errors.Is(err, readline.ErrInterrupt) {
|
|
|
|
return "", interp.ErrInterrupt
|
|
|
|
} else if errors.Is(err, io.EOF) {
|
|
|
|
return "", interp.ErrEOF
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return line, nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (o *stdOS) History() ([]string, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
// TODO: refactor history handling to use internal fs?
|
2023-03-09 08:53:05 +03:00
|
|
|
r, err := os.Open(o.historyFile)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
var hs []string
|
|
|
|
lineScanner := bufio.NewScanner(r)
|
|
|
|
for lineScanner.Scan() {
|
|
|
|
hs = append(hs, lineScanner.Text())
|
|
|
|
}
|
|
|
|
if err := lineScanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hs, nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
func (o *stdOS) Close() error {
|
2020-06-08 03:29:51 +03:00
|
|
|
// only close if is terminal otherwise ansi reset will write
|
|
|
|
// to stdout and mess up raw output
|
|
|
|
if o.rl != nil {
|
|
|
|
o.rl.Close()
|
|
|
|
}
|
2021-11-25 15:33:16 +03:00
|
|
|
close(o.closeChan)
|
2020-06-08 03:29:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
func Main(r *interp.Registry, version string) {
|
2020-06-08 03:29:51 +03:00
|
|
|
os.Exit(func() int {
|
2021-08-31 15:14:12 +03:00
|
|
|
defer maybeProfile()()
|
|
|
|
maybeLogFile()
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
sos := newStandardOS()
|
|
|
|
defer sos.Close()
|
|
|
|
i, err := interp.New(sos, r)
|
|
|
|
defer i.Stop()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(sos.Stderr(), err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-01-29 13:30:51 +03:00
|
|
|
if err := i.Main(context.Background(), sos.Stdout(), version); err != nil {
|
2024-02-07 19:04:24 +03:00
|
|
|
if ex, ok := err.(interp.Exiter); ok {
|
2020-06-08 03:29:51 +03:00
|
|
|
return ex.ExitCode()
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}())
|
|
|
|
}
|