You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
3.9 KiB
Go

3 years ago
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: main
* @Date: 2021/10/18 11:15
* @package: wechat
* @Version: v1.0.0
*
* @Description:
*
*/
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: view
* @Date: 2021/10/15 6:27
* @package: view
* @Version: v1.0.0
*
* @Description:
*
*/
package main
import (
"fmt"
3 years ago
"github.com/rocket049/gocui"
"log"
"math/rand"
3 years ago
"time"
)
var (
viewArr = []string{"msg", "send"}
active = 1
)
func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
if _, err := g.SetCurrentView(name); err != nil {
return nil, err
}
return g.SetViewOnTop(name)
}
func nextView(g *gocui.Gui, v *gocui.View) error {
nextIndex := (active + 1) % len(viewArr)
name := viewArr[nextIndex]
if _, err := setCurrentViewOnTop(g, name); err != nil {
return err
}
active = nextIndex
return nil
}
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("msg", 0, 0, maxX/4*3-1, maxY/5*4-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "message"
3 years ago
v.Autoscroll = true
3 years ago
v.Wrap = true
}
if v, err := g.SetView("send", 0, maxY/5*4, maxX/4*3-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "send"
v.Editable = true
v.Wrap = true
v.Autoscroll = true
if _, err = setCurrentViewOnTop(g, "send"); err != nil {
return err
}
}
3 years ago
if v, err := g.SetView("online", maxX/4*3, 0, maxX-1, maxY-1); err != nil {
3 years ago
if err != gocui.ErrUnknownView {
return err
}
v.Title = "online"
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
3 years ago
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")
3 years ago
if err != nil {
return err
}
3 years ago
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)
}
3 years ago
return err
}
3 years ago
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
}
}
3 years ago
}
3 years ago
return nil
}
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
3 years ago
}
}
3 years ago
}
return nil
}
3 years ago
3 years ago
func backspace(g *gocui.Gui, v *gocui.View) error {
v.EditDelete(true)
return nil
3 years ago
}
func main() {
3 years ago
g, err := gocui.NewGui(gocui.OutputNormal)
3 years ago
if err != nil {
3 years ago
log.Panic(err)
3 years ago
}
defer g.Close()
g.Highlight = true
g.Cursor = true
g.SelFgColor = gocui.ColorGreen
g.SetManagerFunc(layout)
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
3 years ago
log.Panic(err)
3 years ago
}
3 years ago
3 years ago
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil {
3 years ago
log.Panic(err)
3 years ago
}
3 years ago
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)
}
3 years ago
err = g.MainLoop()
if err != nil {
3 years ago
log.Println(err)
3 years ago
}
}