本文整理匯總了Golang中syscall.GetConsoleMode函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetConsoleMode函數的具體用法?Golang GetConsoleMode怎麽用?Golang GetConsoleMode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetConsoleMode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getNativeConsole
// getNativeConsole returns the console modes ('state') for the native Windows console
func getNativeConsole() (State, error) {
var (
err error
state State
)
// Get the handle to stdout
if state.outHandle, err = syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE); err != nil {
return state, err
}
// Get the console mode from the consoles stdout handle
if err = syscall.GetConsoleMode(state.outHandle, &state.outMode); err != nil {
return state, err
}
// Get the handle to stdin
if state.inHandle, err = syscall.GetStdHandle(syscall.STD_INPUT_HANDLE); err != nil {
return state, err
}
// Get the console mode from the consoles stdin handle
if err = syscall.GetConsoleMode(state.inHandle, &state.inMode); err != nil {
return state, err
}
return state, nil
}
示例2: PromptForPassword
func (pr passwordReader) PromptForPassword(promptText string, args ...interface{}) (passwd string) {
hStdin := syscall.Handle(os.Stdin.Fd())
var originalMode uint32
err := syscall.GetConsoleMode(hStdin, &originalMode)
if err != nil {
return
}
var newMode uint32 = (originalMode &^ ENABLE_ECHO_INPUT)
err = setConsoleMode(hStdin, newMode)
defer setConsoleMode(hStdin, originalMode)
if err != nil {
return
}
// Display the prompt.
fmt.Printf(promptText+": ", args...)
defer fmt.Println("")
rd := bufio.NewReader(os.Stdin)
line, err := rd.ReadString('\r')
if err == nil {
return strings.TrimSpace(line)
}
return ""
}
示例3: NewTerminal
// NewTerminal creates a terminal and sets it to raw input mode.
func NewTerminal() (*Terminal, error) {
term := &Terminal{
In: os.Stdin,
Out: os.Stdout,
History: make([]string, 0, 10),
histIdx: -1,
terminal: new(terminal),
}
err := syscall.GetConsoleMode(syscall.Handle(term.In.Fd()), &term.origMode)
if err != nil {
return term, nil
}
mode := term.origMode
term.isTerm = true
// Set new mode flags.
mode &^= (echoInputFlag | insertModeFlag | lineInputFlag | mouseInputFlag |
processedInputFlag | windowInputFlag)
ret, _, err := setConsoleMode.Call(term.In.Fd(), uintptr(mode))
if ret == 0 {
return nil, err
}
return term, nil
}
示例4: newFile
// newFile returns a new File with the given file handle and name.
// Unlike NewFile, it does not check that h is syscall.InvalidHandle.
func newFile(h syscall.Handle, name string) *File {
f := &File{&file{fd: h, name: name}}
var m uint32
if syscall.GetConsoleMode(f.fd, &m) == nil {
f.isConsole = true
}
runtime.SetFinalizer(f.file, (*file).close)
return f
}
示例5: DisableFlags
// DisableFlags disables the terminal flags specified in the function
// argument 'flags', and returns the previous state of the terminal
// so that it can be restored.
func DisableFlags(fd FileDescriptor, flags Flag) (*State, error) {
var st uint32
err := syscall.GetConsoleMode(handle(fd), &st)
if err != nil {
return nil, err
}
st &^= uint32(flags)
err = win.SetConsoleMode(handle(fd), st)
return &State{st}, err
}
示例6: IsAtty
// Might not catch everything...
func IsAtty(fd uintptr) bool {
handle := syscall.Handle(fd)
var mode uint32
err := syscall.GetConsoleMode(handle, &mode)
if err != nil {
return false
}
return true
}
示例7: NewFile
// NewFile returns a new File with the given file descriptor and name.
func NewFile(fd uintptr, name string) *File {
h := syscall.Handle(fd)
if h == syscall.InvalidHandle {
return nil
}
var m uint32
if syscall.GetConsoleMode(h, &m) == nil {
return newConsoleFile(h, name)
}
return newFile(h, name)
}
示例8: NewFile
// NewFile returns a new File with the given file descriptor and name.
func NewFile(fd uintptr, name string) *File {
h := syscall.Handle(fd)
if h == syscall.InvalidHandle {
return nil
}
f := &File{&file{fd: h, name: name}}
var m uint32
if syscall.GetConsoleMode(f.fd, &m) == nil {
f.isConsole = true
}
runtime.SetFinalizer(f.file, (*file).close)
return f
}
示例9: main
func main() {
var mode uint32
err := syscall.GetConsoleMode(syscall.Stdin, &mode)
isConsole := err == nil
commandName := filepath.Base(os.Args[0])
if isConsole == true || commandName != names.Jujud {
runConsole()
} else {
runService()
}
}
示例10: useNativeConsole
// useNativeConsole determines if the docker client should use the built-in
// console which supports ANSI emulation, or fall-back to the golang emulator
// (github.com/azure/go-ansiterm).
func useNativeConsole() bool {
osv, err := system.GetOSVersion()
if err != nil {
return false
}
// Native console is not available major version 10
if osv.MajorVersion < 10 {
return false
}
// Must have a late pre-release TP4 build of Windows Server 2016/Windows 10 TH2 or later
if osv.Build < 10578 {
return false
}
// Environment variable override
if e := os.Getenv("USE_NATIVE_CONSOLE"); e != "" {
if e == "1" {
return true
}
return false
}
// Get the handle to stdout
stdOutHandle, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
if err != nil {
return false
}
// Get the console mode from the consoles stdout handle
var mode uint32
if err := syscall.GetConsoleMode(stdOutHandle, &mode); err != nil {
return false
}
// Legacy mode does not have native ANSI emulation.
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683167(v=vs.85).aspx
const enableVirtualTerminalProcessing = 0x0004
if mode&enableVirtualTerminalProcessing == 0 {
return false
}
// TODO Windows (Post TP4). The native emulator still has issues which
// mean it shouldn't be enabled for everyone. Change this next line to true
// to change the default to "enable if available". In the meantime, users
// can still try it out by using USE_NATIVE_CONSOLE env variable.
return false
}
示例11: makeRaw
func makeRaw(console syscall.Handle) (func(), error) {
// Get old mode so that we can recover later
var oldMode uint32
if err := syscall.GetConsoleMode(console, &oldMode); err != nil {
return nil, err
}
var newMode uint32 = uint32(int(oldMode) & ^ENABLE_ECHO_INPUT)
if err := setConsoleMode(console, newMode); err != nil {
return nil, err
}
return func() {
setConsoleMode(console, oldMode)
}, nil
}
示例12: getPassword
func getPassword() (password string, err error) {
var oldMode uint32
err = syscall.GetConsoleMode(syscall.Stdin, &oldMode)
if err != nil {
return
}
var newMode uint32 = (oldMode &^ ENABLE_ECHO_INPUT)
err = setConsoleMode(syscall.Stdin, newMode)
defer setConsoleMode(syscall.Stdin, oldMode)
if err != nil {
return
}
return readline()
}
示例13: read
func read(f *os.File) (string, error) {
handle := syscall.Handle(f.Fd())
// Grab the old console mode so we can reset it. We defer the reset
// right away because it doesn't matter (it is idempotent).
var oldMode uint32
if err := syscall.GetConsoleMode(handle, &oldMode); err != nil {
return "", err
}
defer setConsoleMode(handle, oldMode)
// The new mode is the old mode WITHOUT the echo input flag set.
var newMode uint32 = uint32(int(oldMode) & ^ENABLE_ECHO_INPUT)
if err := setConsoleMode(handle, newMode); err != nil {
return "", err
}
return readline(f)
}
示例14: AskForPassword
func (ui terminalUI) AskForPassword(prompt string, args ...interface{}) (passwd string) {
hStdin := syscall.Handle(os.Stdin.Fd())
var originalMode uint32
err := syscall.GetConsoleMode(hStdin, &originalMode)
if err != nil {
return
}
var newMode = uint32(originalMode &^ EnableEchoInput)
err = setConsoleMode(hStdin, newMode)
defer setConsoleMode(hStdin, originalMode)
defer ui.Say("")
if err != nil {
return
}
return ui.Ask(prompt, args...)
}
示例15: AskForPassword
func (ui terminalUI) AskForPassword(prompt string) (passwd string) {
hStdin := syscall.Handle(os.Stdin.Fd())
var originalMode uint32
err := syscall.GetConsoleMode(hStdin, &originalMode)
if err != nil {
return
}
var newMode uint32 = (originalMode &^ ENABLE_ECHO_INPUT)
err = setConsoleMode(hStdin, newMode)
defer setConsoleMode(hStdin, originalMode)
defer ui.Say("")
if err != nil {
return
}
return ui.Ask(prompt)
}