okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

Ayman Bagabasfix: lint issuesf678006

upstream
2.1 KiB77 linesraw
1//go:build windows
2// +build windows
3
4package uv
5
6import (
7	"fmt"
8
9	"github.com/charmbracelet/x/term"
10	"golang.org/x/sys/windows"
11)
12
13func makeRaw(inTty, outTty term.File) (inTtyState, outTtyState *term.State, err error) {
14	if inTty == nil || outTty == nil || !term.IsTerminal(inTty.Fd()) || !term.IsTerminal(outTty.Fd()) {
15		return nil, nil, ErrNotTerminal
16	}
17
18	// Save stdin state and enable VT inpu
19	// We also need to enable VT input here.
20	inTtyState, err = term.MakeRaw(inTty.Fd())
21	if err != nil {
22		return nil, nil, fmt.Errorf("error making terminal raw: %w", err)
23	}
24
25	// Enable VT input
26	var imode uint32
27	if err := windows.GetConsoleMode(windows.Handle(inTty.Fd()), &imode); err != nil {
28		return nil, nil, fmt.Errorf("error getting console mode: %w", err)
29	}
30
31	if err := windows.SetConsoleMode(windows.Handle(inTty.Fd()), imode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {
32		return nil, nil, fmt.Errorf("error setting console mode: %w", err)
33	}
34
35	// Save output screen buffer state and enable VT processing.
36	outTtyState, err = term.GetState(outTty.Fd())
37	if err != nil {
38		return nil, nil, fmt.Errorf("error getting terminal state: %w", err)
39	}
40
41	var omode uint32
42	if err := windows.GetConsoleMode(windows.Handle(outTty.Fd()), &omode); err != nil {
43		return nil, nil, fmt.Errorf("error getting console mode: %w", err)
44	}
45
46	if err := windows.SetConsoleMode(windows.Handle(outTty.Fd()),
47		omode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING|
48			windows.DISABLE_NEWLINE_AUTO_RETURN); err != nil {
49		return nil, nil, fmt.Errorf("error setting console mode: %w", err)
50	}
51
52	return inTtyState, outTtyState, nil
53}
54
55func getSize(_, outTty term.File) (w, h int, err error) {
56	if outTty != nil {
57		return term.GetSize(outTty.Fd()) //nolint:wrapcheck
58	}
59	return 0, 0, ErrNotTerminal
60}
61
62func getWinsize(inTty, outTty term.File) (ws Winsize, err error) {
63	w, h, err := getSize(inTty, outTty)
64	return Winsize{Col: uint16(w), Row: uint16(h)}, err
65}
66
67func optimizeMovements(*term.State) (useTabs, useBspace bool) {
68	return supportsBackspace(0), supportsHardTabs(0)
69}
70
71func supportsBackspace(uint64) bool {
72	return true
73}
74
75func supportsHardTabs(uint64) bool {
76	return true
77}