初次提交
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
)
|
||||
|
||||
func usage(w io.Writer) {
|
||||
io.WriteString(w, "commands:\n")
|
||||
io.WriteString(w, completer.Tree(" "))
|
||||
}
|
||||
|
||||
// Function constructor - constructs new function for listing given directory
|
||||
func listFiles(path string) func(string) []string {
|
||||
return func(line string) []string {
|
||||
names := make([]string, 0)
|
||||
files, _ := ioutil.ReadDir(path)
|
||||
for _, f := range files {
|
||||
names = append(names, f.Name())
|
||||
}
|
||||
return names
|
||||
}
|
||||
}
|
||||
|
||||
var completer = readline.NewPrefixCompleter(
|
||||
readline.PcItem("mode",
|
||||
readline.PcItem("vi"),
|
||||
readline.PcItem("emacs"),
|
||||
),
|
||||
readline.PcItem("login"),
|
||||
readline.PcItem("say",
|
||||
readline.PcItemDynamic(listFiles("./"),
|
||||
readline.PcItem("with",
|
||||
readline.PcItem("following"),
|
||||
readline.PcItem("items"),
|
||||
),
|
||||
),
|
||||
readline.PcItem("hello"),
|
||||
readline.PcItem("bye"),
|
||||
),
|
||||
readline.PcItem("setprompt"),
|
||||
readline.PcItem("setpassword"),
|
||||
readline.PcItem("bye"),
|
||||
readline.PcItem("help"),
|
||||
readline.PcItem("go",
|
||||
readline.PcItem("build", readline.PcItem("-o"), readline.PcItem("-v")),
|
||||
readline.PcItem("install",
|
||||
readline.PcItem("-v"),
|
||||
readline.PcItem("-vv"),
|
||||
readline.PcItem("-vvv"),
|
||||
),
|
||||
readline.PcItem("test"),
|
||||
),
|
||||
readline.PcItem("sleep"),
|
||||
)
|
||||
|
||||
func filterInput(r rune) (rune, bool) {
|
||||
switch r {
|
||||
// block CtrlZ feature
|
||||
case readline.CharCtrlZ:
|
||||
return r, false
|
||||
}
|
||||
return r, true
|
||||
}
|
||||
|
||||
func main() {
|
||||
l, err := readline.NewEx(&readline.Config{
|
||||
Prompt: "\033[31m»\033[0m ",
|
||||
HistoryFile: "/tmp/readline.tmp",
|
||||
AutoComplete: completer,
|
||||
InterruptPrompt: "^C",
|
||||
EOFPrompt: "exit",
|
||||
|
||||
HistorySearchFold: true,
|
||||
FuncFilterInputRune: filterInput,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
setPasswordCfg := l.GenPasswordConfig()
|
||||
setPasswordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
|
||||
l.SetPrompt(fmt.Sprintf("Enter password(%v): ", len(line)))
|
||||
l.Refresh()
|
||||
return nil, 0, false
|
||||
})
|
||||
|
||||
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)
|
||||
switch {
|
||||
case strings.HasPrefix(line, "mode "):
|
||||
switch line[5:] {
|
||||
case "vi":
|
||||
l.SetVimMode(true)
|
||||
case "emacs":
|
||||
l.SetVimMode(false)
|
||||
default:
|
||||
println("invalid mode:", line[5:])
|
||||
}
|
||||
case line == "mode":
|
||||
if l.IsVimMode() {
|
||||
println("current mode: vim")
|
||||
} else {
|
||||
println("current mode: emacs")
|
||||
}
|
||||
case line == "login":
|
||||
pswd, err := l.ReadPassword("please enter your password: ")
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
println("you enter:", strconv.Quote(string(pswd)))
|
||||
case line == "help":
|
||||
usage(l.Stderr())
|
||||
case line == "setpassword":
|
||||
pswd, err := l.ReadPasswordWithConfig(setPasswordCfg)
|
||||
if err == nil {
|
||||
println("you set:", strconv.Quote(string(pswd)))
|
||||
}
|
||||
case strings.HasPrefix(line, "setprompt"):
|
||||
if len(line) <= 10 {
|
||||
log.Println("setprompt <prompt>")
|
||||
break
|
||||
}
|
||||
l.SetPrompt(line[10:])
|
||||
case strings.HasPrefix(line, "say"):
|
||||
line := strings.TrimSpace(line[3:])
|
||||
if len(line) == 0 {
|
||||
log.Println("say what?")
|
||||
break
|
||||
}
|
||||
go func() {
|
||||
for range time.Tick(time.Second) {
|
||||
log.Println(line)
|
||||
}
|
||||
}()
|
||||
case line == "bye":
|
||||
goto exit
|
||||
case line == "sleep":
|
||||
log.Println("sleep 4 second")
|
||||
time.Sleep(4 * time.Second)
|
||||
case line == "":
|
||||
default:
|
||||
log.Println("you said:", strconv.Quote(line))
|
||||
}
|
||||
}
|
||||
exit:
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# readline-im
|
||||
|
||||

