|
|
@ -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()
|
|
|
|
|
|
|
|
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")
|
|
|
|
func arrowDown(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if err != nil {
|
|
|
|
if v != nil {
|
|
|
|
return nil, err
|
|
|
|
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() {
|
|
|
|
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:
|
|
|
|
|
|
|
|
logx.Info(msg)
|
|
|
|
|
|
|
|
err := display(msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
err = g.MainLoop()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|