1
0
mirror of https://shylinux.com/x/icebergs synced 2025-06-26 10:27:31 +08:00
This commit is contained in:
shaoying 2022-11-26 18:17:35 +08:00
parent bf1edda0e7
commit a14a231e8a

View File

@ -42,7 +42,6 @@ func (f *Frame) prompt(m *ice.Message, list ...string) *Frame {
if len(list) == 0 { if len(list) == 0 {
list = append(list, f.ps1...) list = append(list, f.ps1...)
} }
fmt.Fprintf(f.stdout, "\r") fmt.Fprintf(f.stdout, "\r")
for _, v := range list { for _, v := range list {
switch v { switch v {
@ -65,10 +64,9 @@ func (f *Frame) printf(m *ice.Message, str string, arg ...ice.Any) *Frame {
return f return f
} }
func (f *Frame) change(m *ice.Message, ls []string) []string { func (f *Frame) change(m *ice.Message, ls []string) []string {
if len(ls) == 1 && ls[0] == "~" { // 模块列表 if len(ls) == 1 && ls[0] == "~" {
ls = []string{ctx.CONTEXT} ls = []string{ctx.CONTEXT}
} else if len(ls) > 0 && strings.HasPrefix(ls[0], "~") {
} else if len(ls) > 0 && strings.HasPrefix(ls[0], "~") { // 切换模块
target := ls[0][1:] target := ls[0][1:]
if ls = ls[1:]; len(target) == 0 && len(ls) > 0 { if ls = ls[1:]; len(target) == 0 && len(ls) > 0 {
target, ls = ls[0], ls[1:] target, ls = ls[0], ls[1:]
@ -76,7 +74,7 @@ func (f *Frame) change(m *ice.Message, ls []string) []string {
if target == "~" { if target == "~" {
target = "" target = ""
} }
m.Spawn(f.target).Search(target+ice.PT, func(p *ice.Context, s *ice.Context, key string) { m.Spawn(f.target).Search(target+ice.PT, func(p *ice.Context, s *ice.Context) {
m.Logs(mdb.SELECT, ctx.CONTEXT, s.Name) m.Logs(mdb.SELECT, ctx.CONTEXT, s.Name)
f.target = s f.target = s
}) })
@ -86,8 +84,7 @@ func (f *Frame) change(m *ice.Message, ls []string) []string {
func (f *Frame) alias(m *ice.Message, ls []string) []string { func (f *Frame) alias(m *ice.Message, ls []string) []string {
if len(ls) == 0 { if len(ls) == 0 {
return ls return ls
} } else if alias := kit.Simple(kit.Value(m.Optionv(ice.MSG_ALIAS), ls[0])); len(alias) > 0 {
if alias := kit.Simple(kit.Value(m.Optionv(ice.MSG_ALIAS), ls[0])); len(alias) > 0 {
ls = append(alias, ls[1:]...) ls = append(alias, ls[1:]...)
} }
return ls return ls
@ -104,8 +101,6 @@ func (f *Frame) parse(m *ice.Message, h, line string) string {
if ls = f.change(msg, f.alias(msg, ls)); len(ls) == 0 { if ls = f.change(msg, f.alias(msg, ls)); len(ls) == 0 {
return "" return ""
} }
msg.Render("", kit.List())
if msg.Cmdy(ls); h == STDIO && msg.IsErrNotFound() { if msg.Cmdy(ls); h == STDIO && msg.IsErrNotFound() {
msg.SetResult().Cmdy(cli.SYSTEM, ls) msg.SetResult().Cmdy(cli.SYSTEM, ls)
} }
@ -115,40 +110,30 @@ func (f *Frame) parse(m *ice.Message, h, line string) string {
func (f *Frame) scan(m *ice.Message, h, line string) *Frame { func (f *Frame) scan(m *ice.Message, h, line string) *Frame {
f.ps1 = kit.Simple(m.Confv(PROMPT, kit.Keym(PS1))) f.ps1 = kit.Simple(m.Confv(PROMPT, kit.Keym(PS1)))
f.ps2 = kit.Simple(m.Confv(PROMPT, kit.Keym(PS2))) f.ps2 = kit.Simple(m.Confv(PROMPT, kit.Keym(PS2)))
ps := f.ps1
m.Optionv("message", m) m.Optionv("message", m)
if m.I, m.O = f.stdin, f.stdout; h == STDIO { m.I, m.O = f.stdin, f.stdout
gdb.Event(m, SOURCE_STDIO) ps, bio := f.ps1, bufio.NewScanner(f.stdin)
m.Sleep("3s")
}
bio := bufio.NewScanner(f.stdin)
for f.prompt(m, ps...); f.stdin != nil && bio.Scan(); f.prompt(m, ps...) { for f.prompt(m, ps...); f.stdin != nil && bio.Scan(); f.prompt(m, ps...) {
if len(bio.Text()) == 0 && h == STDIO { if len(bio.Text()) == 0 && h == STDIO {
continue // 空行 continue
} }
f.count++ f.count++
if strings.HasSuffix(bio.Text(), "\\") { if strings.HasSuffix(bio.Text(), "\\") {
line += bio.Text()[:len(bio.Text())-1] line += bio.Text()[:len(bio.Text())-1]
ps = f.ps2 ps = f.ps2
continue // 续行 continue
} }
if line += bio.Text(); strings.Count(line, "`")%2 == 1 { if line += bio.Text(); strings.Count(line, "`")%2 == 1 {
line += ice.NL line += ice.NL
ps = f.ps2 ps = f.ps2
continue // 多行 continue
} }
if strings.HasPrefix(strings.TrimSpace(line), "#") { if strings.HasPrefix(strings.TrimSpace(line), "#") {
line = "" line = ""
continue // 注释 continue
} }
if ps = f.ps1; f.stdout == os.Stdout { if ps = f.ps1; f.stdout == os.Stdout && ice.Info.Colors {
if ice.Info.Colors { f.printf(m, "\033[0m")
f.printf(m, "\033[0m") // 清空格式
}
} }
line = f.parse(m, h, line) line = f.parse(m, h, line)
} }
@ -164,22 +149,17 @@ func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server {
} }
return f return f
} }
func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server {
return &Frame{}
}
func (f *Frame) Start(m *ice.Message, arg ...string) bool { func (f *Frame) Start(m *ice.Message, arg ...string) bool {
m.Optionv(FRAME, f) m.Optionv(FRAME, f)
switch f.source = kit.Select(STDIO, arg, 0); f.source { switch f.source = kit.Select(STDIO, arg, 0); f.source {
case STDIO: // 终端交互 case STDIO:
if m.Cap(ice.CTX_STREAM, f.source); f.target == nil { if m.Cap(ice.CTX_STREAM, f.source); f.target == nil {
f.target = m.Target() f.target = m.Target()
} }
r, w, _ := os.Pipe() r, w, _ := os.Pipe()
go func() { io.Copy(w, os.Stdin) }() go func() { io.Copy(w, os.Stdin) }()
f.pipe, f.stdin, f.stdout = w, r, os.Stdout f.pipe, f.stdin, f.stdout = w, r, os.Stdout
m.Option(ice.MSG_OPTS, ice.MSG_USERNAME, ice.MSG_USERROLE)
m.Option(ice.MSG_OPTS, ice.MSG_USERNAME)
f.scan(m, STDIO, "") f.scan(m, STDIO, "")
default: // 脚本文件 default: // 脚本文件
@ -213,6 +193,9 @@ func (f *Frame) Close(m *ice.Message, arg ...string) bool {
f.stdin = nil f.stdin = nil
return true return true
} }
func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server {
return &Frame{}
}
const ( const (
FRAME = "frame" FRAME = "frame"
@ -233,24 +216,21 @@ const (
) )
func init() { func init() {
Index.Merge(&ice.Context{Configs: ice.Configs{ Index.MergeCommands(ice.Commands{
SOURCE: {Name: SOURCE, Help: "加载脚本", Value: kit.Data()}, SOURCE: {Name: "source file", Help: "脚本解析", Actions: mdb.HashAction(), Hand: func(m *ice.Message, arg ...string) {
PROMPT: {Name: PROMPT, Help: "命令提示", Value: kit.Data(
PS1, []ice.Any{"\033[33;44m", mdb.COUNT, "[", mdb.TIME, "]", "\033[5m", TARGET, "\033[0m", "\033[44m", ">", "\033[0m ", "\033[?25h", "\033[32m"},
PS2, []ice.Any{mdb.COUNT, " ", TARGET, "> "},
)},
}, Commands: ice.Commands{
SOURCE: {Name: "source file", Help: "脚本解析", Hand: func(m *ice.Message, arg ...string) {
if f, ok := m.Target().Server().(*Frame); ok { if f, ok := m.Target().Server().(*Frame); ok {
f.Spawn(m, m.Target()).Start(m, arg...) f.Spawn(m, m.Target()).Start(m, arg...)
} }
}}, }},
TARGET: {Name: "target name run", Help: "当前模块", Hand: func(m *ice.Message, arg ...string) { TARGET: {Name: "target name run", Help: "当前模块", Hand: func(m *ice.Message, arg ...string) {
f := m.Target().Server().(*Frame) f := m.Target().Server().(*Frame)
m.Search(arg[0]+ice.PT, func(p *ice.Context, s *ice.Context, key string) { f.target = s }) m.Search(arg[0]+ice.PT, func(p *ice.Context, s *ice.Context) { f.target = s })
f.prompt(m) f.prompt(m)
}}, }},
PROMPT: {Name: "prompt arg run", Help: "命令提示", Hand: func(m *ice.Message, arg ...string) { PROMPT: {Name: "prompt arg run", Help: "命令提示", Actions: mdb.AutoConfig(
PS1, []ice.Any{"\033[33;44m", mdb.COUNT, "[", mdb.TIME, "]", "\033[5m", TARGET, "\033[0m", "\033[44m", ">", "\033[0m ", "\033[?25h", "\033[32m"},
PS2, []ice.Any{mdb.COUNT, " ", TARGET, "> "},
), Hand: func(m *ice.Message, arg ...string) {
if f, ok := m.Optionv(FRAME).(*Frame); ok { if f, ok := m.Optionv(FRAME).(*Frame); ok {
f.prompt(m, arg...) f.prompt(m, arg...)
} }
@ -275,5 +255,5 @@ func init() {
f.Close(m, arg...) f.Close(m, arg...)
} }
}}, }},
}}) })
} }