1
0
forked from x/icebergs

add ssh history

This commit is contained in:
shaoying 2020-03-09 20:24:28 +08:00
parent 6a83da3fd6
commit 5f36ef2b0f
3 changed files with 34 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -29,7 +30,7 @@ func (f *Frame) prompt(m *ice.Message) *Frame {
case "count": case "count":
fmt.Fprintf(f.out, kit.Format("%d", f.count)) fmt.Fprintf(f.out, kit.Format("%d", f.count))
case "time": case "time":
fmt.Fprintf(f.out, m.Time("15:04:05")) fmt.Fprintf(f.out, time.Now().Format("15:04:05"))
case "target": case "target":
fmt.Fprintf(f.out, f.target.Name) fmt.Fprintf(f.out, f.target.Name)
default: default:
@ -41,7 +42,7 @@ func (f *Frame) prompt(m *ice.Message) *Frame {
} }
func (f *Frame) printf(m *ice.Message, res string, arg ...interface{}) *Frame { func (f *Frame) printf(m *ice.Message, res string, arg ...interface{}) *Frame {
if len(arg) > 0 { if len(arg) > 0 {
fmt.Fprintf(f.out, res, arg) fmt.Fprintf(f.out, res, arg...)
} else { } else {
fmt.Fprint(f.out, res) fmt.Fprint(f.out, res)
} }
@ -150,10 +151,11 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool {
m.Cap(ice.CTX_STREAM, "stdio") m.Cap(ice.CTX_STREAM, "stdio")
f.target = m.Target() f.target = m.Target()
default: default:
// 解析脚本
if s, e := os.Open(arg[0]); m.Warn(e != nil, "%s", e) { if s, e := os.Open(arg[0]); m.Warn(e != nil, "%s", e) {
// 打开失败
return true return true
} else { } else {
// 解析脚本
defer s.Close() defer s.Close()
if f.in, f.out = s, os.Stdout; m.Optionv(ice.MSG_STDOUT) != nil { if f.in, f.out = s, os.Stdout; m.Optionv(ice.MSG_STDOUT) != nil {
f.out = m.Optionv(ice.MSG_STDOUT).(io.Writer) f.out = m.Optionv(ice.MSG_STDOUT).(io.Writer)
@ -186,16 +188,39 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool {
line = "" line = ""
continue continue
} }
if strings.HasPrefix(strings.TrimSpace(line), "!") {
m.Option("cache.offend", 0)
m.Option("cache.limit", 30)
if len(line) == 1 {
// 历史记录
m.Grows("history", nil, "", "", func(index int, value map[string]interface{}) {
f.printf(m, "%d\t%s\n", index+1, value["line"])
})
line = ""
continue
}
if i, e := strconv.Atoi(line[1:]); e == nil {
// 历史命令
m.Grows("history", nil, "id", kit.Format(i), func(index int, value map[string]interface{}) {
line = kit.Format(value["line"])
})
} else {
m.Grow("history", nil, kit.Dict("line", line)) }
m.Option(ice.MSG_PROMPT, m.Confv("prompt", "meta.PS1")) } else {
// 记录历史
m.Grow("history", nil, kit.Dict("line", line))
f.count++
}
if f.out == os.Stdout { // 清空格式
if m.Option(ice.MSG_PROMPT, m.Confv("prompt", "meta.PS1")); f.out == os.Stdout {
f.printf(m, "\033[0m") f.printf(m, "\033[0m")
} }
// 解析命令
f.parse(m, line) f.parse(m, line)
m.Cost("stdin: %v", line) m.Cost("stdin: %v", line)
f.count++
line = "" line = ""
} }
return true return true

View File

@ -26,7 +26,7 @@ var Index = &ice.Context{Name: "docker", Help: "虚拟机",
"auto": {Name: "auto", Help: "自动化", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "auto": {Name: "auto", Help: "自动化", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
prefix := []string{ice.CLI_SYSTEM, "docker"} prefix := []string{ice.CLI_SYSTEM, "docker"}
// 创建容器 // 创建容器
pid := m.Cmdx(prefix, "run", "-dt", "--name", arg[0], "--mount", kit.Format("type=bind,source=%s,target=/root", pid := m.Cmdx(prefix, "run", "-dt", "-w", "/root", "--name", arg[0], "--mount", kit.Format("type=bind,source=%s,target=/root",
kit.Path(m.Conf(ice.WEB_DREAM, "meta.path"), arg[0])), "alpine") kit.Path(m.Conf(ice.WEB_DREAM, "meta.path"), arg[0])), "alpine")
m.Log(ice.LOG_CREATE, "%s: %s", arg[0], pid) m.Log(ice.LOG_CREATE, "%s: %s", arg[0], pid)

View File

@ -167,6 +167,7 @@ ShyInit() {
elif bindkey &>/dev/null; then elif bindkey &>/dev/null; then
# zsh # zsh
bindkey -s '\C-G\C-R' 'ShySyncs base\n' bindkey -s '\C-G\C-R' 'ShySyncs base\n'
bindkey -s '\C-G\C-G' 'ShySyncs history\n'
setopt nosharehistory setopt nosharehistory
fi fi