ollama

Форк
0
/
term_windows.go 
63 строки · 1.8 Кб
1
package readline
2

3
import (
4
	"syscall"
5
	"unsafe"
6
)
7

8
const (
9
	enableLineInput       = 2
10
	enableWindowInput     = 8
11
	enableMouseInput      = 16
12
	enableInsertMode      = 32
13
	enableQuickEditMode   = 64
14
	enableExtendedFlags   = 128
15
	enableProcessedOutput = 1
16
	enableWrapAtEolOutput = 2
17
	enableAutoPosition    = 256 // Cursor position is not affected by writing data to the console.
18
	enableEchoInput       = 4   // Characters are written to the console as they're read.
19
	enableProcessedInput  = 1   // Enables input processing (like recognizing Ctrl+C).
20
)
21

22
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
23

24
var (
25
	procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
26
	procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
27
)
28

29
type State struct {
30
	mode uint32
31
}
32

33
// IsTerminal checks if the given file descriptor is associated with a terminal
34
func IsTerminal(fd int) bool {
35
	var st uint32
36
	r, _, e := syscall.SyscallN(procGetConsoleMode.Addr(), uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
37
	// if the call succeeds and doesn't produce an error, it's a terminal
38
	return r != 0 && e == 0
39
}
40

41
func SetRawMode(fd int) (*State, error) {
42
	var st uint32
43
	// retrieve the current mode of the terminal
44
	_, _, e := syscall.SyscallN(procGetConsoleMode.Addr(), uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
45
	if e != 0 {
46
		return nil, error(e)
47
	}
48
	// modify the mode to set it to raw
49
	raw := st &^ (enableEchoInput | enableProcessedInput | enableLineInput | enableProcessedOutput)
50
	// apply the new mode to the terminal
51
	_, _, e = syscall.SyscallN(procSetConsoleMode.Addr(), uintptr(fd), uintptr(raw), 0)
52
	if e != 0 {
53
		return nil, error(e)
54
	}
55
	// return the original state so that it can be restored later
56
	return &State{st}, nil
57
}
58

59
func UnsetRawMode(fd int, state any) error {
60
	s := state.(*State)
61
	_, _, err := syscall.SyscallN(procSetConsoleMode.Addr(), uintptr(fd), uintptr(s.mode), 0)
62
	return err
63
}
64

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.