From ddd52ca9335cb5c4cf8ec6dfc1848de566b592b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:39:25 +0800 Subject: [PATCH 1/9] =?UTF-8?q?feat:=20=E7=A6=81=E7=94=A8=E5=BF=AB?= =?UTF-8?q?=E9=80=9F=E7=BC=96=E8=BE=91&=E4=BC=98=E5=8C=96=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/gocq/main.go | 11 ++++++++--- global/terminal/doc.go | 2 +- global/terminal/double_click.go | 2 +- global/terminal/quick_edit.go | 6 ++++++ global/terminal/quick_edit_windows.go | 19 +++++++++++++++++++ global/terminal/vt100.go | 6 ++++++ global/terminal/vt100_windows.go | 19 +++++++++++++++++++ main.go | 4 ++++ 8 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 global/terminal/quick_edit.go create mode 100644 global/terminal/quick_edit_windows.go create mode 100644 global/terminal/vt100.go create mode 100644 global/terminal/vt100_windows.go diff --git a/cmd/gocq/main.go b/cmd/gocq/main.go index fded837..adde588 100644 --- a/cmd/gocq/main.go +++ b/cmd/gocq/main.go @@ -41,8 +41,10 @@ var allowStatus = [...]client.UserOnlineStatus{ client.StatusGaming, client.StatusVacationing, client.StatusWatchingTV, client.StatusFitness, } -// Main 启动主程序 -func Main() { +// InitBase 解析参数并检测 +// +// 如果在 windows 下双击打开了程序,程序将在此函数释出脚本后终止 +func InitBase() { base.Parse() if !base.FastStart && terminal.RunningByDoubleClick() { err := terminal.NoMoreDoubleClick() @@ -50,7 +52,7 @@ func Main() { log.Errorf("遇到错误: %v", err) time.Sleep(time.Second * 5) } - return + os.Exit(0) } switch { case base.LittleH: @@ -65,7 +67,10 @@ func Main() { } } base.Init() +} +// Main 启动主程序,必须在 InitBase 之后执行 +func Main() { rotateOptions := []rotatelogs.Option{ rotatelogs.WithRotationTime(time.Hour * 24), } diff --git a/global/terminal/doc.go b/global/terminal/doc.go index 55212d9..80afc19 100644 --- a/global/terminal/doc.go +++ b/global/terminal/doc.go @@ -1,2 +1,2 @@ -// Package terminal 包含用于检测在windows下是否通过双击运行go-cqhttp的函数 +// Package terminal 包含用于检测在windows下是否通过双击运行go-cqhttp, 禁用快速编辑, 启用VT100的函数 package terminal diff --git a/global/terminal/double_click.go b/global/terminal/double_click.go index 53f0e36..dbe3d9b 100644 --- a/global/terminal/double_click.go +++ b/global/terminal/double_click.go @@ -2,7 +2,7 @@ package terminal -// RunningByDoubleClick 检查是否通过双击直接运行,非Windows系统永远返回false +// RunningByDoubleClick 检查是否通过双击直接运行,非Windows系统永远返回false func RunningByDoubleClick() bool { return false } diff --git a/global/terminal/quick_edit.go b/global/terminal/quick_edit.go new file mode 100644 index 0000000..b85bf71 --- /dev/null +++ b/global/terminal/quick_edit.go @@ -0,0 +1,6 @@ +package terminal + +// DisableQuickEdit 禁用快速编辑,非Windows系统永远返回nil +func DisableQuickEdit() error { + return nil +} diff --git a/global/terminal/quick_edit_windows.go b/global/terminal/quick_edit_windows.go new file mode 100644 index 0000000..1186b48 --- /dev/null +++ b/global/terminal/quick_edit_windows.go @@ -0,0 +1,19 @@ +package terminal + +import "golang.org/x/sys/windows" + +// DisableQuickEdit 禁用快速编辑 +func DisableQuickEdit() error { + stdin := windows.Handle(os.Stdin.Fd()) + + var mode uint32 + err := windows.GetConsoleMode(stdin, &mode) + if err != nil { + return err + } + + mode &^= windows.ENABLE_QUICK_EDIT_MODE // 禁用快速编辑模式 + mode |= windows.ENABLE_EXTENDED_FLAGS // 启用扩展标志 + + return windows.SetConsoleMode(stdin, mode) +} diff --git a/global/terminal/vt100.go b/global/terminal/vt100.go new file mode 100644 index 0000000..ff135a4 --- /dev/null +++ b/global/terminal/vt100.go @@ -0,0 +1,6 @@ +package terminal + +// EnableVT100 启用颜色、控制字符,非Windows系统永远返回nil +func EnableVT100() error { + return nil +} diff --git a/global/terminal/vt100_windows.go b/global/terminal/vt100_windows.go new file mode 100644 index 0000000..b879ae1 --- /dev/null +++ b/global/terminal/vt100_windows.go @@ -0,0 +1,19 @@ +package terminal + +import "golang.org/x/sys/windows" + +// EnableVT100 启用颜色、控制字符 +func EnableVT100() error { + stdout := windows.Handle(os.Stdout.Fd()) + + var mode uint32 + err := windows.GetConsoleMode(stdout, &mode) + if err != nil { + return err + } + + mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING // 启用虚拟终端处理 + mode |= windows.ENABLE_PROCESSED_OUTPUT // 启用处理后的输出 + + return windows.SetConsoleMode(stdout, mode) +} diff --git a/main.go b/main.go index 30838f7..74dbfbe 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/Mrs4s/go-cqhttp/cmd/gocq" + "github.com/Mrs4s/go-cqhttp/global/terminal" _ "github.com/Mrs4s/go-cqhttp/db/leveldb" // leveldb 数据库支持 _ "github.com/Mrs4s/go-cqhttp/modules/silk" // silk编码模块 @@ -13,5 +14,8 @@ import ( ) func main() { + gocq.InitBase() + terminal.DisableQuickEdit() + terminal.EnableVT100() gocq.Main() } From 2830676e3bc43b370477c704655ff9e8cb9b4190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:44:46 +0800 Subject: [PATCH 2/9] make lint happy --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 74dbfbe..1c77d8c 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ import ( func main() { gocq.InitBase() - terminal.DisableQuickEdit() - terminal.EnableVT100() + _ = terminal.DisableQuickEdit() + _ = terminal.EnableVT100() gocq.Main() } From 63d9ffa90bb2cce8594371eb41267c2b55f799e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:47:40 +0800 Subject: [PATCH 3/9] make lint happy --- global/terminal/quick_edit.go | 2 ++ global/terminal/vt100.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/global/terminal/quick_edit.go b/global/terminal/quick_edit.go index b85bf71..afa6b4c 100644 --- a/global/terminal/quick_edit.go +++ b/global/terminal/quick_edit.go @@ -1,3 +1,5 @@ +//go:build !windows + package terminal // DisableQuickEdit 禁用快速编辑,非Windows系统永远返回nil diff --git a/global/terminal/vt100.go b/global/terminal/vt100.go index ff135a4..34bda9e 100644 --- a/global/terminal/vt100.go +++ b/global/terminal/vt100.go @@ -1,3 +1,5 @@ +//go:build !windows + package terminal // EnableVT100 启用颜色、控制字符,非Windows系统永远返回nil From 1b8ebf55a5df3dbb59e021f3b399720d7f02ab2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:49:54 +0800 Subject: [PATCH 4/9] make lint happy --- global/terminal/quick_edit_windows.go | 6 +++++- global/terminal/vt100_windows.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/global/terminal/quick_edit_windows.go b/global/terminal/quick_edit_windows.go index 1186b48..3ac9aef 100644 --- a/global/terminal/quick_edit_windows.go +++ b/global/terminal/quick_edit_windows.go @@ -1,6 +1,10 @@ package terminal -import "golang.org/x/sys/windows" +import ( + "os" + + "golang.org/x/sys/windows" +) // DisableQuickEdit 禁用快速编辑 func DisableQuickEdit() error { diff --git a/global/terminal/vt100_windows.go b/global/terminal/vt100_windows.go index b879ae1..0105435 100644 --- a/global/terminal/vt100_windows.go +++ b/global/terminal/vt100_windows.go @@ -1,6 +1,10 @@ package terminal -import "golang.org/x/sys/windows" +import ( + "os" + + "golang.org/x/sys/windows" +) // EnableVT100 启用颜色、控制字符 func EnableVT100() error { From dbddd18e3af9546a6935e8999a940a58845a71ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 21:14:35 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/gocq/main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/gocq/main.go b/cmd/gocq/main.go index adde588..bcedbbc 100644 --- a/cmd/gocq/main.go +++ b/cmd/gocq/main.go @@ -43,7 +43,9 @@ var allowStatus = [...]client.UserOnlineStatus{ // InitBase 解析参数并检测 // -// 如果在 windows 下双击打开了程序,程序将在此函数释出脚本后终止 +// 如果在 windows 下双击打开了程序,程序将在此函数释出脚本后终止; +// 如果传入 -h 参数,程序将打印帮助后终止; +// 如果传入 -d 参数,程序将在启动 daemon 后终止。 func InitBase() { base.Parse() if !base.FastStart && terminal.RunningByDoubleClick() { From c4d703dc866e2a199d73b1def6be658322e3524b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 22:11:05 +0800 Subject: [PATCH 6/9] fix mouse scroll --- global/terminal/quick_edit_windows.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/global/terminal/quick_edit_windows.go b/global/terminal/quick_edit_windows.go index 3ac9aef..81fa7ec 100644 --- a/global/terminal/quick_edit_windows.go +++ b/global/terminal/quick_edit_windows.go @@ -19,5 +19,8 @@ func DisableQuickEdit() error { mode &^= windows.ENABLE_QUICK_EDIT_MODE // 禁用快速编辑模式 mode |= windows.ENABLE_EXTENDED_FLAGS // 启用扩展标志 + mode &^= windows.ENABLE_MOUSE_INPUT // 禁用鼠标输入 + mode |= windows.ENABLE_PROCESSED_INPUT // 启用控制输入 + return windows.SetConsoleMode(stdin, mode) } From d867451ef63d23970e3c3379eef3590ffe53a782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 22:40:52 +0800 Subject: [PATCH 7/9] fix input --- global/terminal/quick_edit_windows.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/global/terminal/quick_edit_windows.go b/global/terminal/quick_edit_windows.go index 81fa7ec..3a1dfaa 100644 --- a/global/terminal/quick_edit_windows.go +++ b/global/terminal/quick_edit_windows.go @@ -22,5 +22,11 @@ func DisableQuickEdit() error { mode &^= windows.ENABLE_MOUSE_INPUT // 禁用鼠标输入 mode |= windows.ENABLE_PROCESSED_INPUT // 启用控制输入 + mode &^= windows.ENABLE_INSERT_MODE // 禁用插入模式 + mode |= windows.ENABLE_ECHO_INPUT | windows.ENABLE_LINE_INPUT // 启用输入回显&逐行输入 + + mode &^= windows.ENABLE_WINDOW_INPUT // 禁用窗口输入 + mode &^= windows.ENABLE_VIRTUAL_TERMINAL_INPUT // 禁用虚拟终端输入 + return windows.SetConsoleMode(stdin, mode) } From 377d7af2c1b73ee003ecd168a20585d0174cdfc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 22:47:23 +0800 Subject: [PATCH 8/9] add RestoreInputMode --- global/terminal/quick_edit.go | 5 +++++ global/terminal/quick_edit_windows.go | 12 ++++++++++++ main.go | 1 + 3 files changed, 18 insertions(+) diff --git a/global/terminal/quick_edit.go b/global/terminal/quick_edit.go index afa6b4c..1ed25e4 100644 --- a/global/terminal/quick_edit.go +++ b/global/terminal/quick_edit.go @@ -2,6 +2,11 @@ package terminal +// RestoreInputMode 还原输入模式,非Windows系统永远返回nil +func RestoreInputMode() error { + return nil +} + // DisableQuickEdit 禁用快速编辑,非Windows系统永远返回nil func DisableQuickEdit() error { return nil diff --git a/global/terminal/quick_edit_windows.go b/global/terminal/quick_edit_windows.go index 3a1dfaa..dbcd9ff 100644 --- a/global/terminal/quick_edit_windows.go +++ b/global/terminal/quick_edit_windows.go @@ -6,6 +6,17 @@ import ( "golang.org/x/sys/windows" ) +var inputmode uint32 + +// RestoreInputMode 还原输入模式 +func RestoreInputMode() error { + if inputmode == 0 { + return nil + } + stdin := windows.Handle(os.Stdin.Fd()) + return windows.SetConsoleMode(stdin, mode) +} + // DisableQuickEdit 禁用快速编辑 func DisableQuickEdit() error { stdin := windows.Handle(os.Stdin.Fd()) @@ -15,6 +26,7 @@ func DisableQuickEdit() error { if err != nil { return err } + inputmode = mode mode &^= windows.ENABLE_QUICK_EDIT_MODE // 禁用快速编辑模式 mode |= windows.ENABLE_EXTENDED_FLAGS // 启用扩展标志 diff --git a/main.go b/main.go index 1c77d8c..90f5b20 100644 --- a/main.go +++ b/main.go @@ -18,4 +18,5 @@ func main() { _ = terminal.DisableQuickEdit() _ = terminal.EnableVT100() gocq.Main() + _ = terminal.RestoreInputMode() } From 07b1e6b72e62f65be7a1f19d850ab50fdbf59bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 28 Feb 2023 22:50:23 +0800 Subject: [PATCH 9/9] fix RestoreInputMode --- global/terminal/quick_edit_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global/terminal/quick_edit_windows.go b/global/terminal/quick_edit_windows.go index dbcd9ff..6e6bd95 100644 --- a/global/terminal/quick_edit_windows.go +++ b/global/terminal/quick_edit_windows.go @@ -14,7 +14,7 @@ func RestoreInputMode() error { return nil } stdin := windows.Handle(os.Stdin.Fd()) - return windows.SetConsoleMode(stdin, mode) + return windows.SetConsoleMode(stdin, inputmode) } // DisableQuickEdit 禁用快速编辑