]> Zhao Yanbai Git Server - acecode.git/commitdiff
...
authoracevest <zhaoyanbai@126.com>
Tue, 14 Jul 2020 16:03:10 +0000 (00:03 +0800)
committeracevest <zhaoyanbai@126.com>
Tue, 14 Jul 2020 16:04:19 +0000 (00:04 +0800)
learn/go/gocui_input.go [new file with mode: 0644]
learn/go/time_fmt_trans.go [new file with mode: 0644]

diff --git a/learn/go/gocui_input.go b/learn/go/gocui_input.go
new file mode 100644 (file)
index 0000000..11c6087
--- /dev/null
@@ -0,0 +1,128 @@
+package main
+
+import (
+       "fmt"
+       "log"
+       "time"
+
+       "github.com/jroimartin/gocui"
+)
+
+// 输出窗口
+func viewOutput(g *gocui.Gui, x0, y0, x1, y1 int) error {
+       v, err := g.SetView("out", x0, y0, x1, y1)
+       if err != nil {
+               if err != gocui.ErrUnknownView {
+                       return err
+               }
+               v.Wrap = true
+               v.Overwrite = false
+               v.Autoscroll = true
+               v.SelBgColor = gocui.ColorRed
+               v.Title = "Messages"
+       }
+       return nil
+}
+
+// 输入窗口(编辑器)
+func viewInput(g *gocui.Gui, x0, y0, x1, y1 int) error {
+       if v, err := g.SetView("main", x0, y0, x1, y1); err != nil {
+               if err != gocui.ErrUnknownView {
+                       return err
+               }
+               v.Editable = true
+               v.Wrap = true
+               v.Overwrite = false
+               if _, err := g.SetCurrentView("main"); err != nil {
+                       return err
+               }
+               //fmt.Fprintf(v, "fcuk")
+       }
+       return nil
+}
+
+// 显示输出、输入窗口
+func layout(g *gocui.Gui) error {
+       maxX, maxY := g.Size()
+       if err := viewOutput(g, 1, 1, maxX-1, maxY-4); err != nil {
+               return err
+       }
+       if err := viewInput(g, 1, maxY-3, maxX-1, maxY-1); err != nil {
+               return err
+       }
+       return nil
+}
+
+// 退出函数
+func quit(g *gocui.Gui, v *gocui.View) error {
+       return gocui.ErrQuit
+}
+
+type VOT struct {
+       Msg string
+}
+
+func (self VOT) Show(g *gocui.Gui) error {
+       v, err := g.View("out")
+       if err != nil {
+               return nil
+       }
+       fmt.Fprintf(v, "%v", self.Msg)
+       return nil
+}
+
+func viewPrint(g *gocui.Gui, msg string) {
+       var out VOT
+       out.Msg = msg
+       g.Update(out.Show)
+}
+
+func main() {
+       g, err := gocui.NewGui(gocui.OutputNormal)
+       if err != nil {
+               //log.Panicln(err)
+       }
+
+       g.Cursor = true
+       g.Mouse = false
+       g.ASCII = false
+
+       go func() {
+               ticker := time.NewTicker(time.Second)
+               for t := range ticker.C {
+                       s := fmt.Sprintf("ticker %v\n", t)
+                       viewPrint(g, s)
+               }
+       }()
+
+       g.SetManagerFunc(layout)
+       if err := g.SetKeybinding("main", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
+               log.Panicln(err)
+       }
+       if err := g.SetKeybinding("main", gocui.KeyEnter, gocui.ModNone, updateInput); err != nil {
+               log.Panicln(err)
+       }
+       if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
+               log.Panicln(err)
+       }
+       g.Close()
+}
+
+//updateInput 当按下ENTER键时调用,把输入的内容复制到输出窗口中
+func updateInput(g *gocui.Gui, cv *gocui.View) error {
+       v, err := g.View("out")
+       if cv != nil && err == nil {
+               var b = make([]byte, 300)
+               n, _ := cv.Read(b)
+               v.Write([]byte("FUCK:"))
+               v.Write(b[:n])
+               if n == 0 {
+                       v.Write([]byte("\n"))
+               }
+               v.Autoscroll = true
+       }
+       l := len(cv.Buffer())
+       cv.MoveCursor(0-l, 0, true)
+       cv.Clear()
+       return nil
+}
diff --git a/learn/go/time_fmt_trans.go b/learn/go/time_fmt_trans.go
new file mode 100644 (file)
index 0000000..04d237d
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: time_fmt_trans.go
+ *      Author: Zhao Yanbai
+ *              2020-07-14 16:47:39 星期二 CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+
+import (
+       "fmt"
+       "time"
+)
+
+func main() {
+       defer fmt.Println("Program Exited...")
+
+       {
+               // 时间转时间戳
+               now := time.Now()
+               timestamp := now.Unix()
+               fmt.Println("当前时间转换到时间戳:", timestamp)
+       }
+
+       {
+               // 时间戳转时间
+               timestamp := time.Now().Unix()
+               now := time.Unix(timestamp, 0)
+               fmt.Println("当前时间戳转换到时间:", now)
+       }
+
+       {
+               // 时间格式化成字符串
+               str := ""
+               now := time.Now()
+
+               str = now.Format("2006-01-02 03:04:05")
+               fmt.Println("时间格式化成字符串 12小时制:", str)
+
+               str = now.Format("2006-01-02 15:04:05")
+               fmt.Println("时间格式化成字符串 24小时制:", str)
+       }
+
+       {
+               type TimeFmt struct {
+                       tip string
+                       fmt string
+               }
+
+               var timefmts = []TimeFmt{
+                       {"ANSIC", time.ANSIC},
+                       {"UnixDate", time.UnixDate},
+                       {"RubyDate", time.RubyDate},
+                       {"RFC822", time.RFC822},
+                       {"RFC822Z", time.RFC822Z},
+                       {"RFC850", time.RFC850},
+                       {"RFC1123", time.RFC1123},
+                       {"RFC1123Z", time.RFC1123Z},
+                       {"RFC3339", time.RFC3339},
+                       {"RFC3339Nano", time.RFC3339Nano},
+                       {"Kitchen", time.Kitchen},
+                       {"Stamp", time.Stamp},
+                       {"StampMilli", time.StampMilli},
+                       {"StampMicro", time.StampMicro},
+                       {"StampNano", time.StampNano},
+               }
+
+               now := time.Now()
+               for _, f := range timefmts {
+                       fmt.Printf("时间格式化成 %-14s: %s\n", f.tip, now.Format(f.fmt))
+               }
+       }
+
+       {
+               // 时间字符串转换成时间
+               str := ""
+               const TIME_LAYOUT = "2006-01-02 15:04:05"
+               str = time.Now().Format(TIME_LAYOUT)
+               now, _ := time.ParseInLocation(TIME_LAYOUT, str, time.Local)
+               fmt.Println("时间字符串: ", str, " 转换成本地时间: ", now)
+       }
+
+}