|
||||
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
)
|
||||
import "log"
|
||||
|
||||
func main() {
|
||||
rl, err := readline.NewEx(&readline.Config{
|
||||
UniqueEditLine: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rl.Close()
|
||||
|
||||
rl.SetPrompt("username: ")
|
||||
username, err := rl.Readline()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
rl.ResetHistory()
|
||||
log.SetOutput(rl.Stderr())
|
||||
|
||||
fmt.Fprintln(rl, "Hi,", username+"! My name is Dave.")
|
||||
rl.SetPrompt(username + "> ")
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
rand.Seed(time.Now().Unix())
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case <-time.After(time.Duration(rand.Intn(20)) * 100 * time.Millisecond):
|
||||
case <-done:
|
||||
break loop
|
||||
}
|
||||
log.Println("Dave:", "hello")
|
||||
}
|
||||
log.Println("Dave:", "bye")
|
||||
done <- struct{}{}
|
||||
}()
|
||||
|
||||
for {
|
||||
ln := rl.Line()
|
||||
if ln.CanContinue() {
|
||||
continue
|
||||
} else if ln.CanBreak() {
|
||||
break
|
||||
}
|
||||
log.Println(username+":", ln.Line)
|
||||
}
|
||||
rl.Clean()
|
||||
done <- struct{}{}
|
||||
<-done
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rl, err := readline.NewEx(&readline.Config{
|
||||
Prompt: "> ",
|
||||
HistoryFile: "/tmp/readline-multiline",
|
||||
DisableAutoSaveHistory: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rl.Close()
|
||||
|
||||
var cmds []string
|
||||
for {
|
||||
line, err := rl.Readline()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
line = strings.TrimSpace(line)
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
cmds = append(cmds, line)
|
||||
if !strings.HasSuffix(line, ";") {
|
||||
rl.SetPrompt(">>> ")
|
||||
continue
|
||||
}
|
||||
cmd := strings.Join(cmds, " ")
|
||||
cmds = cmds[:0]
|
||||
rl.SetPrompt("> ")
|
||||
rl.SaveHistory(cmd)
|
||||
println(cmd)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
// This is a small example using readline to read a password
|
||||
// and check it's strength while typing using the zxcvbn library.
|
||||
// Depending on the strength the prompt is colored nicely to indicate strength.
|
||||
//
|
||||
// This file is licensed under the WTFPL:
|
||||
//
|
||||
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
// Version 2, December 2004
|
||||
//
|
||||
// Copyright (C) 2004 Sam Hocevar <[email protected]>
|
||||
//
|
||||
// Everyone is permitted to copy and distribute verbatim or modified
|
||||
// copies of this license document, and changing it is allowed as long
|
||||
// as the name is changed.
|
||||
//
|
||||
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
//
|
||||
// 0. You just DO WHAT THE FUCK YOU WANT TO.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
zxcvbn "github.com/nbutton23/zxcvbn-go"
|
||||
)
|
||||
|
||||
const (
|
||||
Cyan = 36
|
||||
Green = 32
|
||||
Magenta = 35
|
||||
Red = 31
|
||||
Yellow = 33
|
||||
BackgroundRed = 41
|
||||
)
|
||||
|
||||
// Reset sequence
|
||||
var ColorResetEscape = "\033[0m"
|
||||
|
||||
// ColorResetEscape translates a ANSI color number to a color escape.
|
||||
func ColorEscape(color int) string {
|
||||
return fmt.Sprintf("\033[0;%dm", color)
|
||||
}
|
||||
|
||||
// Colorize the msg using ANSI color escapes
|
||||
func Colorize(msg string, color int) string {
|
||||
return ColorEscape(color) + msg + ColorResetEscape
|
||||
}
|
||||
|
||||
func createStrengthPrompt(password []rune) string {
|
||||
symbol, color := "", Red
|
||||
strength := zxcvbn.PasswordStrength(string(password), nil)
|
||||
|
||||
switch {
|
||||
case strength.Score <= 1:
|
||||
symbol = "✗"
|
||||
color = Red
|
||||
case strength.Score <= 2:
|
||||
symbol = "⚡"
|
||||
color = Magenta
|
||||
case strength.Score <= 3:
|
||||
symbol = "⚠"
|
||||
color = Yellow
|
||||
case strength.Score <= 4:
|
||||
symbol = "✔"
|
||||
color = Green
|
||||
}
|
||||
|
||||
prompt := Colorize(symbol, color)
|
||||
if strength.Entropy > 0 {
|
||||
entropy := fmt.Sprintf(" %3.0f", strength.Entropy)
|
||||
prompt += Colorize(entropy, Cyan)
|
||||
} else {
|
||||
prompt += Colorize(" ENT", Cyan)
|
||||
}
|
||||
|
||||
prompt += Colorize(" New Password: ", color)
|
||||
return prompt
|
||||
}
|
||||
|
||||
func main() {
|
||||
rl, err := readline.New("")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rl.Close()
|
||||
|
||||
setPasswordCfg := rl.GenPasswordConfig()
|
||||
setPasswordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
|
||||
rl.SetPrompt(createStrengthPrompt(line))
|
||||
rl.Refresh()
|
||||
return nil, 0, false
|
||||
})
|
||||
|
||||
pswd, err := rl.ReadPasswordWithConfig(setPasswordCfg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Your password was:", string(pswd))
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import "github.com/chzyer/readline"
|
||||
|
||||
func main() {
|
||||
if err := readline.DialRemote("tcp", ":12344"); err != nil {
|
||||
println(err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := &readline.Config{
|
||||
Prompt: "readline-remote: ",
|
||||
}
|
||||
handleFunc := func(rl *readline.Instance) {
|
||||
for {
|
||||
line, err := rl.Readline()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
fmt.Fprintln(rl.Stdout(), "receive:"+line)
|
||||
}
|
||||
}
|
||||
err := readline.ListenRemote("tcp", ":12344", cfg, handleFunc)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user