Golang fmt.Print("\033c") 和 fmt.Print("\x1bc") 不清除屏幕(ANSI 转义码不起作用)如何修复?
Golang fmt.Print("\033c") and fmt.Print("\x1bc") are not clearing screen(ANSI Escape code not working) how to fix?
每当我 运行 fmt.Print("3c")
在继续 Windows cmd 时,它不会清除屏幕但打印 c
每当我在 [=38] 中执行 console.log("3c")
=] 它工作正常。
根据以下链接,这应该有效:
https://en.wikipedia.org/wiki/ANSI_escape_code#Windows
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#samples
我已经尝试将它记录在 javascript 中并且它工作正常我还尝试在打印 c
的末尾添加一个 \n 字符并将其更改为 \x1bc 也刚刚打印:c
以下所有打印:c
on windows cmd
func main() {
fmt.Print("3c")
fmt.Print("3c\n")
fmt.Print("\x1bc")
fmt.Print("\x1bc\n")
fmt.Println("3c")
fmt.Println("\x1bc")
}
我预计它会像 javascript 那样清除屏幕,但由于某些原因它没有
澄清一下,我说的是 ANSI escape code 不工作。
问题是 windows 需要 ENABLE_VIRTUAL_TERMINAL_PROCESSING(0x0004) 控制台标志 我通过使用以下代码将标志添加到控制台来修复它:
package main
// #include "Windows.h"
import "C"
import "fmt"
func main() {
// Needed for getting handle and getting current console mode
var mode C.DWORD
handle := C.GetStdHandle(C.STD_OUTPUT_HANDLE)
C.GetConsoleMode(handle, &mode)
// Enable Virtual Terminal Processing by adding the flag to the current mode
C.SetConsoleMode(handle, mode|0x0004)
fmt.Print("3c")
}
===编辑===
就像评论中提到的 Matmarbon // #include "Windows.h"
行是必需的,你需要安装 gcc
每当我 运行 fmt.Print("3c")
在继续 Windows cmd 时,它不会清除屏幕但打印 c
每当我在 [=38] 中执行 console.log("3c")
=] 它工作正常。
根据以下链接,这应该有效: https://en.wikipedia.org/wiki/ANSI_escape_code#Windows
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#samples
我已经尝试将它记录在 javascript 中并且它工作正常我还尝试在打印 c
的末尾添加一个 \n 字符并将其更改为 \x1bc 也刚刚打印:c
以下所有打印:c
on windows cmd
func main() {
fmt.Print("3c")
fmt.Print("3c\n")
fmt.Print("\x1bc")
fmt.Print("\x1bc\n")
fmt.Println("3c")
fmt.Println("\x1bc")
}
我预计它会像 javascript 那样清除屏幕,但由于某些原因它没有
澄清一下,我说的是 ANSI escape code 不工作。
问题是 windows 需要 ENABLE_VIRTUAL_TERMINAL_PROCESSING(0x0004) 控制台标志 我通过使用以下代码将标志添加到控制台来修复它:
package main
// #include "Windows.h"
import "C"
import "fmt"
func main() {
// Needed for getting handle and getting current console mode
var mode C.DWORD
handle := C.GetStdHandle(C.STD_OUTPUT_HANDLE)
C.GetConsoleMode(handle, &mode)
// Enable Virtual Terminal Processing by adding the flag to the current mode
C.SetConsoleMode(handle, mode|0x0004)
fmt.Print("3c")
}
===编辑===
就像评论中提到的 Matmarbon // #include "Windows.h"
行是必需的,你需要安装 gcc