1
0
forked from x/icebergs

opt pushrender

This commit is contained in:
shylinux 2021-01-10 00:27:30 +08:00
parent 8e8161594f
commit 58ef8ff511
11 changed files with 69 additions and 100 deletions

View File

@ -50,7 +50,7 @@ func init() {
ls[0] = strings.TrimPrefix(ls[0], "icebergs/") ls[0] = strings.TrimPrefix(ls[0], "icebergs/")
} }
m.PushPlugin(INNER, INNER, kit.SSH_RUN) m.ShowPlugin("", INNER, kit.SSH_RUN)
m.Push("args", kit.Format([]string{"usr/icebergs/", ls[0], ls[1]})) m.Push("args", kit.Format([]string{"usr/icebergs/", ls[0], ls[1]}))
} }
}}, }},

View File

@ -124,7 +124,7 @@ func init() {
} }
} else if len(arg) > 2 { // 加载插件 } else if len(arg) > 2 { // 加载插件
m.PushPlugins(arg[0], arg[1], arg[2], ctx.COMMAND) m.ShowPlugin(arg[0], arg[1], arg[2], ctx.COMMAND)
} else if len(arg) > 1 { // 命令列表 } else if len(arg) > 1 { // 命令列表
m.Cmd(SPACE, arg[0], ctx.CONTEXT, arg[1], ctx.COMMAND).Table(func(index int, value map[string]string, head []string) { m.Cmd(SPACE, arg[0], ctx.CONTEXT, arg[1], ctx.COMMAND).Table(func(index int, value map[string]string, head []string) {

View File

@ -55,6 +55,7 @@ const ( // RENDER
RENDER_ANCHOR = "_anchor" RENDER_ANCHOR = "_anchor"
RENDER_BUTTON = "_button" RENDER_BUTTON = "_button"
RENDER_IMAGES = "_images" RENDER_IMAGES = "_images"
RENDER_VIDEOS = "_videos"
RENDER_QRCODE = "_qrcode" RENDER_QRCODE = "_qrcode"
RENDER_SCRIPT = "_script" RENDER_SCRIPT = "_script"
RENDER_DOWNLOAD = "_download" RENDER_DOWNLOAD = "_download"

View File

@ -53,7 +53,7 @@ func init() {
return return
} }
m.PushPlugin(INNER, INNER, mdb.RENDER) m.ShowPlugin("", INNER, mdb.RENDER)
m.Push(kit.SSH_ARG, kit.Format([]string{m.Option(kit.MDB_PATH), m.Option(kit.MDB_FILE), m.Option(kit.MDB_LINE)})) m.Push(kit.SSH_ARG, kit.Format([]string{m.Option(kit.MDB_PATH), m.Option(kit.MDB_FILE), m.Option(kit.MDB_LINE)}))
}}, }},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {

View File

@ -3,10 +3,12 @@ package ice
import ( import (
kit "github.com/shylinux/toolkits" kit "github.com/shylinux/toolkits"
"fmt"
"io" "io"
"os" "os"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@ -30,6 +32,8 @@ var Info = struct {
HostName string HostName string
UserName string UserName string
} }
nLocalCmd int32
}{} }{}
type Frame struct{ code int } type Frame struct{ code int }
@ -221,3 +225,8 @@ func Name(name string, value interface{}) string {
names[name] = value names[name] = value
return name return name
} }
func (m *Message) AddCmd(cmd *Command) string {
name := fmt.Sprintf("_cb_%d", atomic.AddInt32(&Info.nLocalCmd, 1))
m.target.Commands[name] = cmd
return kit.Keys(m.target.Cap(CTX_FOLLOW), name)
}

139
misc.go
View File

