batch

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2018 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Console = func(c *cli.Context) error {
	completions = append(
		completions,
		readline.PcItem("help"),
		readline.PcItem("exit"),
		readline.PcItem("quit"),
	)
	completer := readline.NewPrefixCompleter(completions...)
	shellwords.ParseEnv = true
	shellwords.ParseBacktick = true

	l, err := readline.NewEx(&readline.Config{
		Prompt:            "\033[35mcalends \033[36m» \033[0m",
		HistoryFile:       "",
		AutoComplete:      completer,
		InterruptPrompt:   "^C",
		EOFPrompt:         "exit",
		HistorySearchFold: true,
	})
	if err != nil {
		panic(err)
	}
	defer l.Close()

	printf = func(message string, args ...interface{}) {
		io.WriteString(l.Stdout(), fmt.Sprintf(message, args...))
	}

	log.SetOutput(l.Stderr())
	for {
		line, err := l.Readline()
		if err == readline.ErrInterrupt {
			if len(line) == 0 {
				break
			} else {
				continue
			}
		} else if err == io.EOF {
			break
		}

		line = strings.TrimSpace(line)

		if line == "" || line == "help" {
			printf("commands:\n")
			printf(completer.Tree("    "))
			continue
		} else if line == "quit" || line == "exit" {
			break
		}

		args, err := shellwords.Parse(line)
		if err != nil {
			log.Printf("%v\n", err)
		} else if cmd, ok := commands[args[0]]; ok {
			err = cmd(args[1:])
			if err != nil {
				log.Printf("%v\n", err)
			}
		} else {
			log.Printf("unknown command %q\n", args[0])
		}
	}
	return nil
}

Main logic

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL