1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 01:24:05 +08:00

opt xterm

This commit is contained in:
IT 老营长 @云轩领航-创始人 2023-03-10 07:24:31 +08:00
parent 45c782207f
commit 4d8bf2ccd1
5 changed files with 71 additions and 42 deletions

View File

@ -45,11 +45,22 @@ func ProcessFloat(m *ice.Message, arg ...string) {
m.Option(ice.PROCESS_ARG, arg)
m.Cmdy(COMMAND, arg[0])
}
func ProcessField(m *ice.Message, cmd string, args []string, arg ...string) {
func ProcessField(m *ice.Message, cmd string, args ice.Any, arg ...string) {
if cmd = kit.Select(m.ActionKey(), cmd); len(arg) == 0 || arg[0] != ice.RUN {
m.Option("_index", m.PrefixKey())
if m.Cmdy(COMMAND, cmd).ProcessField(ACTION, m.ActionKey(), ice.RUN); len(args) > 0 {
m.Push(ARGS, kit.Format(args))
m.Cmdy(COMMAND, cmd).ProcessField(ACTION, m.ActionKey(), ice.RUN)
switch cb := args.(type) {
case func() string:
m.Push(ARGS, kit.Format([]string{cb()}))
case func() []string:
m.Push(ARGS, kit.Format(cb()))
case []string:
m.Push(ARGS, kit.Format(cb))
case string:
m.Push(ARGS, kit.Format([]string{cb}))
case nil:
default:
m.ErrorNotImplement(args)
}
} else {
if aaa.Right(m, cmd, arg[1:]) {

View File

@ -35,6 +35,10 @@ func _hash_insert(m *ice.Message, prefix, chain string, arg ...string) string {
if expire := m.Conf(prefix, kit.Keys(chain, kit.Keym(EXPIRE))); expire != "" {
arg = kit.Simple(TIME, m.Time(expire), arg)
}
if arg[0] == HASH {
m.Conf(prefix, kit.Keys(chain, HASH, arg[1]), kit.Data(arg[2:]))
return m.Echo(arg[1]).Result()
}
return m.Echo(Rich(m, prefix, chain, kit.Data(arg, TARGET, m.Optionv(TARGET)))).Result()
}
func _hash_delete(m *ice.Message, prefix, chain, field, value string) {
@ -262,6 +266,7 @@ func HashSelectField(m *ice.Message, key string, field string) (value string) {
func HashSelectTarget(m *ice.Message, key string, create Any) (target Any) {
HashSelectUpdate(m, key, func(value ice.Map) {
target = value[TARGET]
m.Debug("what %v %v", target, create)
if _target, ok := target.([]string); ok && len(_target) == 0 {
target = nil
}

View File

@ -23,6 +23,10 @@ const (
)
const (
WEBSITE = "website"
RESIZE = "resize"
OUTPUT = "output"
INPUT = "input"
VIEW = "view"
CODE_VIMER = "web.code.vimer"
CODE_INNER = "web.code.inner"

View File

@ -21,6 +21,7 @@ func _sh_cmds(m *ice.Message, p string) (string, string) {
}
const (
BASH = "bash"
VIM = "vim"
CONF = "conf"
)

View File

@ -11,6 +11,7 @@ import (
ice "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/cli"
"shylinux.com/x/icebergs/base/ctx"
"shylinux.com/x/icebergs/base/log"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs"
"shylinux.com/x/icebergs/base/web"
@ -25,18 +26,23 @@ type _xterm struct {
func (s _xterm) Setsize(rows, cols string) error {
return pty.Setsize(s.File, &pty.Winsize{Rows: uint16(kit.Int(rows)), Cols: uint16(kit.Int(cols))})
}
func (s _xterm) Writeln(data string, arg ...ice.Any) {
s.Write(kit.Format(data, arg...) + ice.NL)
}
func (s _xterm) Write(data string) (int, error) {
return s.File.Write([]byte(data))
}
func (s _xterm) Close() error {
return s.Cmd.Process.Kill()
}
func _xterm_get(m *ice.Message, h string) _xterm {
h = kit.Select(m.Option(mdb.HASH), h)
m.Assert(h != "")
if h = kit.Select(m.Option(mdb.HASH), h); m.Assert(h != "") {
if mdb.HashSelectField(m, m.Option(mdb.HASH, h), mdb.TYPE) == "" {
mdb.HashCreate(m, mdb.HASH, h, m.OptionSimple("type,name,text"))
}
mdb.HashModify(m, web.VIEW, m.Option(ice.MSG_DAEMON))
}
t := mdb.HashSelectField(m, m.Option(mdb.HASH, h), mdb.TYPE)
mdb.HashModify(m, "view", m.Option(ice.MSG_DAEMON))
return mdb.HashSelectTarget(m, h, func() ice.Any {
ls := kit.Split(kit.Select(nfs.SH, strings.Split(t, " # ")[0]))
cmd := exec.Command(cli.SystemFind(m, ls[0]), ls[1:]...)
@ -45,81 +51,83 @@ func _xterm_get(m *ice.Message, h string) _xterm {
tty, err := pty.Start(cmd)
m.Assert(err)
m.Go(func() {
// defer mdb.HashSelectUpdate(m, h, func(value ice.Map) { delete(value, mdb.TARGET) })
defer mdb.HashRemove(m, mdb.HASH, h)
defer tty.Close()
defer mdb.HashRemove(m, mdb.HASH, h)
m.Option(ice.LOG_DISABLE, ice.TRUE)
buf := make([]byte, ice.MOD_BUFS)
for {
if n, e := tty.Read(buf); !m.Warn(e) && e == nil {
m.Option(ice.MSG_DAEMON, mdb.HashSelectField(m, h, "view"))
web.PushNoticeGrow(m, string(buf[:n]))
_xterm_echo(m, h, string(buf[:n]))
} else {
_xterm_echo(m, h, "~~~end~~~")
break
}
}
})
m.Go(func() { _xterm_cmds(m.Sleep("1s"), h, mdb.HashSelectField(m, h, mdb.TEXT)) })
return _xterm{cmd, tty}
}).(_xterm)
}
func _xterm_echo(m *ice.Message, h string, str string) {
m.Options(ice.MSG_DAEMON, mdb.HashSelectField(m, h, web.VIEW))
mdb.HashModify(m, mdb.HASH, h, mdb.TIME, m.Time())
web.PushNoticeGrow(m, h, str)
}
func _xterm_cmds(m *ice.Message, h string, cmd string, arg ...ice.Any) {
if cmd != "" {
_xterm_get(m, h).Writeln(cmd, arg...)
}
ctx.ProcessHold(m)
}
const XTERM = "xterm"
func init() {
Index.MergeCommands(ice.Commands{
XTERM: {Name: "xterm hash auto", Help: "命令行", Actions: ice.MergeActions(ice.Actions{
ctx.PROCESS: {Hand: func(m *ice.Message, arg ...string) {
if len(arg) == 0 || arg[0] != ice.RUN {
arg = []string{m.Cmdx("", mdb.CREATE, arg)}
}
ctx.ProcessField(m, m.PrefixKey(), arg, arg...)
}},
mdb.INPUTS: {Hand: func(m *ice.Message, arg ...string) {
switch mdb.HashInputs(m, arg).Cmdy(FAVOR, "_system_term", ice.OptionFields(arg[0])).Cut(arg[0]); arg[0] {
switch mdb.HashInputs(m, arg); arg[0] {
case mdb.TYPE:
if m.Option(nfs.LINE) != "" && m.Option(nfs.FILE) != "" {
m.Push(arg[0], "vim +"+m.Option(nfs.LINE)+ice.SP+m.Option(nfs.PATH)+m.Option(nfs.FILE))
m.Push(arg[0], VIM+" +"+m.Option(nfs.LINE)+ice.SP+m.Option(nfs.PATH)+m.Option(nfs.FILE))
}
m.Push(arg[0], "bash", "sh")
m.Push(arg[0], BASH, SH)
case mdb.NAME:
m.Push(arg[0], ice.Info.Hostname, path.Base(m.Option(mdb.TYPE)))
m.Push(arg[0], path.Base(m.Option(mdb.TYPE)), ice.Info.Hostname)
}
}},
mdb.CREATE: {Name: "create type=sh name text theme:textarea", Hand: func(m *ice.Message, arg ...string) {
m.Debug("what %v", m.FormatChain())
mdb.HashCreate(m, mdb.NAME, m.OptionDefault(mdb.NAME, kit.Split(m.Option(mdb.TYPE))[0]), m.OptionSimple(mdb.TYPE, mdb.NAME, mdb.TEXT, web.THEME))
}},
"resize": {Hand: func(m *ice.Message, arg ...string) {
mdb.CREATE: {Name: "create type*=sh name text theme:textarea", Hand: func(m *ice.Message, arg ...string) { mdb.HashCreate(m) }},
web.RESIZE: {Hand: func(m *ice.Message, arg ...string) {
_xterm_get(m, "").Setsize(m.OptionDefault("rows", "24"), m.OptionDefault("cols", "80"))
}},
"input": {Hand: func(m *ice.Message, arg ...string) {
web.INPUT: {Hand: func(m *ice.Message, arg ...string) {
if b, e := base64.StdEncoding.DecodeString(strings.Join(arg, "")); !m.Warn(e) {
_xterm_get(m, "").Write(string(b))
mdb.HashModify(m, mdb.TIME, m.Time())
}
}},
"debug": {Help: "日志", Hand: func(m *ice.Message, arg ...string) {
_xterm_get(m, kit.Select("", arg, 0)).Write("cd ~/contexts; tail -f var/log/bench.log" + ice.NL)
ctx.ProcessHold(m)
INSTALL: {Hand: func(m *ice.Message, arg ...string) {
_xterm_cmds(m, kit.Select("", arg, 0), m.Cmdx(PUBLISH, ice.CONTEXTS, INSTALL))
}},
"proxy": {Help: "代理", Hand: func(m *ice.Message, arg ...string) {
_xterm_get(m, kit.Select("", arg, 0)).Write(kit.Format(`git config --global url."%s".insteadOf "https://shylinux.com"`, m.Option(ice.MSG_USERHOST)) + ice.NL)
ctx.ProcessHold(m)
log.DEBUG: {Hand: func(m *ice.Message, arg ...string) {
_xterm_cmds(m, kit.Select("", arg, 0), "cd ~/contexts; tail -f var/log/bench.log")
}},
INSTALL: {Help: "安装", Hand: func(m *ice.Message, arg ...string) {
_xterm_get(m, kit.Select("", arg, 0)).Write(m.Cmdx(PUBLISH, ice.CONTEXTS, INSTALL) + ice.NL)
ctx.ProcessHold(m)
web.OUTPUT: {Hand: func(m *ice.Message, arg ...string) {
web.ProcessWebsite(m, "", "", m.OptionSimple(mdb.HASH), ctx.STYLE, web.OUTPUT)
}},
web.WEBSITE: {Help: "网页", Hand: func(m *ice.Message, arg ...string) { web.ProcessWebsite(m, "", "", m.OptionSimple(mdb.HASH)) }},
}, mdb.HashAction(mdb.FIELD, "time,hash,type,name,text,view,theme", mdb.TOOLS, FAVOR), ctx.CmdAction(), ctx.ProcessAction()), Hand: func(m *ice.Message, arg ...string) {
ctx.PROCESS: {Hand: func(m *ice.Message, arg ...string) {
ctx.ProcessField(m, m.PrefixKey(), func() string { return m.Cmdx("", mdb.CREATE, arg) }, arg...)
}},
}, mdb.HashAction(mdb.FIELD, "time,hash,type,name,text,view,theme", mdb.TOOLS, FAVOR), ctx.ProcessAction()), Hand: func(m *ice.Message, arg ...string) {
if mdb.HashSelect(m, arg...); len(arg) == 0 {
m.PushAction(web.WEBSITE, mdb.REMOVE).Action(mdb.CREATE, mdb.PRUNES)
m.PushAction(web.OUTPUT, mdb.REMOVE).Action(mdb.CREATE, mdb.PRUNES)
} else {
if m.Length() == 0 {
arg[0] = m.Cmdx("", mdb.CREATE, mdb.TYPE, arg[0])
mdb.HashSelect(m, arg[0])
m.Push(mdb.HASH, arg[0])
mdb.HashSelect(m, arg[0]).Push(mdb.HASH, arg[0])
}
m.Action(INSTALL, "debug", "proxy")
m.Push(mdb.HASH, arg[0])
// m.Action(INSTALL, log.DEBUG)
ctx.DisplayLocal(m, "")
}
}},