@ -10,7 +10,6 @@ import (
"net/url" "net/url"
"path" "path"
"strings" "strings"
"sync/atomic"
) )
func (m *Message) Prefix(arg ...string) string { func (m *Message) Prefix(arg ...string) string {
@ -42,7 +41,7 @@ func (m *Message) Watch(key string, arg ...string) *Message {
if len(arg) == 0 { if len(arg) == 0 {
arg = append(arg, m.Prefix("auto")) arg = append(arg, m.Prefix("auto"))
} }
m.Cmd("gdb.event", "action", "listen", "event", key, "cmd", strings.Join(arg, " ")) m.Cmd("gdb.event", "action", "listen", "event", key, kit.SSH_CMD, strings.Join(arg, " "))
return m return m
} }
func (m *Message) Event(key string, arg ...string) *Message { func (m *Message) Event(key string, arg ...string) *Message {
@ -61,29 +60,21 @@ func (m *Message) Space(arg interface{}) []string {
return []string{"web.space", kit.Format(arg)} return []string{"web.space", kit.Format(arg)}
} }
func (m *Message) PushPlugins(pod, ctx, cmd string, arg ...string) { func (m *Message) ShowPlugin(pod, ctx, cmd string, arg ...string) {
m.Cmdy("space", pod, "context", ctx, "command", cmd) m.Cmdy("web.space", pod, "context", ctx, "command", cmd)
m.Option(MSG_PROCESS, PROCESS_FIELD) m.Option(MSG_PROCESS, PROCESS_FIELD)
m.Option(FIELD_PREFIX, arg) m.Option(FIELD_PREFIX, arg)
} }
func (m *Message) PushPlugin(key string, arg ...string) {
m.Cmdy("command", key)
m.Option(MSG_PROCESS, PROCESS_FIELD)
m.Option(FIELD_PREFIX, arg)
}
func (m *Message) PushRender(key, view, name string, arg ...string) *Message {
return m.Push(key, _render(m, view, name, arg))
}
func (m *Message) PushPodCmd(cmd string, arg ...string) { func (m *Message) PushPodCmd(cmd string, arg ...string) {
m.Table(func(index int, value map[string]string, head []string) { m.Table(func(index int, value map[string]string, head []string) {
m.Push("pod", m.Option(MSG_USERPOD)) m.Push(kit.SSH_POD, m.Option(MSG_USERPOD))
}) })
m.Cmd("web.space").Table(func(index int, value map[string]string, head []string) { m.Cmd("web.space").Table(func(index int, value map[string]string, head []string) {
switch value["type"] { switch value[kit.MDB_TYPE] {
case "worker", "server": case "worker", "server":
m.Cmd("web.space", value["name"], m.Prefix(cmd), arg).Table(func(index int, val map[string]string, head []string) { m.Cmd("web.space", value[kit.MDB_NAME], m.Prefix(cmd), arg).Table(func(index int, val map[string]string, head []string) {
val["pod"] = kit.Keys(value["name"], val["pod"]) val[kit.SSH_POD] = kit.Keys(value[kit.MDB_NAME], val[kit.SSH_POD])
m.Push("", val, head) m.Push("", val, head)
}) })
} }
@ -109,12 +100,12 @@ func (m *Message) PushSearch(args ...interface{}) {
func (m *Message) PushSearchWeb(cmd string, name string) { func (m *Message) PushSearchWeb(cmd string, name string) {
msg := m.Spawn() msg := m.Spawn()
msg.Option("fields", "type,name,text") msg.Option("fields", "type,name,text")
msg.Cmd("mdb.select", m.Prefix(cmd), "", "hash").Table(func(index int, value map[string]string, head []string) { msg.Cmd("mdb.select", m.Prefix(cmd), "", kit.MDB_HASH).Table(func(index int, value map[string]string, head []string) {
text := kit.MergeURL(value["text"], value["name"], name) text := kit.MergeURL(value[kit.MDB_TEXT], value[kit.MDB_NAME], name)
if value["name"] == "" { if value[kit.MDB_NAME] == "" {
text = kit.MergeURL(value["text"] + url.QueryEscape(name)) text = kit.MergeURL(value[kit.MDB_TEXT] + url.QueryEscape(name))
} }
m.PushSearch("cmd", cmd, "type", kit.Select("", value["type"]), "name", name, "text", text) m.PushSearch(kit.SSH_CMD, cmd, kit.MDB_TYPE, kit.Select("", value[kit.MDB_TYPE]), kit.MDB_NAME, name, kit.MDB_TEXT, text)
}) })
} }
@ -124,11 +115,11 @@ func _render(m *Message, cmd string, args ...interface{}) string {
} }
switch arg := kit.Simple(args...); cmd { switch arg := kit.Simple(args...); cmd {
case RENDER_DOWNLOAD: // [name] link case RENDER_DOWNLOAD: // [name] file
if len(arg) == 1 { if len(arg) == 1 {
arg[0] = kit.MergeURL2(m.Option(MSG_USERWEB), path.Join("/share/local", arg[0]), "pod", m.Option(MSG_USERPOD)) arg[0] = kit.MergeURL2(m.Option(MSG_USERWEB), path.Join("/share/local", arg[0]), kit.SSH_POD, m.Option(MSG_USERPOD))
} else { } else {
arg[1] = kit.MergeURL2(m.Option(MSG_USERWEB), path.Join("/share/local", arg[1]), "pod", m.Option(MSG_USERPOD)) arg[1] = kit.MergeURL2(m.Option(MSG_USERWEB), path.Join("/share/local", arg[1]), kit.SSH_POD, m.Option(MSG_USERPOD))
} }
return fmt.Sprintf(`<a href="%s" download="%s">%s</a>`, kit.Select(arg[0], arg, 1), path.Base(arg[0]), arg[0]) return fmt.Sprintf(`<a href="%s" download="%s">%s</a>`, kit.Select(arg[0], arg, 1), path.Base(arg[0]), arg[0])
@ -143,10 +134,13 @@ func _render(m *Message, cmd string, args ...interface{}) string {
} }
return strings.Join(list, "") return strings.Join(list, "")
case RENDER_IMAGES: // src size case RENDER_IMAGES: // src [size]
return fmt.Sprintf(`<img src="%s" height=%s>`, arg[0], kit.Select("120", arg, 1)) return fmt.Sprintf(`<img src="%s" height=%s>`, arg[0], kit.Select("120", arg, 1))
case RENDER_QRCODE: // text size height case RENDER_VIDEOS: // src [size]
return fmt.Sprintf(`<video src="%s" height=%s controls>`, arg[0], kit.Select("120", arg, 1))
case RENDER_QRCODE: // text [size]
buf := bytes.NewBuffer(make([]byte, 0, MOD_BUFS)) buf := bytes.NewBuffer(make([]byte, 0, MOD_BUFS))
if qr, e := qrcode.New(arg[0], qrcode.Medium); m.Assert(e) { if qr, e := qrcode.New(arg[0], qrcode.Medium); m.Assert(e) {
m.Assert(qr.Write(kit.Int(kit.Select("240", arg, 1)), buf)) m.Assert(qr.Write(kit.Int(kit.Select("240", arg, 1)), buf))
@ -155,49 +149,49 @@ func _render(m *Message, cmd string, args ...interface{}) string {
return fmt.Sprintf(`<img src="%s" title='%s' height=%s>`, src, arg[0], kit.Select("240", arg, 1)) return fmt.Sprintf(`<img src="%s" title='%s' height=%s>`, src, arg[0], kit.Select("240", arg, 1))
case RENDER_SCRIPT: // type text case RENDER_SCRIPT: // type text
if arg[1] == "break" { if len(arg) == 1 && arg[0] != kit.SSH_BREAK {
return "<br>" arg = []string{kit.SSH_SHELL, arg[0]}
} }
list := []string{} list := []string{kit.Format(`<div class="story" data-type="spark" data-name="%s">`, arg[0])}
list = append(list, kit.Format(`<div class="story" data-type="spark" data-name="%s">`, arg[0])) for _, l := range strings.Split(strings.Join(arg[1:], "\n"), "\n") {
for _, l := range strings.Split(arg[1], "\n") { switch list = append(list, "<div>"); arg[0] {
list = append(list, "<div>") case kit.SSH_SHELL:
switch arg[0] { list = append(list, "<label>", "$ ", "</label>")
case "shell":
list = append(list, "<label>$ </label>")
default: default:
list = append(list, "<label>&lt; </label>") list = append(list, "<label>", "&lt; ", "</label>")
} }
list = append(list, "<span>") list = append(list, "<span>", l, "</span>")
list = append(list, l)
list = append(list, "</span>")
list = append(list, "</div>") list = append(list, "</div>")
} }
list = append(list, "</div>") list = append(list, "</div>")
return strings.Join(list, "") return strings.Join(list, "")
default:
return arg[0]
} }
return "" return ""
} }
func (m *Message) PushDownload(arg ...interface{}) { // [name] link func (m *Message) PushRender(key, view, name string, arg ...string) *Message {
m.Push("link", _render(m, RENDER_DOWNLOAD, arg...)) return m.Push(key, _render(m, view, name, arg))
}
func (m *Message) PushDownload(arg ...interface{}) { // [name] file
m.Push(kit.MDB_LINK, _render(m, RENDER_DOWNLOAD, arg...))
} }
func (m *Message) PushAnchor(arg ...interface{}) { // [name] link func (m *Message) PushAnchor(arg ...interface{}) { // [name] link
m.Push("link", _render(m, RENDER_ANCHOR, arg...)) m.Push(kit.MDB_LINK, _render(m, RENDER_ANCHOR, arg...))
} }
func (m *Message) PushButton(arg ...string) { func (m *Message) PushButton(arg ...string) {
m.Push("action", _render(m, RENDER_BUTTON, strings.Join(arg, ","))) m.Push(kit.MDB_ACTION, _render(m, RENDER_BUTTON, strings.Join(arg, ",")))
} }
func (m *Message) PushScript(text string, arg ...string) *Message { func (m *Message) PushScript(arg ...string) *Message { // [type] text...
mime := "shell" return m.Push(kit.MDB_SCRIPT, _render(m, RENDER_SCRIPT, arg))
if len(arg) > 0 {
mime, text = text, strings.Join(arg, "\n")
}
return m.Push("script", _render(m, RENDER_SCRIPT, mime, text))
} }
func (m *Message) PushImages(key, src string, arg ...string) { // src [size] func (m *Message) PushImages(key, src string, arg ...string) { // key src [size]
m.Push(key, _render(m, RENDER_IMAGES, src, arg)) m.Push(key, _render(m, RENDER_IMAGES, src, arg))
} }
func (m *Message) PushQRCode(key string, text string, arg ...string) { // text [size] func (m *Message) PushVideos(key, src string, arg ...string) { // key src [size]
m.Push(key, _render(m, RENDER_VIDEOS, src, arg))
}
func (m *Message) PushQRCode(key string, text string, arg ...string) { // key text [size]
m.Push(key, _render(m, RENDER_QRCODE, text, arg)) m.Push(key, _render(m, RENDER_QRCODE, text, arg))
} }
func (m *Message) PushAction(list ...interface{}) { func (m *Message) PushAction(list ...interface{}) {
@ -212,51 +206,16 @@ func (m *Message) EchoAnchor(arg ...interface{}) *Message { // [name] link
func (m *Message) EchoButton(arg ...string) *Message { func (m *Message) EchoButton(arg ...string) *Message {
return m.Echo(_render(m, RENDER_BUTTON, strings.Join(arg, ","))) return m.Echo(_render(m, RENDER_BUTTON, strings.Join(arg, ",")))
} }
func (m *Message) EchoScript(text string, arg ...string) *Message { func (m *Message) EchoScript(arg ...string) *Message {
mime := "shell" return m.Echo(_render(m, RENDER_SCRIPT, arg))
if len(arg) > 0 {
mime, text = text, strings.Join(arg, "\n")
}
return m.Echo(_render(m, RENDER_SCRIPT, mime, text))
} }
func (m *Message) EchoQRCode(text string, arg ...string) *Message { // text [size] func (m *Message) EchoQRCode(text string, arg ...string) *Message { // text [size]
return m.Echo(_render(m, RENDER_QRCODE, text, arg)) return m.Echo(_render(m, RENDER_QRCODE, text, arg))
} }
func (m *Message) SortStr(key string) { m.Sort(key, "str") }
func (m *Message) SortStrR(key string) { m.Sort(key, "str_r") }
func (m *Message) SortInt(key string) { m.Sort(key, "int") } func (m *Message) SortInt(key string) { m.Sort(key, "int") }
func (m *Message) SortIntR(key string) { m.Sort(key, "int_r") } func (m *Message) SortIntR(key string) { m.Sort(key, "int_r") }
func (m *Message) SortStr(key string) { m.Sort(key, "str") }
func (m *Message) SortStrR(key string) { m.Sort(key, "str_r") }
func (m *Message) SortTime(key string) { m.Sort(key, "time") } func (m *Message) SortTime(key string) { m.Sort(key, "time") }
func (m *Message) SortTimeR(key string) { m.Sort(key, "time_r") } func (m *Message) SortTimeR(key string) { m.Sort(key, "time_r") }
func (m *Message) PushRenderOld(key, view, name string, arg ...string) *Message {
switch view {
case "button":
list := []string{}
for _, k := range kit.Split(name) {
list = append(list, fmt.Sprintf(`<input type="button" name="%s" value="%s">`,
k, kit.Select(k, kit.Value(m.cmd.Meta, kit.Keys("trans", k)))))
}
m.Push(key, strings.Join(list, ""))
case "video":
m.Push(key, fmt.Sprintf(`<video src="%s" height=%s controls>`, name, kit.Select("120", arg, 0)))
case "img":
m.Push(key, fmt.Sprintf(`<img src="%s" height=%s>`, name, kit.Select("120", arg, 0)))
case "a": // name [link]
m.Push(key, fmt.Sprintf(`<a href="%s" target="_blank">%s</a>`, kit.Select(name, arg, 0), name))
case "download": // name [link]
m.Push(key, fmt.Sprintf(`<a href="%s" download="%s">%s</a>`, kit.Select(name, arg, 0), path.Base(name), name))
default:
m.Push(key, name)
}
return m
}
var count = int32(0)
func (m *Message) AddCmd(cmd *Command) string {
name := fmt.Sprintf("_cb_%d", atomic.AddInt32(&count, 1))
m.target.Commands[name] = cmd
return kit.Keys(m.target.Cap(CTX_FOLLOW), name)
}

View File

@ -51,7 +51,7 @@ func init() {
return return
} }
m.PushPlugin(cli.SYSTEM, cli.SYSTEM, cli.RUN) m.ShowPlugin("", cli.SYSTEM, cli.RUN)
m.Push(kit.SSH_ARG, kit.Split(m.Option(kit.MDB_TEXT))) m.Push(kit.SSH_ARG, kit.Split(m.Option(kit.MDB_TEXT)))
}}, }},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {

View File

@ -44,7 +44,7 @@ func init() {
return return
} }
m.PushPlugin(cli.SYSTEM, cli.SYSTEM, cli.RUN) m.ShowPlugin("", cli.SYSTEM, cli.RUN)
m.Push(cli.ARG, kit.Format(kit.Simple(kit.Split(m.Option(kit.MDB_TEXT))))) m.Push(cli.ARG, kit.Format(kit.Simple(kit.Split(m.Option(kit.MDB_TEXT)))))
}}, }},
FAVOR: {Name: "favor topic=some@key type name text", Help: "收藏", Hand: func(m *ice.Message, arg ...string) { FAVOR: {Name: "favor topic=some@key type name text", Help: "收藏", Hand: func(m *ice.Message, arg ...string) {

View File

@ -43,7 +43,7 @@ func init() {
m.Cmdy(nfs.DIR, kit.Select("", arg, 1)) m.Cmdy(nfs.DIR, kit.Select("", arg, 1))
return return
} }
m.PushPlugin(nfs.DIR, nfs.DIR, cli.RUN) m.ShowPlugin("", nfs.DIR, cli.RUN)
}}, }},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Option(mdb.FIELDS, kit.Select(m.Conf(m.Prefix(TRASH), kit.META_FIELD), mdb.DETAIL, len(arg) > 0)) m.Option(mdb.FIELDS, kit.Select(m.Conf(m.Prefix(TRASH), kit.META_FIELD), mdb.DETAIL, len(arg) > 0))

