1
0
forked from x/ContextOS

tce fix parse close

This commit is contained in:
shaoying 2018-07-17 21:30:09 +08:00
parent d14be8e6ef
commit 055a152a46
3 changed files with 66 additions and 54 deletions

View File

@ -268,16 +268,11 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Option("target", m.Target().Name) m.Option("target", m.Target().Name)
yac = m.Sesss("yac") yac = m.Sesss("yac")
yac.Call(func(cmd *ctx.Message) *ctx.Message { yac.Call(func(cmd *ctx.Message) *ctx.Message {
if cmd.Detail(0) == "scan_end" {
m.Target().Close(m.Spawn())
return nil
}
cmd.Cmd() cmd.Cmd()
m.Option("target", cli.target.Name) m.Option("target", cli.target.Name)
if cmd.Has("return") { if cmd.Has("return") {
m.Target().Close(m.Spawn())
m.Options("scan_end", true) m.Options("scan_end", true)
m.Target().Close(m.Spawn())
} }
return nil return nil
}, "parse", arg[1]) }, "parse", arg[1])
@ -348,6 +343,7 @@ func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
return false return false
} }
} }
return false
} }
return true return true
} }

View File

@ -125,36 +125,41 @@ func (nfs *NFS) insert(rest []rune, letters []rune) []rune { // {{{
// }}} // }}}
func (nfs *NFS) escape(key ...string) *NFS { // {{{ func (nfs *NFS) escape(form string, args ...interface{}) *NFS { // {{{
for _, k := range key { fmt.Fprintf(nfs.out, "\033[%s", fmt.Sprintf(form, args...))
fmt.Fprintf(nfs.out, "\033[%s", k)
}
return nfs return nfs
} }
// }}} // }}}
func (nfs *NFS) color(str string, attr ...int) *NFS { // {{{ func (nfs *NFS) color(str string, attr ...int) *NFS { // {{{
fg := nfs.Confi("color") if !nfs.Confs("color") {
fmt.Fprintf(nfs.out, "%s", str)
return nfs
}
fg := nfs.Confi("fgcolor")
if len(attr) > 0 { if len(attr) > 0 {
fg = attr[0] fg = attr[0]
} }
bg := nfs.Confi("backcolor") bg := nfs.Confi("bgcolor")
if len(attr) > 1 { if len(attr) > 1 {
bg = attr[1] bg = attr[1]
} }
for i := 2; i < len(attr); i++ { for i := 2; i < len(attr); i++ {
fmt.Fprintf(nfs.out, "\033[%dm", attr[i]) nfs.escape("%dm", attr[i])
} }
fmt.Fprintf(nfs.out, "\033[4%dm\033[3%dm%s\033[0m", bg, fg, str) nfs.escape("4%dm", bg).escape("3%dm", fg)
fmt.Fprintf(nfs.out, "%s", str)
nfs.escape("0m")
return nfs return nfs
} }
// }}} // }}}
func (nfs *NFS) prompt(arg ...string) { // {{{ func (nfs *NFS) prompt(arg ...string) string { // {{{
nfs.escape("2K", "G", "?25h") nfs.escape("2K").escape("G").escape("?25h")
line, rest := "", "" line, rest := "", ""
if len(arg) > 0 { if len(arg) > 0 {
@ -164,46 +169,45 @@ func (nfs *NFS) prompt(arg ...string) { // {{{
rest = arg[1] rest = arg[1]
} }
if nfs.color(fmt.Sprintf("[%s]%s> ", time.Now().Format("15:04:05"), nfs.Option("target")), nfs.Confi("pscolor")).color(line).color(rest); len(rest) > 0 { ps := fmt.Sprintf("[%s]%s> ", time.Now().Format("15:04:05"), nfs.Option("target"))
fmt.Fprintf(nfs.out, "\033[%dD", len(rest)) if nfs.color(ps, nfs.Confi("pscolor")).color(line).color(rest); len(rest) > 0 {
nfs.escape("%dD", len(rest))
} }
return ps
} }
// }}} // }}}
func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{ func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{
switch {
case nfs.out != nil:
str := fmt.Sprintf(str, arg...) str := fmt.Sprintf(str, arg...)
nfs.color(str)
ls := strings.Split(str, "\n") ls := strings.Split(str, "\n")
for i, l := range ls { for i, l := range ls {
rest := "" rest := ""
if len(nfs.pages) > 0 && !strings.HasSuffix(nfs.pages[len(nfs.pages)-1], "\n") { if len(nfs.pages) > 0 && !strings.HasSuffix(nfs.pages[len(nfs.pages)-1], "\n") {
rest = nfs.pages[len(nfs.pages)-1] rest = nfs.pages[len(nfs.pages)-1]
nfs.pages = nfs.pages[:len(nfs.pages)-1] nfs.pages = nfs.pages[:len(nfs.pages)-1]
} }
if i == len(ls)-1 { if rest += l; i < len(ls)-1 {
nfs.pages = append(nfs.pages, rest+l) rest += "\n"
} else {
nfs.pages = append(nfs.pages, rest+l+"\n")
} }
nfs.pages = append(nfs.pages, rest)
} }
switch {
case nfs.out != nil:
nfs.color(str)
case nfs.io != nil: case nfs.io != nil:
str := fmt.Sprintf(str, arg...) fmt.Fprint(nfs.io, str)
fmt.Fprintf(nfs.in, "%s", str)
default: default:
return false return false
} }
return true return true
} }
// }}} // }}}
func (nfs *NFS) page(buf []string, pos int, top int, height int) int { // {{{ func (nfs *NFS) page(buf []string, pos int, top int, height int) int { // {{{
nfs.escape("2J", "H") nfs.escape("2J").escape("H")
begin := pos begin := pos
for i := 0; i < height; i++ { for i := 0; i < height; i++ {
@ -234,6 +238,7 @@ func (nfs *NFS) View(buf []string, top int, height int) { // {{{
for { for {
switch ev := termbox.PollEvent(); ev.Type { switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey: case termbox.EventKey:
nfs.width, nfs.height = termbox.Size()
switch ev.Key { switch ev.Key {
case termbox.KeyCtrlC: case termbox.KeyCtrlC:
return return
@ -266,12 +271,10 @@ func (nfs *NFS) View(buf []string, top int, height int) { // {{{
// }}} // }}}
func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
if nfs.Cap("stream") != "stdio" { if !nfs.Caps("termbox") {
return nfs.in.Read(p) return nfs.in.Read(p)
} }
nfs.width, nfs.height = termbox.Size()
buf := make([]rune, 0, 1024) buf := make([]rune, 0, 1024)
rest := make([]rune, 0, 1024) rest := make([]rune, 0, 1024)
@ -285,6 +288,7 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
for { for {
switch ev := termbox.PollEvent(); ev.Type { switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey: case termbox.EventKey:
nfs.width, nfs.height = termbox.Size()
switch ev.Key { switch ev.Key {
case termbox.KeyCtrlC: case termbox.KeyCtrlC:
termbox.Close() termbox.Close()
@ -295,7 +299,7 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
nfs.page(nfs.pages, len(nfs.pages)-nfs.height, 0, nfs.height) nfs.page(nfs.pages, len(nfs.pages)-nfs.height, 0, nfs.height)
case termbox.KeyCtrlL: case termbox.KeyCtrlL:
nfs.escape("2J", "H") nfs.escape("2J").escape("H")
case termbox.KeyCtrlJ, termbox.KeyCtrlM: case termbox.KeyCtrlJ, termbox.KeyCtrlM:
buf = append(buf, rest...) buf = append(buf, rest...)
@ -496,6 +500,13 @@ 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 nfs.Message = m
if len(arg) > 0 && arg[0] == "scan" { if len(arg) > 0 && arg[0] == "scan" {
nfs.Caches["termbox"] = &ctx.Cache{Name: "termbox", Value: "false", Help: "termbox"}
nfs.Configs["color"] = &ctx.Config{Name: "color", Value: "false", Help: "color"}
nfs.Configs["fgcolor"] = &ctx.Config{Name: "fgcolor", Value: "9", Help: "fgcolor"}
nfs.Configs["bgcolor"] = &ctx.Config{Name: "bgcolor", Value: "9", Help: "bgcolor"}
nfs.Configs["pscolor"] = &ctx.Config{Name: "pscolor", Value: "2", Help: "pscolor"}
nfs.in = m.Optionv("in").(*os.File) nfs.in = m.Optionv("in").(*os.File)
bio := bufio.NewScanner(nfs) bio := bufio.NewScanner(nfs)
s, e := nfs.in.Stat() s, e := nfs.in.Stat()
@ -505,6 +516,8 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
if m.Cap("stream", arg[1]) == "stdio" { if m.Cap("stream", arg[1]) == "stdio" {
termbox.Init() termbox.Init()
defer termbox.Close() defer termbox.Close()
nfs.Cap("termbox", "true")
nfs.Conf("color", "true")
nfs.out = m.Optionv("out").(*os.File) nfs.out = m.Optionv("out").(*os.File)
} }
@ -737,12 +750,10 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
"git_status": &ctx.Config{Name: "git_status", Value: "-sb", Help: "版本控制状态参数"}, "git_status": &ctx.Config{Name: "git_status", Value: "-sb", Help: "版本控制状态参数"},
"git_diff": &ctx.Config{Name: "git_diff", Value: "--stat", Help: "版本控制状态参数"}, "git_diff": &ctx.Config{Name: "git_diff", Value: "--stat", Help: "版本控制状态参数"},
// "git_log": &ctx.Config{Name: "git_log", Value: "--pretty='%h %an %ad %s' --date='format:%m/%d %H:%M' --graph --stat", Help: "版本控制状态参数"},
"git_log": &ctx.Config{Name: "git_log", Value: "--pretty=%h %an(%ad) %s --date=format:%m/%d %H:%M --graph", Help: "版本控制状态参数"}, "git_log": &ctx.Config{Name: "git_log", Value: "--pretty=%h %an(%ad) %s --date=format:%m/%d %H:%M --graph", Help: "版本控制状态参数"},
"git_log_form": &ctx.Config{Name: "git_log", Value: "stat", Help: "版本控制状态参数"}, "git_log_form": &ctx.Config{Name: "git_log", Value: "stat", Help: "版本控制状态参数"},
"git_log_skip": &ctx.Config{Name: "git_log", Value: "0", Help: "版本控制状态参数"}, "git_log_skip": &ctx.Config{Name: "git_log", Value: "0", Help: "版本控制状态参数"},
"git_log_line": &ctx.Config{Name: "git_log", Value: "3", Help: "版本控制状态参数"}, "git_log_line": &ctx.Config{Name: "git_log", Value: "3", Help: "版本控制状态参数"},
// "git_log": &ctx.Config{Name: "git_log", Value: "--pretty=oneline --graph --stat", Help: "版本控制状态参数"},
"git_path": &ctx.Config{Name: "git_path", Value: ".", Help: "版本控制默认路径"}, "git_path": &ctx.Config{Name: "git_path", Value: ".", Help: "版本控制默认路径"},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{

View File

@ -191,14 +191,16 @@ func (yac *YAC) parse(m *ctx.Message, page int, void int, line string, level int
hash, word := 0, []string{} hash, word := 0, []string{}
for star, s := 0, page; s != 0 && len(line) > 0; { for star, s := 0, page; s != 0 && len(line) > 0; {
//解析空白 //解析空白
lex := m.Sesss("lex").Cmd("scan", line, yac.name(void)) lex := m.Sesss("lex")
lex.Cmd("scan", line, yac.name(void))
if lex.Result(0) == "-1" { if lex.Result(0) == "-1" {
break break
} }
//解析单词 //解析单词
line = lex.Result(1) line = lex.Result(1)
lex = m.Sesss("lex").Cmd("scan", line, yac.name(s)) lex = m.Sesss("lex")
lex.Cmd("scan", line, yac.name(s))
if lex.Result(0) == "-1" { if lex.Result(0) == "-1" {
break break
} }
@ -216,7 +218,7 @@ func (yac *YAC) parse(m *ctx.Message, page int, void int, line string, level int
} }
if state == nil { //嵌套语法递归解析 if state == nil { //嵌套语法递归解析
for i := 0; i < yac.Capi("ncell"); i++ { for i := 0; i < m.Capi("ncell"); i++ {
if x := yac.mat[s][byte(i)]; i < m.Capi("nlang") && x != nil { if x := yac.mat[s][byte(i)]; i < m.Capi("nlang") && x != nil {
if l, w := yac.parse(m, i, void, line, level+1); len(w) > 0 { if l, w := yac.parse(m, i, void, line, level+1); len(w) > 0 {
line, word = l, append(word, w...) line, word = l, append(word, w...)
@ -298,7 +300,7 @@ func (yac *YAC) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
} }
// }}} // }}}
func (yac *YAC) Start(m *ctx.Message, arg ...string) bool { // {{{ func (yac *YAC) Start(m *ctx.Message, arg ...string) (close bool) { // {{{
yac.Message = m yac.Message = m
if len(arg) > 0 && arg[0] == "parse" { if len(arg) > 0 && arg[0] == "parse" {
@ -311,6 +313,7 @@ func (yac *YAC) Start(m *ctx.Message, arg ...string) bool { // {{{
data := make(chan string, 1) data := make(chan string, 1)
next := make(chan bool, 1) next := make(chan bool, 1)
close = true
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
// m.Option("scan_end", true) // m.Option("scan_end", true)
@ -330,7 +333,9 @@ func (yac *YAC) Start(m *ctx.Message, arg ...string) bool { // {{{
//解析循环 //解析循环
for m.Cap("stream", nfs.Target().Name); !m.Options("scan_end"); next <- true { for m.Cap("stream", nfs.Target().Name); !m.Options("scan_end"); next <- true {
_, word := yac.parse(m, m.Optioni("page"), m.Optioni("void"), <-data, 1) line := <-data
_, word := yac.parse(m, m.Optioni("page"), m.Optioni("void"), line, 1)
if len(word) > 0 { if len(word) > 0 {
word = word[:len(word)-1] word = word[:len(word)-1]
if last := len(word) - 1; last >= 0 && len(word[last]) > 0 && word[last][len(word[last])-1] != '\n' { if last := len(word) - 1; last >= 0 && len(word[last]) > 0 && word[last][len(word[last])-1] != '\n' {