1
0
forked from x/icebergs

add ssh.history search

This commit is contained in:
shaoying 2020-03-22 11:08:32 +08:00
parent 3f22640b66
commit ff04e585a6

View File

@ -20,7 +20,6 @@ type Frame struct {
out io.Writer out io.Writer
target *ice.Context target *ice.Context
count int
exit bool exit bool
} }
@ -63,7 +62,7 @@ func (f *Frame) prompt(m *ice.Message) *Frame {
for _, v := range kit.Simple(m.Optionv(ice.MSG_PROMPT)) { for _, v := range kit.Simple(m.Optionv(ice.MSG_PROMPT)) {
switch v { switch v {
case "count": case "count":
fmt.Fprintf(f.out, kit.Format("%d", f.count)) fmt.Fprintf(f.out, "%d", kit.Int(m.Conf("history", "meta.count"))+1)
case "time": case "time":
fmt.Fprintf(f.out, time.Now().Format("15:04:05")) fmt.Fprintf(f.out, time.Now().Format("15:04:05"))
case "target": case "target":
@ -176,7 +175,7 @@ func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server {
} }
func (f *Frame) Start(m *ice.Message, arg ...string) bool { func (f *Frame) Start(m *ice.Message, arg ...string) bool {
m.Option(ice.MSG_PROMPT, m.Confv("prompt", "meta.PS1")) m.Option(ice.MSG_PROMPT, m.Confv("prompt", "meta.PS1"))
f.count, f.target = kit.Int(m.Conf("history", "meta.count"))+1, m.Source() f.target = m.Source()
switch kit.Select("stdio", arg, 0) { switch kit.Select("stdio", arg, 0) {
case "stdio": case "stdio":
@ -223,14 +222,11 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool {
line = "" line = ""
continue continue
} }
if strings.HasPrefix(strings.TrimSpace(line), "!") { if strings.HasPrefix(strings.TrimSpace(line), "!") {
m.Option("cache.offend", 0)
m.Option("cache.limit", 30)
if len(line) == 1 { if len(line) == 1 {
// 历史记录 // 历史记录
m.Grows("history", nil, "", "", func(index int, value map[string]interface{}) { f.printf(m, m.Cmd("history").Table().Result())
f.printf(m, "%d\t%s\n", index+1, value["line"])
})
line = "" line = ""
continue continue
} }
@ -240,12 +236,13 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool {
line = kit.Format(value["line"]) line = kit.Format(value["line"])
}) })
} else { } else {
f.printf(m, m.Cmd("history", "search", line[1:]).Table().Result())
line = ""
continue
} }
} else { } else {
// 记录历史 // 记录历史
m.Grow("history", nil, kit.Dict("line", line)) m.Grow("history", nil, kit.Dict("line", line))
f.count++
} }
// 清空格式 // 清空格式
@ -268,31 +265,15 @@ func (f *Frame) Close(m *ice.Message, arg ...string) bool {
var Index = &ice.Context{Name: "ssh", Help: "终端模块", var Index = &ice.Context{Name: "ssh", Help: "终端模块",
Caches: map[string]*ice.Cache{}, Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{ Configs: map[string]*ice.Config{
"history": {Name: "history", Help: "history", Value: kit.Data()},
"prompt": {Name: "prompt", Help: "prompt", Value: kit.Data( "prompt": {Name: "prompt", Help: "prompt", Value: kit.Data(
"PS1", []interface{}{"\033[33;44m", "count", "[", "time", "]", "\033[5m", "target", "\033[0m", "\033[44m", ">", "\033[0m ", "\033[?25h", "\033[32m"}, "PS1", []interface{}{"\033[33;44m", "count", "[", "time", "]", "\033[5m", "target", "\033[0m", "\033[44m", ">", "\033[0m ", "\033[?25h", "\033[32m"},
"PS2", []interface{}{"count", " ", "target", "> "}, "PS2", []interface{}{"count", " ", "target", "> "},
)}, )},
"super": {Name: "super", Help: "super", Value: kit.Data()}, "super": {Name: "super", Help: "super", Value: kit.Data()},
"demo": {Name: "demo", Help: "demo", Value: kit.Data(
"short", "name",
"limit", "5", "least", "2",
)},
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
"demo": {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
data := m.Richs("demo", nil, arg[0], nil)
if data != nil {
m.Echo("%v", data)
return
}
m.Rich("demo", nil, kit.Dict(
"name", arg[0],
))
}},
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Load() m.Load()
}}, }},
@ -314,10 +295,18 @@ var Index = &ice.Context{Name: "ssh", Help: "终端模块",
return return
} }
f := m.Target().Server().(*Frame) switch arg[0] {
m.Grows("history", nil, "id", arg[0], func(index int, value map[string]interface{}) { case "search":
f.parse(m, kit.Format(value["line"])) m.Grows("history", nil, "", "", func(index int, value map[string]interface{}) {
}) if strings.Contains(kit.Format(value["line"]), arg[1]) {
m.Push("", value, []string{"id", "time", "line"})
}
})
default:
m.Grows("history", nil, "id", arg[0], func(index int, value map[string]interface{}) {
m.Push("", value, []string{"id", "time", "line"})
})
}
}}, }},
"return": {Name: "return", Help: "解析", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "return": {Name: "return", Help: "解析", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Option(ice.MSG_PROMPT, m.Confv("prompt", "meta.PS1")) m.Option(ice.MSG_PROMPT, m.Confv("prompt", "meta.PS1"))