Skip to content

Commit ec0eb47

Browse files
authored
Merge pull request #7 from Daniel-Ric/feature/2026-02-03/fix-redeclared-identifiers-in-terminal-code-0y75sm
Use x/sys/windows for console modes and handle invalid Windows console handles
2 parents 9093988 + 21140f2 commit ec0eb47

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

internal/cli/terminal_windows.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
package cli
44

55
import (
6+
"errors"
7+
68
"golang.org/x/sys/windows"
79
)
810

911
type terminalState struct {
10-
mode uint32
12+
handle windows.Handle
13+
mode uint32
1114
}
1215

1316
func makeRaw(fd int) (*terminalState, error) {
14-
handle := windows.Handle(fd)
17+
handle, err := windows.GetStdHandle(windows.STD_INPUT_HANDLE)
18+
if err != nil {
19+
return nil, err
20+
}
1521
var original uint32
1622
if err := windows.GetConsoleMode(handle, &original); err != nil {
23+
if errors.Is(err, windows.ERROR_INVALID_HANDLE) {
24+
return nil, nil
25+
}
1726
return nil, err
1827
}
1928

@@ -27,13 +36,12 @@ func makeRaw(fd int) (*terminalState, error) {
2736
return nil, err
2837
}
2938

30-
return &terminalState{mode: original}, nil
39+
return &terminalState{handle: handle, mode: original}, nil
3140
}
3241

3342
func restore(fd int, state *terminalState) {
3443
if state == nil {
3544
return
3645
}
37-
handle := windows.Handle(fd)
38-
_ = windows.SetConsoleMode(handle, state.mode)
46+
_ = windows.SetConsoleMode(state.handle, state.mode)
3947
}

0 commit comments

Comments
 (0)