1
0
forked from x/ContextOS

tce add termbox

This commit is contained in:
shaoying 2018-04-23 14:28:12 +08:00
parent 1a66c4c170
commit 86ba6d97af
4 changed files with 171 additions and 46 deletions

View File

@ -1,3 +1,4 @@
config debug on
~aaa login root root
~web serve ./ ":9090"

View File

@ -206,7 +206,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
}
// }}}
}},
"list": &ctx.Command{Name: "list add table field", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
"list": &ctx.Command{Name: "list add table field [where condition]", Help: "执行查询语句",
Formats: map[string]int{"where": 1},
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
mdb, ok := m.Target().Server.(*MDB) // {{{
m.Assert(ok)
if len(arg) == 0 {
@ -221,33 +223,39 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
if mdb.list == nil {
mdb.list = make(map[string][]string)
}
mdb.list[m.Cap("count")] = arg[1:]
mdb.list[m.Cap("count")] = append(mdb.list[m.Cap("count")], m.Option("where"))
mdb.list[m.Cap("count")] = append(mdb.list[m.Cap("count")], arg[1:]...)
mdb.list_key = append(mdb.list_key, m.Cap("count"))
m.Capi("count", 1)
case "set":
mdb.list[arg[1]] = arg[2:]
mdb.list[arg[1]] = []string{m.Option("where")}
mdb.list[arg[1]] = append(mdb.list[arg[1]], arg[2:]...)
default:
if table, ok := mdb.list[arg[0]]; ok {
msg := m.Spawn(m.Target())
fields := strings.Join(table[1:], ",")
fields := strings.Join(table[2:], ",")
condition := ""
if len(arg) > 1 && len(arg[1]) > 0 {
condition = fmt.Sprintf("where %s", arg[1])
} else if len(table[0]) > 0 {
condition = fmt.Sprintf("where %s", table[0])
}
other := ""
if len(arg) > 2 {
other = strings.Join(arg[2:], " ")
}
msg.Cmd("query", fmt.Sprintf("select %s from %s %s %s", fields, table[0], condition, other))
msg.Cmd("query", fmt.Sprintf("select %s from %s %s %s", fields, table[1], condition, other))
m.Echo("%s %s\n", table[1], condition)
for _, k := range msg.Meta["append"] {
m.Echo("\t%s", k)
m.Echo("%s\t", k)
}
m.Echo("\n")
for i := 0; i < len(msg.Meta[msg.Meta["append"][0]]); i++ {
for _, k := range msg.Meta["append"] {
m.Echo("\t%s", msg.Meta[k][i])
m.Echo("%s\t", msg.Meta[k][i])
}
m.Echo("\n")
}

View File

@ -6,6 +6,7 @@ import ( // {{{
"bufio"
"encoding/json"
"fmt"
"github.com/nsf/termbox-go"
"github.com/skip2/go-qrcode"
"io"
"net/url"
@ -26,16 +27,23 @@ type NFS struct {
in *os.File
out *os.File
buf []string
cli *ctx.Message
x, y int
*ctx.Message
*ctx.Context
}
func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{
switch {
case nfs.io != nil:
fmt.Fprintf(nfs.io, str, arg...)
str := fmt.Sprintf(str, arg...)
nfs.y += strings.Count(str, "\n")
fmt.Fprintf(nfs.in, "%s", str)
case nfs.out != nil:
fmt.Fprintf(nfs.out, str, arg...)
str := fmt.Sprintf(str, arg...)
nfs.y += strings.Count(str, "\n")
fmt.Fprintf(nfs.out, "%s", str)
default:
return false
}
@ -43,6 +51,94 @@ func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{
}
// }}}
func (nfs *NFS) clear(line string) {
termbox.SetCursor(nfs.x, nfs.y)
termbox.Flush()
nfs.print(" ")
termbox.SetCursor(nfs.x, nfs.y)
termbox.Flush()
nfs.print("%s%s", nfs.cli.Conf("PS1"), line)
}
func (nfs *NFS) Read(p []byte) (n int, err error) {
if nfs.Cap("stream") != "stdio" {
return nfs.in.Read(p)
}
his := len(nfs.buf)
buf := make([]rune, 0, 1024)
back := buf
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyCtrlC:
termbox.Close()
os.Exit(1)
case termbox.KeyCtrlJ, termbox.KeyEnter:
buf = append(buf, '\n')
nfs.print("\n")
nfs.y++
b := []byte(string(buf[:len(buf)]))
n = len(b)
nfs.Log("fuck", nil, "%d %v", n, b)
copy(p, b)
return
case termbox.KeyCtrlP:
his = (his + len(nfs.buf) - 1) % len(nfs.buf)
b := nfs.buf[his]
buf = buf[:len(b)]
n := copy(buf, []rune(b))
buf = buf[:n]
nfs.clear(string(buf))
case termbox.KeyCtrlN:
his = (his + len(nfs.buf) - 1) % len(nfs.buf)
b := nfs.buf[his]
buf = buf[:len(b)]
n := copy(buf, []rune(b))
buf = buf[:n]
nfs.clear(string(buf))
case termbox.KeyCtrlH:
if len(buf) > 0 {
buf = buf[:len(buf)-1]
nfs.clear(string(buf))
n--
}
case termbox.KeyCtrlL:
termbox.Sync()
nfs.y = 0
nfs.clear(string(buf))
case termbox.KeyCtrlU:
nfs.clear("")
if len(buf) > 0 {
back = buf
}
buf = make([]rune, 0, 1024)
n = 0
case termbox.KeyCtrlY:
buf = back
n = len(buf)
nfs.clear(string(buf))
case termbox.KeySpace:
nfs.print(" ")
buf = append(buf, ' ')
n++
default:
print(string(ev.Ch))
buf = append(buf, ev.Ch)
n++
}
}
}
return
}
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
c.Caches = map[string]*ctx.Cache{
@ -84,6 +180,7 @@ func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
// }}}
func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
nfs.Message = m
if socket, ok := m.Data["io"]; ok {
nfs.io = socket.(io.ReadWriteCloser)
nfs.Reader = bufio.NewReader(nfs.io)
@ -204,8 +301,12 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
}
cli := m.Reply()
nfs.cli = cli
yac := m.Find(cli.Conf("yac"))
bio := bufio.NewScanner(nfs.in)
bio := bufio.NewScanner(nfs)
if m.Cap("stream") == "stdio" {
termbox.Init()
}
nfs.Context.Master(nil)
pos := 0
@ -266,6 +367,9 @@ out:
} else {
m.Cap("status", "stop")
}
if m.Cap("stream") == "stdio" {
termbox.Close()
}
return false
}

View File

@ -414,6 +414,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
method = m.Option("method")
}
m.Echo("%s", uri)
var body io.Reader
index := strings.Index(uri, "?")
switch method {
@ -523,6 +524,17 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"/demo": &ctx.Command{Name: "/demo", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Add("append", "hi", "hello")
}},
"temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Spawn(m.Target())
question := []string{}
for i := 1; i < 21; i++ {
question = append(question, fmt.Sprintf("{\"type\":\"1001\",\"title\":{\"text\":\"第%d题\"}}", i))
}
qs := "[" + strings.Join(question, ",") + "]"
msg.Cmd("get", "method", "POST", "evaluating_add/", "questions", qs)
m.Add("append", "hi", "hello")
}},
},
}