jager 3 years ago
parent f467bd2fb2
commit b1353fc647

@ -25,19 +25,16 @@
package main
import (
"context"
"fmt"
"github.com/jageros/hawox/logx"
"github.com/jroimartin/gocui"
"os/signal"
"syscall"
"github.com/rocket049/gocui"
"log"
"math/rand"
"time"
)
var (
viewArr = []string{"msg", "send"}
active = 1
g *gocui.Gui
)
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)
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 {
return err
}
if nextIndex == 1 {
fmt.Fprintln(out, "")
}
if nextIndex == 0 || nextIndex == 3 {
g.Cursor = true
} else {
g.Cursor = false
}
active = nextIndex
return nil
}
@ -82,7 +63,7 @@ func layout(g *gocui.Gui) error {
return err
}
v.Title = "message"
//v.Editable = true
v.Autoscroll = true
v.Wrap = true
}
@ -98,12 +79,11 @@ func layout(g *gocui.Gui) error {
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 {
return err
}
v.Title = "online"
v.Wrap = true
}
return nil
}
@ -112,44 +92,73 @@ func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func display(msg string) error {
v, err := g.View("msg")
func sendMsg(g *gocui.Gui, v *gocui.View) error {
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 {
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
}
func read() (chan string, error) {
var ch = make(chan string, 100)
func arrowUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
}
return nil
}
v, err := g.View("send")
if err != nil {
return nil, err
func arrowDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
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
}
go func() {
var str string
for {
_, err := fmt.Fscanln(v, &str)
if err == nil {
ch <- str
}
}
}()
return nil
}
return ch, nil
func backspace(g *gocui.Gui, v *gocui.View) error {
v.EditDelete(true)
return nil
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer cancel()
logx.Init(logx.InfoLevel, logx.SetFileOut("logs", "wechat"))
logx.Info("=============")
var err error
g, err = gocui.NewGui(gocui.OutputNormal)
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
logx.Panic(err)
log.Panic(err)
}
defer g.Close()
@ -160,36 +169,39 @@ func main() {
g.SetManagerFunc(layout)
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 {
logx.Panic(err)
log.Panic(err)
}
go func() {
time.Sleep(time.Second*2)
logx.Info("-----------")
ch, err := read()
if err != nil {
logx.Error(err)
return
}
for {
select {
case <-ctx.Done():
return
case msg := <-ch:
logx.Info(msg)
err := display(msg)
if err != nil {
return
if err := g.SetKeybinding("send", gocui.KeyDelete, gocui.ModNone, backspace); err != nil {
log.Panic(err)
}
if err := g.SetKeybinding("send", gocui.KeyBackspace, gocui.ModNone, backspace); err != nil {
log.Panic(err)
}
if err := g.SetKeybinding("send", gocui.KeyBackspace2, gocui.ModNone, backspace); err != nil {
log.Panic(err)
}
if err := g.SetKeybinding("send", gocui.KeyEnter, gocui.ModNone, sendMsg); err != nil {
log.Panic(err)
}
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()
if err != nil {
logx.Error(err)
log.Println(err)
}
}

@ -6,6 +6,7 @@ require (
github.com/gin-gonic/gin v1.7.4
github.com/jageros/hawox v0.0.5
github.com/jroimartin/gocui v0.5.0
github.com/rocket049/gocui v0.3.2
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.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
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/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
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