forked from x/ContextOS
tce add termbox
This commit is contained in:
parent
1a66c4c170
commit
86ba6d97af
@ -1,3 +1,4 @@
|
|||||||
|
config debug on
|
||||||
~aaa login root root
|
~aaa login root root
|
||||||
|
|
||||||
~web serve ./ ":9090"
|
~web serve ./ ":9090"
|
||||||
|
@ -206,55 +206,63 @@ 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: "执行查询语句",
|
||||||
mdb, ok := m.Target().Server.(*MDB) // {{{
|
Formats: map[string]int{"where": 1},
|
||||||
m.Assert(ok)
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
if len(arg) == 0 {
|
mdb, ok := m.Target().Server.(*MDB) // {{{
|
||||||
for _, k := range mdb.list_key {
|
m.Assert(ok)
|
||||||
m.Echo("%s: %v\n", k, mdb.list[k])
|
if len(arg) == 0 {
|
||||||
|
for _, k := range mdb.list_key {
|
||||||
|
m.Echo("%s: %v\n", k, mdb.list[k])
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch arg[0] {
|
switch arg[0] {
|
||||||
case "add":
|
case "add":
|
||||||
if mdb.list == nil {
|
if mdb.list == nil {
|
||||||
mdb.list = make(map[string][]string)
|
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_key = append(mdb.list_key, m.Cap("count"))
|
mdb.list[m.Cap("count")] = append(mdb.list[m.Cap("count")], arg[1:]...)
|
||||||
m.Capi("count", 1)
|
mdb.list_key = append(mdb.list_key, m.Cap("count"))
|
||||||
case "set":
|
m.Capi("count", 1)
|
||||||
mdb.list[arg[1]] = arg[2:]
|
case "set":
|
||||||
default:
|
mdb.list[arg[1]] = []string{m.Option("where")}
|
||||||
if table, ok := mdb.list[arg[0]]; ok {
|
mdb.list[arg[1]] = append(mdb.list[arg[1]], arg[2:]...)
|
||||||
msg := m.Spawn(m.Target())
|
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 := ""
|
condition := ""
|
||||||
if len(arg) > 1 && len(arg[1]) > 0 {
|
if len(arg) > 1 && len(arg[1]) > 0 {
|
||||||
condition = fmt.Sprintf("where %s", arg[1])
|
condition = fmt.Sprintf("where %s", arg[1])
|
||||||
}
|
} else if len(table[0]) > 0 {
|
||||||
other := ""
|
condition = fmt.Sprintf("where %s", table[0])
|
||||||
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))
|
other := ""
|
||||||
for _, k := range msg.Meta["append"] {
|
if len(arg) > 2 {
|
||||||
m.Echo("\t%s", k)
|
other = strings.Join(arg[2:], " ")
|
||||||
}
|
}
|
||||||
m.Echo("\n")
|
|
||||||
for i := 0; i < len(msg.Meta[msg.Meta["append"][0]]); i++ {
|
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"] {
|
for _, k := range msg.Meta["append"] {
|
||||||
m.Echo("\t%s", msg.Meta[k][i])
|
m.Echo("%s\t", k)
|
||||||
}
|
}
|
||||||
m.Echo("\n")
|
m.Echo("\n")
|
||||||
|
for i := 0; i < len(msg.Meta[msg.Meta["append"][0]]); i++ {
|
||||||
|
for _, k := range msg.Meta["append"] {
|
||||||
|
m.Echo("%s\t", msg.Meta[k][i])
|
||||||
|
}
|
||||||
|
m.Echo("\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// }}}
|
||||||
// }}}
|
}},
|
||||||
}},
|
|
||||||
},
|
},
|
||||||
Index: map[string]*ctx.Context{
|
Index: map[string]*ctx.Context{
|
||||||
"void": &ctx.Context{Name: "void",
|
"void": &ctx.Context{Name: "void",
|
||||||
|
@ -6,6 +6,7 @@ import ( // {{{
|
|||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/nsf/termbox-go"
|
||||||
"github.com/skip2/go-qrcode"
|
"github.com/skip2/go-qrcode"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -23,19 +24,26 @@ type NFS struct {
|
|||||||
send map[int]*ctx.Message
|
send map[int]*ctx.Message
|
||||||
target *ctx.Context
|
target *ctx.Context
|
||||||
|
|
||||||
in *os.File
|
in *os.File
|
||||||
out *os.File
|
out *os.File
|
||||||
buf []string
|
buf []string
|
||||||
|
cli *ctx.Message
|
||||||
|
x, y int
|
||||||
|
|
||||||
|
*ctx.Message
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{
|
func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{
|
||||||
switch {
|
switch {
|
||||||
case nfs.io != nil:
|
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:
|
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:
|
default:
|
||||||
return false
|
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 { // {{{
|
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||||
c.Caches = map[string]*ctx.Cache{
|
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 { // {{{
|
func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
|
nfs.Message = m
|
||||||
if socket, ok := m.Data["io"]; ok {
|
if socket, ok := m.Data["io"]; ok {
|
||||||
nfs.io = socket.(io.ReadWriteCloser)
|
nfs.io = socket.(io.ReadWriteCloser)
|
||||||
nfs.Reader = bufio.NewReader(nfs.io)
|
nfs.Reader = bufio.NewReader(nfs.io)
|
||||||
@ -204,8 +301,12 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
cli := m.Reply()
|
cli := m.Reply()
|
||||||
|
nfs.cli = cli
|
||||||
yac := m.Find(cli.Conf("yac"))
|
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)
|
nfs.Context.Master(nil)
|
||||||
pos := 0
|
pos := 0
|
||||||
|
|
||||||
@ -266,6 +367,9 @@ out:
|
|||||||
} else {
|
} else {
|
||||||
m.Cap("status", "stop")
|
m.Cap("status", "stop")
|
||||||
}
|
}
|
||||||
|
if m.Cap("stream") == "stdio" {
|
||||||
|
termbox.Close()
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,6 +414,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
method = m.Option("method")
|
method = m.Option("method")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.Echo("%s", uri)
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
index := strings.Index(uri, "?")
|
index := strings.Index(uri, "?")
|
||||||
switch method {
|
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) {
|
"/demo": &ctx.Command{Name: "/demo", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Add("append", "hi", "hello")
|
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")
|
||||||
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user