1
0
forked from x/ContextOS

tce pro nfs.color

This commit is contained in:
shaoying 2018-04-25 21:34:52 +08:00
parent 11f0531d15
commit fb5eb6c5ed

View File

@ -31,7 +31,7 @@ type NFS struct {
buf []string buf []string
pages []string pages []string
x, y, width, height int width, height int
*ctx.Message *ctx.Message
*ctx.Context *ctx.Context
@ -52,51 +52,38 @@ func (nfs *NFS) insert(rest []rune, letters []rune) []rune { // {{{
// }}} // }}}
func (nfs *NFS) color(str string, attr ...termbox.Attribute) { // {{{ func (nfs *NFS) escape(key ...string) *NFS { // {{{
fg := termbox.Attribute(nfs.Confi("color")) for _, k := range key {
fmt.Fprintf(nfs.out, "\033[%s", k)
}
return nfs
}
// }}}
func (nfs *NFS) color(str string, attr ...int) *NFS { // {{{
fg := nfs.Confi("color")
if len(attr) > 0 { if len(attr) > 0 {
fg = attr[0] fg = attr[0]
} }
bg := termbox.Attribute(nfs.Confi("backcolor")) bg := nfs.Confi("backcolor")
if len(attr) > 1 { if len(attr) > 1 {
bg = attr[1] bg = attr[1]
} }
for _, v := range []rune(str) { for i := 2; i < len(attr); i++ {
if v == '\n' { fmt.Fprintf(nfs.out, "\033[%dm", attr[i])
nfs.x = 0
nfs.y++
continue
}
if nfs.Confs("screen") {
termbox.SetCell(nfs.x, nfs.y, v, fg, bg)
}
if nfs.x++; nfs.x >= nfs.width {
nfs.x = 0
nfs.y++
}
if nfs.Confs("screen") {
if nfs.y >= nfs.height {
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
nfs.y = 0
}
}
} }
if !nfs.Confs("screen") {
if fg == termbox.ColorWhite { fmt.Fprintf(nfs.out, "\033[4%dm\033[3%dm%s\033[0m", bg, fg, str)
fg = fg + 8 return nfs
}
fmt.Fprintf(nfs.out, "\033[4%dm\033[3%dm%s\033[0m", bg-1, fg-1, str)
}
termbox.SetCursor(nfs.x, nfs.y)
termbox.Flush()
} }
// }}} // }}}
func (nfs *NFS) clear(arg ...string) { // {{{ func (nfs *NFS) prompt(arg ...string) { // {{{
line := "" nfs.escape("2K", "G", "?25h")
rest := ""
line, rest := "", ""
if len(arg) > 0 { if len(arg) > 0 {
line = arg[0] line = arg[0]
} }
@ -104,13 +91,9 @@ func (nfs *NFS) clear(arg ...string) { // {{{
rest = arg[1] rest = arg[1]
} }
nfs.y-- if nfs.color(nfs.cli.Conf("PS1"), nfs.Confi("pscolor")).color(line).color(rest); len(rest) > 0 {
nfs.x = 0 fmt.Fprintf(nfs.out, "\033[%dD", len(rest))
nfs.color(strings.Repeat(" ", nfs.width)) }
nfs.x = 0
ps := nfs.cli.Conf("PS1")
nfs.color(fmt.Sprintf("%s%s%s", ps, line, rest))
} }
// }}} // }}}
@ -138,7 +121,6 @@ func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{
case nfs.io != nil: case nfs.io != nil:
str := fmt.Sprintf(str, arg...) str := fmt.Sprintf(str, arg...)
nfs.y += strings.Count(str, "\n")
fmt.Fprintf(nfs.in, "%s", str) fmt.Fprintf(nfs.in, "%s", str)
default: default:
return false return false
@ -147,16 +129,11 @@ func (nfs *NFS) print(str string, arg ...interface{}) bool { // {{{
} }
// }}} // }}}
func (nfs *NFS) page(buf []string, pos int) { // {{{ func (nfs *NFS) page(buf []string, pos int) int { // {{{
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack) nfs.escape("2J", "H")
termbox.SetCursor(0, 0)
termbox.Sync()
nfs.y = 0
nfs.x = 0
begin := pos begin := pos
for i := 0; i < nfs.height-1; i++ { for i := 0; i < nfs.height; i++ {
if pos < len(buf) && pos >= 0 { if pos < len(buf) && pos >= 0 {
if len(buf[pos]) > nfs.width { if len(buf[pos]) > nfs.width {
nfs.color(fmt.Sprintf("%s", buf[pos][:nfs.width])) nfs.color(fmt.Sprintf("%s", buf[pos][:nfs.width]))
@ -168,14 +145,14 @@ func (nfs *NFS) page(buf []string, pos int) { // {{{
nfs.color("\n") nfs.color("\n")
} }
} }
nfs.y = nfs.height - 1
nfs.x = 0 nfs.escape("E").color(fmt.Sprintf("%d/%d", begin, len(nfs.pages)), nfs.Confi("statuscolor"), nfs.Confi("statusbackcolor"))
nfs.color(fmt.Sprintf("%d/%d", begin, len(nfs.pages)), termbox.ColorRed, termbox.ColorGreen) return pos
} }
// }}} // }}}
func (nfs *NFS) View(y int) { // {{{ func (nfs *NFS) View() { // {{{
pos := len(nfs.pages) - y pos := len(nfs.pages) - nfs.height
if pos < 0 { if pos < 0 {
pos = 0 pos = 0
} }
@ -241,20 +218,11 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
os.Exit(1) os.Exit(1)
case termbox.KeyCtrlV: case termbox.KeyCtrlV:
y := nfs.y + 1 nfs.View()
nfs.View(y) nfs.page(nfs.pages, len(nfs.pages)-nfs.height)
nfs.page(nfs.pages, len(nfs.pages)-y)
nfs.y = y - 1
nfs.x = 0
// termbox.SetCell(nfs.x, nfs.y, ' ', termbox.ColorWhite, termbox.ColorBlack)
// termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
case termbox.KeyCtrlL: case termbox.KeyCtrlL:
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack) nfs.escape("2J", "H")
termbox.SetCursor(0, 0)
termbox.Sync()
nfs.y = 0
nfs.x = 0
case termbox.KeyCtrlJ, termbox.KeyCtrlM: case termbox.KeyCtrlJ, termbox.KeyCtrlM:
tab = tab[:0] tab = tab[:0]
@ -386,11 +354,10 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
nfs.print(string(ev.Ch)) nfs.print(string(ev.Ch))
} }
} }
nfs.clear(string(buf), string(rest)) nfs.prompt(string(buf), string(rest))
} }
} }
return return
} }
// }}} // }}}
@ -559,10 +526,12 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
nfs.cli = cli nfs.cli = cli
yac := m.Find(cli.Conf("yac")) yac := m.Find(cli.Conf("yac"))
bio := bufio.NewScanner(nfs) bio := bufio.NewScanner(nfs)
if m.Cap("stream") == "stdio" { if m.Cap("stream") == "stdio" {
termbox.Init() termbox.Init()
nfs.width, nfs.height = termbox.Size() defer termbox.Close()
} }
nfs.Context.Master(nil) nfs.Context.Master(nil)
pos := 0 pos := 0
@ -572,10 +541,8 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
goto out goto out
} }
if len(arg) > 2 { nfs.pages = append(nfs.pages, nfs.cli.Conf("PS1"))
nfs.print("%v\n", arg[2]) nfs.prompt()
}
nfs.print("%s", cli.Conf("PS1"))
for rest, text := "", ""; pos < m.Capi("nline") || bio.Scan(); { for rest, text := "", ""; pos < m.Capi("nline") || bio.Scan(); {
if pos == m.Capi("nline") { if pos == m.Capi("nline") {
@ -614,7 +581,8 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
} }
} }
nfs.print("%s", cli.Conf("PS1")) nfs.pages = append(nfs.pages, fmt.Sprintf("\033[32m%s\033[m", nfs.cli.Conf("PS1")))
nfs.prompt()
} }
out: out:
@ -623,9 +591,6 @@ out:
} else { } else {
m.Cap("status", "stop") m.Cap("status", "stop")
} }
if m.Cap("stream") == "stdio" {
termbox.Close()
}
return false return false
} }
@ -651,10 +616,12 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
"nfile": &ctx.Cache{Name: "nfile", Value: "0", Help: "已经打开的文件数量"}, "nfile": &ctx.Cache{Name: "nfile", Value: "0", Help: "已经打开的文件数量"},
}, },
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"}, "size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"},
"screen": &ctx.Config{Name: "screen", Value: "false", Help: "读取文件的默认大小值"}, "color": &ctx.Config{Name: "color", Value: "9", Help: "读取文件的默认大小值"},
"color": &ctx.Config{Name: "color", Value: "8", Help: "读取文件的默认大小值"}, "backcolor": &ctx.Config{Name: "backcolor", Value: "9", Help: "读取文件的默认大小值"},
"backcolor": &ctx.Config{Name: "color", Value: "1", Help: "读取文件的默认大小值"}, "pscolor": &ctx.Config{Name: "pscolor", Value: "2", Help: "读取文件的默认大小值"},
"statuscolor": &ctx.Config{Name: "statuspscolor", Value: "1", Help: "读取文件的默认大小值"},
"statusbackcolor": &ctx.Config{Name: "statusbackcolor", Value: "2", Help: "读取文件的默认大小值"},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"buffer": &ctx.Command{Name: "buffer [index string]", Help: "扫描文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "buffer": &ctx.Command{Name: "buffer [index string]", Help: "扫描文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {