Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
f38353d6
Commit
f38353d6
authored
May 22, 2024
by
Michael Yang
Browse files
stdin.fd
parent
201d853f
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
20 additions
and
21 deletions
+20
-21
readline/readline.go
readline/readline.go
+4
-5
readline/readline_unix.go
readline/readline_unix.go
+1
-1
readline/readline_windows.go
readline/readline_windows.go
+1
-1
readline/term.go
readline/term.go
+3
-3
readline/term_bsd.go
readline/term_bsd.go
+4
-4
readline/term_linux.go
readline/term_linux.go
+4
-4
readline/term_windows.go
readline/term_windows.go
+3
-3
No files found.
readline/readline.go
View file @
f38353d6
...
@@ -5,7 +5,6 @@ import (
...
@@ -5,7 +5,6 @@ import (
"fmt"
"fmt"
"io"
"io"
"os"
"os"
"syscall"
)
)
type
Prompt
struct
{
type
Prompt
struct
{
...
@@ -63,7 +62,7 @@ func New(prompt Prompt) (*Instance, error) {
...
@@ -63,7 +62,7 @@ func New(prompt Prompt) (*Instance, error) {
func
(
i
*
Instance
)
Readline
()
(
string
,
error
)
{
func
(
i
*
Instance
)
Readline
()
(
string
,
error
)
{
if
!
i
.
Terminal
.
rawmode
{
if
!
i
.
Terminal
.
rawmode
{
fd
:=
s
yscall
.
Stdin
fd
:=
o
s
.
Stdin
.
Fd
()
termios
,
err
:=
SetRawMode
(
fd
)
termios
,
err
:=
SetRawMode
(
fd
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
...
@@ -80,7 +79,7 @@ func (i *Instance) Readline() (string, error) {
...
@@ -80,7 +79,7 @@ func (i *Instance) Readline() (string, error) {
fmt
.
Print
(
prompt
)
fmt
.
Print
(
prompt
)
defer
func
()
{
defer
func
()
{
fd
:=
s
yscall
.
Stdin
fd
:=
o
s
.
Stdin
.
Fd
()
//nolint:errcheck
//nolint:errcheck
UnsetRawMode
(
fd
,
i
.
Terminal
.
termios
)
UnsetRawMode
(
fd
,
i
.
Terminal
.
termios
)
i
.
Terminal
.
rawmode
=
false
i
.
Terminal
.
rawmode
=
false
...
@@ -216,7 +215,7 @@ func (i *Instance) Readline() (string, error) {
...
@@ -216,7 +215,7 @@ func (i *Instance) Readline() (string, error) {
case
CharCtrlW
:
case
CharCtrlW
:
buf
.
DeleteWord
()
buf
.
DeleteWord
()
case
CharCtrlZ
:
case
CharCtrlZ
:
fd
:=
s
yscall
.
Stdin
fd
:=
o
s
.
Stdin
.
Fd
()
return
handleCharCtrlZ
(
fd
,
i
.
Terminal
.
termios
)
return
handleCharCtrlZ
(
fd
,
i
.
Terminal
.
termios
)
case
CharEnter
,
CharCtrlJ
:
case
CharEnter
,
CharCtrlJ
:
output
:=
buf
.
String
()
output
:=
buf
.
String
()
...
@@ -248,7 +247,7 @@ func (i *Instance) HistoryDisable() {
...
@@ -248,7 +247,7 @@ func (i *Instance) HistoryDisable() {
}
}
func
NewTerminal
()
(
*
Terminal
,
error
)
{
func
NewTerminal
()
(
*
Terminal
,
error
)
{
fd
:=
s
yscall
.
Stdin
fd
:=
o
s
.
Stdin
.
Fd
()
termios
,
err
:=
SetRawMode
(
fd
)
termios
,
err
:=
SetRawMode
(
fd
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
...
readline/readline_unix.go
View file @
f38353d6
...
@@ -6,7 +6,7 @@ import (
...
@@ -6,7 +6,7 @@ import (
"syscall"
"syscall"
)
)
func
handleCharCtrlZ
(
fd
int
,
termios
any
)
(
string
,
error
)
{
func
handleCharCtrlZ
(
fd
u
int
ptr
,
termios
any
)
(
string
,
error
)
{
t
:=
termios
.
(
*
Termios
)
t
:=
termios
.
(
*
Termios
)
if
err
:=
UnsetRawMode
(
fd
,
t
);
err
!=
nil
{
if
err
:=
UnsetRawMode
(
fd
,
t
);
err
!=
nil
{
return
""
,
err
return
""
,
err
...
...
readline/readline_windows.go
View file @
f38353d6
package
readline
package
readline
func
handleCharCtrlZ
(
fd
int
,
state
any
)
(
string
,
error
)
{
func
handleCharCtrlZ
(
fd
u
int
ptr
,
state
any
)
(
string
,
error
)
{
// not supported
// not supported
return
""
,
nil
return
""
,
nil
}
}
readline/term.go
View file @
f38353d6
...
@@ -8,7 +8,7 @@ import (
...
@@ -8,7 +8,7 @@ import (
type
Termios
syscall
.
Termios
type
Termios
syscall
.
Termios
func
SetRawMode
(
fd
int
)
(
*
Termios
,
error
)
{
func
SetRawMode
(
fd
u
int
ptr
)
(
*
Termios
,
error
)
{
termios
,
err
:=
getTermios
(
fd
)
termios
,
err
:=
getTermios
(
fd
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -25,13 +25,13 @@ func SetRawMode(fd int) (*Termios, error) {
...
@@ -25,13 +25,13 @@ func SetRawMode(fd int) (*Termios, error) {
return
termios
,
setTermios
(
fd
,
&
newTermios
)
return
termios
,
setTermios
(
fd
,
&
newTermios
)
}
}
func
UnsetRawMode
(
fd
int
,
termios
any
)
error
{
func
UnsetRawMode
(
fd
u
int
ptr
,
termios
any
)
error
{
t
:=
termios
.
(
*
Termios
)
t
:=
termios
.
(
*
Termios
)
return
setTermios
(
fd
,
t
)
return
setTermios
(
fd
,
t
)
}
}
// IsTerminal returns true if the given file descriptor is a terminal.
// IsTerminal returns true if the given file descriptor is a terminal.
func
IsTerminal
(
fd
int
)
bool
{
func
IsTerminal
(
fd
u
int
ptr
)
bool
{
_
,
err
:=
getTermios
(
fd
)
_
,
err
:=
getTermios
(
fd
)
return
err
==
nil
return
err
==
nil
}
}
readline/term_bsd.go
View file @
f38353d6
...
@@ -7,17 +7,17 @@ import (
...
@@ -7,17 +7,17 @@ import (
"unsafe"
"unsafe"
)
)
func
getTermios
(
fd
int
)
(
*
Termios
,
error
)
{
func
getTermios
(
fd
u
int
ptr
)
(
*
Termios
,
error
)
{
termios
:=
new
(
Termios
)
termios
:=
new
(
Termios
)
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
uintptr
(
fd
)
,
syscall
.
TIOCGETA
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
fd
,
syscall
.
TIOCGETA
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
if
err
!=
0
{
if
err
!=
0
{
return
nil
,
err
return
nil
,
err
}
}
return
termios
,
nil
return
termios
,
nil
}
}
func
setTermios
(
fd
int
,
termios
*
Termios
)
error
{
func
setTermios
(
fd
u
int
ptr
,
termios
*
Termios
)
error
{
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
uintptr
(
fd
)
,
syscall
.
TIOCSETA
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
fd
,
syscall
.
TIOCSETA
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
if
err
!=
0
{
if
err
!=
0
{
return
err
return
err
}
}
...
...
readline/term_linux.go
View file @
f38353d6
...
@@ -10,17 +10,17 @@ import (
...
@@ -10,17 +10,17 @@ import (
const
tcgets
=
0x5401
const
tcgets
=
0x5401
const
tcsets
=
0x5402
const
tcsets
=
0x5402
func
getTermios
(
fd
int
)
(
*
Termios
,
error
)
{
func
getTermios
(
fd
u
int
ptr
)
(
*
Termios
,
error
)
{
termios
:=
new
(
Termios
)
termios
:=
new
(
Termios
)
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
uintptr
(
fd
)
,
tcgets
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
fd
,
tcgets
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
if
err
!=
0
{
if
err
!=
0
{
return
nil
,
err
return
nil
,
err
}
}
return
termios
,
nil
return
termios
,
nil
}
}
func
setTermios
(
fd
int
,
termios
*
Termios
)
error
{
func
setTermios
(
fd
u
int
ptr
,
termios
*
Termios
)
error
{
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
uintptr
(
fd
)
,
tcsets
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
_
,
_
,
err
:=
syscall
.
Syscall6
(
syscall
.
SYS_IOCTL
,
fd
,
tcsets
,
uintptr
(
unsafe
.
Pointer
(
termios
)),
0
,
0
,
0
)
if
err
!=
0
{
if
err
!=
0
{
return
err
return
err
}
}
...
...
readline/term_windows.go
View file @
f38353d6
...
@@ -9,13 +9,13 @@ type State struct {
...
@@ -9,13 +9,13 @@ type State struct {
}
}
// IsTerminal checks if the given file descriptor is associated with a terminal
// IsTerminal checks if the given file descriptor is associated with a terminal
func
IsTerminal
(
fd
int
)
bool
{
func
IsTerminal
(
fd
u
int
ptr
)
bool
{
var
st
uint32
var
st
uint32
err
:=
windows
.
GetConsoleMode
(
windows
.
Handle
(
fd
),
&
st
)
err
:=
windows
.
GetConsoleMode
(
windows
.
Handle
(
fd
),
&
st
)
return
err
==
nil
return
err
==
nil
}
}
func
SetRawMode
(
fd
int
)
(
*
State
,
error
)
{
func
SetRawMode
(
fd
u
int
ptr
)
(
*
State
,
error
)
{
var
st
uint32
var
st
uint32
if
err
:=
windows
.
GetConsoleMode
(
windows
.
Handle
(
fd
),
&
st
);
err
!=
nil
{
if
err
:=
windows
.
GetConsoleMode
(
windows
.
Handle
(
fd
),
&
st
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -32,7 +32,7 @@ func SetRawMode(fd int) (*State, error) {
...
@@ -32,7 +32,7 @@ func SetRawMode(fd int) (*State, error) {
return
&
State
{
st
},
nil
return
&
State
{
st
},
nil
}
}
func
UnsetRawMode
(
fd
int
,
state
any
)
error
{
func
UnsetRawMode
(
fd
u
int
ptr
,
state
any
)
error
{
s
:=
state
.
(
*
State
)
s
:=
state
.
(
*
State
)
return
windows
.
SetConsoleMode
(
windows
.
Handle
(
fd
),
s
.
mode
)
return
windows
.
SetConsoleMode
(
windows
.
Handle
(
fd
),
s
.
mode
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment