Debian Codegraph @

internal/term/term_windows.go

raw symbols SCIP
1 // Copyright 2022 The age Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package term
6
7 import (
8 "errors"
9 "os"
10 "syscall"
11
12 "golang.org/x/sys/windows"
13 )
14
15 func init() {
16 enableVirtualTerminalProcessing = func(out *os.File) error {
17 // Some instances of the Windows Console (e.g., cmd.exe and Windows PowerShell)
18 // do not have the virtual terminal processing enabled, which is necessary to
19 // make terminal escape sequences work. For this reason the clearLine function
20 // may not properly work. Here we enable the virtual terminal processing, if
21 // possible.
22 //
23 // See https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences.
24
25 const (
26 ENABLE_PROCESSED_OUTPUT uint32 = 0x1
27 ENABLE_VIRTUAL_TERMINAL_PROCESSING uint32 = 0x4
28 )
29
30 kernel32DLL := windows.NewLazySystemDLL("Kernel32.dll")
31 setConsoleMode := kernel32DLL.NewProc("SetConsoleMode")
32
33 var mode uint32
34 if err := syscall.GetConsoleMode(syscall.Handle(out.Fd()), &mode); err != nil {
35 return err
36 }
37
38 mode |= ENABLE_PROCESSED_OUTPUT
39 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING
40
41 // If the SetConsoleMode function fails, the return value is zero.
42 // See https://learn.microsoft.com/en-us/windows/console/setconsolemode#return-value.
43 if ret, _, _ := setConsoleMode.Call(out.Fd(), uintptr(mode)); ret == 0 {
44 return errors.New("SetConsoleMode failed")
45 }
46 return nil
47 }
48 }