jager 3 years ago
parent f467bd2fb2
commit b1353fc647

@ -25,19 +25,16 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"github.com/jageros/hawox/logx" "github.com/rocket049/gocui"
"github.com/jroimartin/gocui" "log"
"os/signal" "math/rand"
"syscall"
"time" "time"
) )
var ( var (
viewArr = []string{"msg", "send"} viewArr = []string{"msg", "send"}
active = 1 active = 1
g *gocui.Gui
) )
func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) { func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
@ -51,26 +48,10 @@ func nextView(g *gocui.Gui, v *gocui.View) error {
nextIndex := (active + 1) % len(viewArr) nextIndex := (active + 1) % len(viewArr)
name := viewArr[nextIndex] name := viewArr[nextIndex]
out, err := g.View("send")
if err != nil {
return err
}
fmt.Fprintln(out, "\nGoing from view "+v.Name()+" to "+name)
if _, err := setCurrentViewOnTop(g, name); err != nil { if _, err := setCurrentViewOnTop(g, name); err != nil {
return err return err
} }
if nextIndex == 1 {
fmt.Fprintln(out, "")
}
if nextIndex == 0 || nextIndex == 3 {
g.Cursor = true
} else {
g.Cursor = false
}
active = nextIndex active = nextIndex
return nil return nil
} }
@ -82,7 +63,7 @@ func layout(g *gocui.Gui) error {
return err return err
} }
v.Title = "message" v.Title = "message"
//v.Editable = true v.Autoscroll = true
v.Wrap = true v.Wrap = true
} }
@ -98,12 +79,11 @@ func layout(g *gocui.Gui) error {
return err return err
} }
} }
if v, err := g.SetView("v3", maxX/4*3, 0, maxX-1, maxY-1); err != nil { if v, err := g.SetView("online", maxX/4*3, 0, maxX-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView { if err != gocui.ErrUnknownView {
return err return err
} }
v.Title = "online" v.Title = "online"
v.Wrap = true
} }
return nil return nil
} }
@ -112,44 +92,73 @@ func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit return gocui.ErrQuit
} }
func display(msg string) error { func sendMsg(g *gocui.Gui, v *gocui.View) error {
v, err := g.View("msg") byts := v.ReadEditor()
if len(byts) <= 0 {
v.Clear()
return v.SetCursor(0, 0)
}
str := string(byts)
msg, err := g.View("msg")
if err != nil { if err != nil {
return err return err
} }
_, err = fmt.Fprintln(v, msg)
flag := rand.Intn(3)
var name string
switch flag {
case 0:
name = "jager"
case 1:
name = "zhe"
case 2:
name = "lu"
}
msgStr := fmt.Sprintf("[%d]%s(%s): %s\n", flag, name, time.Now().Format("01-02 15:04:05"), str)
_, err = msg.Write([]byte(msgStr))
if err == nil {
v.Clear()
err = v.SetCursor(0, 0)
}
return err return err
} }
func read() (chan string, error) { func arrowUp(g *gocui.Gui, v *gocui.View) error {
var ch = make(chan string, 100) if v != nil {
ox, oy := v.Origin()
v, err := g.View("send") cx, cy := v.Cursor()
if err != nil { if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
return nil, err if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
} }
go func() { return nil
var str string }
for {
_, err := fmt.Fscanln(v, &str) func arrowDown(g *gocui.Gui, v *gocui.View) error {
if err == nil { if v != nil {
ch <- str cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
} }
} }
}() }
return nil
}
return ch, nil func backspace(g *gocui.Gui, v *gocui.View) error {
v.EditDelete(true)
return nil
} }
func main() { func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT) g, err := gocui.NewGui(gocui.OutputNormal)
defer cancel()
logx.Init(logx.InfoLevel, logx.SetFileOut("logs", "wechat"))
logx.Info("=============")
var err error
g, err = gocui.NewGui(gocui.OutputNormal)
if err != nil { if err != nil {
logx.Panic(err) log.Panic(err)
} }
defer g.Close() defer g.Close()
@ -160,36 +169,39 @@ func main() {
g.SetManagerFunc(layout) g.SetManagerFunc(layout)
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
logx.Panic(err) log.Panic(err)
} }
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil {
logx.Panic(err) log.Panic(err)
} }
go func() { if err := g.SetKeybinding("send", gocui.KeyDelete, gocui.ModNone, backspace); err != nil {
time.Sleep(time.Second*2) log.Panic(err)
logx.Info("-----------") }
ch, err := read()
if err != nil { if err := g.SetKeybinding("send", gocui.KeyBackspace, gocui.ModNone, backspace); err != nil {
logx.Error(err) log.Panic(err)
return }
}
for { if err := g.SetKeybinding("send", gocui.KeyBackspace2, gocui.ModNone, backspace); err != nil {
select { log.Panic(err)
case <-ctx.Done(): }
return
case msg := <-ch: if err := g.SetKeybinding("send", gocui.KeyEnter, gocui.ModNone, sendMsg); err != nil {
logx.Info(msg) log.Panic(err)
err := display(msg) }
if err != nil {
return if err := g.SetKeybinding("msg", gocui.KeyArrowUp, gocui.ModNone, arrowUp); err != nil {
} log.Panic(err)
} }
}
}() if err := g.SetKeybinding("msg", gocui.KeyArrowDown, gocui.ModNone, arrowDown); err != nil {
log.Panic(err)
}
err = g.MainLoop() err = g.MainLoop()
if err != nil { if err != nil {
logx.Error(err) log.Println(err)
} }
} }

@ -6,6 +6,7 @@ require (
github.com/gin-gonic/gin v1.7.4 github.com/gin-gonic/gin v1.7.4
github.com/jageros/hawox v0.0.5 github.com/jageros/hawox v0.0.5
github.com/jroimartin/gocui v0.5.0 github.com/jroimartin/gocui v0.5.0
github.com/rocket049/gocui v0.3.2
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376
) )

@ -405,6 +405,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/rocket049/gocui v0.3.2 h1:6QEaEX85VheVRLwA5FjAe2tswlIkIg0mgoeBHEguRzA=
github.com/rocket049/gocui v0.3.2/go.mod h1:oKkm50/BFqMgu7hwBRMs7/uketHe9hRMoo29X1gxK9A=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

@ -0,0 +1,50 @@
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: editor
* @Date: 2021/10/19 6:19
* @package: view
* @Version: v1.0.0
*
* @Description:
*
*/
package view
import (
"bytes"
"github.com/jroimartin/gocui"
"github.com/mattn/go-runewidth"
)
func modifyCJK(p []byte) []byte {
buf := bytes.NewBuffer(bytes.Trim(p, " \n\t"))
sz := len(buf.String())
buf1 := bytes.NewBufferString("")
var r rune
var wr bool
for i := 0; i < sz; i++ {
r, _, _ = buf.ReadRune()
if r != rune(0) && wr == false {
buf1.WriteRune(r)
} else if wr == true {
if r != rune(' ') {
buf1.WriteRune(r)
}
}
wr = runewidth.RuneWidth(r) > 1
}
return buf1.Bytes()
}
//ReadEditor Read byte array from editor 'v', delete the auto appended blank after CJK runes.
func ReadEditor(v *gocui.View) []byte {
var b = make([]byte, 300)
n, _ := v.Read(b)
if n > 0 {
return modifyCJK(b[:n])
} else {
return nil
}
}
Loading…
Cancel
Save