View File

@ -18,7 +18,7 @@ func init() {
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
SPIDE: {Name: "spide wid tid cmd auto", Help: "网页爬虫", Action: map[string]*ice.Action{ SPIDE: {Name: "spide wid tid cmd auto", Help: "网页爬虫", Action: map[string]*ice.Action{
"download": {Name: "download", Help: "下载", Hand: func(m *ice.Message, arg ...string) { web.DOWNLOAD: {Name: "download", Help: "下载", Hand: func(m *ice.Message, arg ...string) {
m.Cmd(CACHE, mdb.CREATE, arg) m.Cmd(CACHE, mdb.CREATE, arg)
}}, }},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
@ -29,8 +29,8 @@ func init() {
m.Push(kit.MDB_TIME, value[kit.MDB_TIME]) m.Push(kit.MDB_TIME, value[kit.MDB_TIME])
m.Push(kit.MDB_TYPE, value[kit.MDB_TYPE]) m.Push(kit.MDB_TYPE, value[kit.MDB_TYPE])
m.Push(kit.MDB_NAME, value[kit.MDB_NAME]) m.Push(kit.MDB_NAME, value[kit.MDB_NAME])
m.PushButton("download") m.PushButton(web.DOWNLOAD)
m.PushRenderOld(kit.MDB_TEXT, value[kit.MDB_TYPE], value[kit.MDB_LINK]) m.PushRender(kit.MDB_TEXT, value[kit.MDB_TYPE], value[kit.MDB_LINK])
m.Push(kit.MDB_LINK, value[kit.MDB_LINK]) m.Push(kit.MDB_LINK, value[kit.MDB_LINK])
}) })
break break

View File

@ -59,7 +59,7 @@ func init() {
return return
} }
m.PushPlugin(code.INNER, code.INNER, mdb.RENDER) m.ShowPlugin("", code.INNER, mdb.RENDER)
m.Push(kit.SSH_ARG, kit.Format([]string{kit.Select("./", path.Dir(m.Option(kit.MDB_FILE))), path.Base(m.Option(kit.MDB_FILE)), m.Option(kit.MDB_LINE)})) m.Push(kit.SSH_ARG, kit.Format([]string{kit.Select("./", path.Dir(m.Option(kit.MDB_FILE))), path.Base(m.Option(kit.MDB_FILE)), m.Option(kit.MDB_LINE)}))
}}, }},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {