• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang terminal.GetState函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中golang.org/x/crypto/ssh/terminal.GetState函数的典型用法代码示例。如果您正苦于以下问题:Golang GetState函数的具体用法?Golang GetState怎么用?Golang GetState使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了GetState函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: catchNotifications

// Catch signals
func catchNotifications() {
	state, err := terminal.GetState(int(os.Stdin.Fd()))
	checkErr(err)

	// Deal with SIGINT
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt)

	var timer time.Time
	go func() {
		for sig := range sigChan {
			// Prevent exiting on accidental signal send
			if time.Now().Sub(timer) < time.Second*signalTimeout {
				terminal.Restore(int(os.Stdin.Fd()), state)
				os.Exit(0)
			}

			fmt.Fprintln(os.Stderr, "")
			fmt.Fprintln(os.Stderr, "")
			fmt.Fprintln(os.Stderr, "")
			fmt.Fprintln(os.Stderr, sig, "signal caught!")
			fmt.Fprintf(os.Stderr, "Send signal again within %v seconds to exit\n", signalTimeout)
			fmt.Fprintln(os.Stderr, "")
			fmt.Fprintln(os.Stderr, "")
			fmt.Fprintln(os.Stderr, "")

			timer = time.Now()
		}
	}()
}
开发者ID:joshuaprunier,项目名称:trite,代码行数:31,代码来源:common.go


示例2: getPassword

func getPassword() (string, error) {
	stdin := int(syscall.Stdin)
	initialTermState, err := terminal.GetState(stdin)
	if err != nil {
		return "", err
	}

	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, os.Kill)
	go func() {
		s := <-c
		terminal.Restore(stdin, initialTermState)
		switch sig := s.(type) {
		case syscall.Signal:
			if int(sig) == 2 {
				fmt.Println("^C")
			}
		}
		os.Exit(1)
	}()

	passBytes, err := terminal.ReadPassword(stdin)
	if err != nil {
		return "", err
	}

	signal.Stop(c)
	fmt.Print("\n")
	return string(passBytes), nil
}
开发者ID:github,项目名称:hub,代码行数:30,代码来源:config.go


示例3: getPassword

func getPassword() ([]byte, error) {
	signals := make(chan os.Signal, 1)
	passwords := make(chan password)
	signal.Notify(signals, os.Interrupt, os.Kill)
	defer signal.Stop(signals)
	state, err := terminal.GetState(2)
	if err != nil {
		return nil, err
	}
	defer terminal.Restore(2, state)
	go func() {
		fmt.Fprintf(os.Stderr, "Password: ")
		defer fmt.Fprintf(os.Stderr, "\n")
		p, err := terminal.ReadPassword(2)
		passwords <- password{
			Password: p,
			Err:      err,
		}
		close(passwords)
	}()
	select {
	case <-signals:
		return nil, fmt.Errorf("Password entry cancelled")
	case password := <-passwords:
		return password.Password, password.Err
	}
}
开发者ID:zennro,项目名称:oyster,代码行数:27,代码来源:main.go


示例4: runAttached

func runAttached(c *cli.Context, app, ps, args, release string) (int, error) {
	fd := os.Stdin.Fd()

	var w, h int

	if terminal.IsTerminal(int(fd)) {
		stdinState, err := terminal.GetState(int(fd))
		if err != nil {
			return -1, err
		}

		defer terminal.Restore(int(fd), stdinState)

		w, h, err = terminal.GetSize(int(fd))
		if err != nil {
			return -1, err
		}
	}

	code, err := rackClient(c).RunProcessAttached(app, ps, args, release, h, w, os.Stdin, os.Stdout)
	if err != nil {
		return -1, err
	}

	return code, nil
}
开发者ID:convox,项目名称:rack,代码行数:26,代码来源:run.go


示例5: GetState

func GetState(fd int) (*State, error) {
	state, err := terminal.GetState(fd)
	if err != nil {
		return nil, err
	}

	currentState := State(*state)
	return &currentState, nil
}
开发者ID:ccsblueboy,项目名称:lxd,代码行数:9,代码来源:termios_windows.go


示例6: init

func init() {
	if runtime.GOOS != "windows" {
		terminalState, _ := terminal.GetState(0)
		sigint := make(chan os.Signal, 1)
		signal.Notify(sigint, os.Interrupt)
		go func() {
			<-sigint
			terminal.Restore(0, terminalState)
			fmt.Println("")
			os.Exit(1)
		}()
	}
}
开发者ID:freman,项目名称:hipclean,代码行数:13,代码来源:util.go


示例7: Signals

func Signals() {
	prev, err := terminal.GetState(int(os.Stdin.Fd()))
	if err != nil {
		prev = nil
	}

	s := make(chan os.Signal, 1)
	signal.Notify(s, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
	for _ = range s {
		terminal.Restore(int(os.Stdin.Fd()), prev)
		os.Exit(1)
	}
}
开发者ID:starkandwayne,项目名称:safe,代码行数:13,代码来源:sig.go


示例8: GetPass

/* Read non-empty password from terminal */
func GetPass(prompt string) (pass string, err error) {
	var resp []byte

	fd, err := getTerminalFd()
	if err != nil {
		return
	}

	/* Store current terminal state in case the call gets interrupted by signal */
	oldState, err := terminal.GetState(fd)
	if err != nil {
		err = errors.Errorf("failed to get terminal state: %s\n", err)
		return
	}

	/*
	 * Install signal handler
	 * Unlike the ReadLine function, using a raw terminal state here does not help.
	 * If the prompt gets killed by a signal, the terminal state is not restored.
	 * Hence restore it in a signal handler
	 */
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGINT)
	go func() {
		<-c
		terminal.Restore(fd, oldState)
		fmt.Fprintln(os.Stderr, "aborting")
		os.Exit(1)
	}()

	for i := 0; len(resp) == 0; i++ {
		if i > 0 {
			fmt.Printf("\rInvalid response - try again")
			time.Sleep(500 * time.Millisecond)
		}
		/* Clear line - see https://en.wikipedia.org/wiki/ANSI_escape_code */
		fmt.Printf("\r\033[2K%s: ", prompt)

		/* This function internally takes care of restoring terminal state. */
		resp, err = terminal.ReadPassword(fd)
		if err != nil {
			return
		}
		resp = bytes.TrimSpace(resp)
	}
	/* Restore signal handling */
	signal.Stop(c)
	signal.Reset(syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGINT)
	return string(resp), nil
}
开发者ID:grrtrr,项目名称:clcv2,代码行数:51,代码来源:terminal.go


示例9: restoreTerminal

// restoreTerminal installs a cleanup handler that restores the previous
// terminal state on exit.
func restoreTerminal() {
	if !stdoutIsTerminal() {
		return
	}

	fd := int(os.Stdout.Fd())
	state, err := terminal.GetState(fd)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unable to get terminal state: %v\n", err)
		return
	}

	AddCleanupHandler(func() error {
		err := checkErrno(terminal.Restore(fd, state))
		if err != nil {
			fmt.Fprintf(os.Stderr, "unable to get restore terminal state: %#+v\n", err)
		}
		return err
	})
}
开发者ID:restic,项目名称:restic,代码行数:22,代码来源:global.go


示例10: Init

func (r *LightRenderer) Init() {
	delay := 100
	delayEnv := os.Getenv("ESCDELAY")
	if len(delayEnv) > 0 {
		num, err := strconv.Atoi(delayEnv)
		if err == nil && num >= 0 {
			delay = num
		}
	}
	r.escDelay = delay

	fd := r.fd()
	origState, err := terminal.GetState(fd)
	if err != nil {
		errorExit(err.Error())
	}
	r.origState = origState
	terminal.MakeRaw(fd)
	r.updateTerminalSize()
	initTheme(r.theme, r.defaultTheme(), r.forceBlack)

	_, x := r.findOffset()
	if x > 0 {
		r.upOneLine = true
		r.stderr("\n")
	}
	for i := 1; i < r.MaxY(); i++ {
		r.stderr("\n")
		r.csi("G")
	}

	if r.mouse {
		r.csi("?1000h")
	}
	r.csi(fmt.Sprintf("%dA", r.MaxY()-1))
	r.csi("G")
	// r.csi("s")
	if r.mouse {
		r.yoffset, _ = r.findOffset()
	}
}
开发者ID:junegunn,项目名称:fzf,代码行数:41,代码来源:light.go


示例11: sshWithRestore

func sshWithRestore(c *cli.Context, id, cmd string) (int, error) {
	fd := os.Stdin.Fd()
	isTerm := terminal.IsTerminal(int(fd))
	var h, w int

	if isTerm {
		stdinState, err := terminal.GetState(int(fd))
		if err != nil {
			return -1, err
		}

		h, w, err = terminal.GetSize(int(fd))
		if err != nil {
			return -1, err
		}

		defer terminal.Restore(int(fd), stdinState)
	}

	return rackClient(c).SSHInstance(id, cmd, h, w, isTerm, os.Stdin, os.Stdout)
}
开发者ID:gmelika,项目名称:rack,代码行数:21,代码来源:instances.go


示例12: readPass

func readPass(fd int) ([]byte, error) {
	// make sure an interrupt won't break the terminal
	sigint := make(chan os.Signal)
	state, err := terminal.GetState(fd)
	if err != nil {
		return nil, err
	}
	go func() {
		for _ = range sigint {
			terminal.Restore(fd, state)
			fmt.Println("^C")
			os.Exit(1)
		}
	}()
	signal.Notify(sigint, os.Interrupt)
	defer func() {
		signal.Stop(sigint)
		close(sigint)
	}()
	return terminal.ReadPassword(fd)
}
开发者ID:lunixbochs,项目名称:yoink,代码行数:21,代码来源:logging.go


示例13: catchNotifications

// Catch signals
func catchNotifications() {
	ignoreStdin := false
	state, err := terminal.GetState(int(os.Stdin.Fd()))
	if err != nil {
		// Stdin may be redirected, then just exit
		ignoreStdin = true
	}

	// Deal with SIGINT
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt)

	var timer time.Time
	go func() {
		for sig := range sigChan {
			if ignoreStdin {
				fmt.Fprintln(os.Stderr, sig, "signal caught!")
				fmt.Fprintln(os.Stderr, "Exiting")
				os.Exit(0)
			} else {
				// Prevent exiting on accidental signal send
				if time.Now().Sub(timer) < time.Second*signalTimeout {
					terminal.Restore(int(os.Stdin.Fd()), state)
					os.Exit(0)
				}

				fmt.Fprintln(os.Stderr, "")
				fmt.Fprintln(os.Stderr, "")
				fmt.Fprintln(os.Stderr, "")
				fmt.Fprintln(os.Stderr, sig, "signal caught!")
				fmt.Fprintf(os.Stderr, "Send signal again within %v seconds to exit\n", signalTimeout)
				fmt.Fprintln(os.Stderr, "")
				fmt.Fprintln(os.Stderr, "")
				fmt.Fprintln(os.Stderr, "")

				timer = time.Now()
			}
		}
	}()
}
开发者ID:joshuaprunier,项目名称:mytablecopy,代码行数:41,代码来源:mytablecopy.go


示例14: startTerminal

func startTerminal(c *config) {
	fmt.Printf("Starting terminal mode.\n")
	fmt.Printf("Enter h for [h]elp.\n")
	fmt.Printf("Enter q for [q]uit.\n")
	done := make(chan bool)
	initState, err := terminal.GetState(0)
	protected := false
	if err != nil {
		fmt.Printf("error getting terminal state: %v\n", err.Error())
		return
	}

	go func() {
		terminal.MakeRaw(int(os.Stdin.Fd()))
		n := terminal.NewTerminal(os.Stdin, "> ")
		for {
			var ln string
			var err error
			if !protected {
				ln, err = n.ReadLine()
				if err != nil {
					done <- true
				}
			} else {
				ln, err = n.ReadPassword(">*")
				if err != nil {
					done <- true
				}
			}
			execute(done, &protected, c, ln)
		}
	}()
	select {
	case <-done:
		fmt.Printf("exiting...\n")
		terminal.Restore(0, initState)
		close(done)
	}
}
开发者ID:ironbits,项目名称:dcrd,代码行数:39,代码来源:terminal.go


示例15: getpass

func getpass(prompt string) (pass string, err error) {

	tstate, err := terminal.GetState(0)
	if err != nil {
		return
	}

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		quit := false
		for _ = range sig {
			quit = true
			break
		}
		terminal.Restore(0, tstate)
		if quit {
			fmt.Println()
			os.Exit(2)
		}
	}()
	defer func() {
		signal.Stop(sig)
		close(sig)
	}()

	f := bufio.NewWriter(os.Stdout)
	f.Write([]byte(prompt))
	f.Flush()

	passbytes, err := terminal.ReadPassword(0)
	pass = string(passbytes)

	f.Write([]byte("\n"))
	f.Flush()

	return
}
开发者ID:rapidloop,项目名称:rtop-bot,代码行数:38,代码来源:sshhelper.go


示例16: cmdExec

func cmdExec(c *cli.Context) error {
	fd := os.Stdin.Fd()

	var h, w int

	if terminal.IsTerminal(int(fd)) {
		stdinState, err := terminal.GetState(int(fd))
		if err != nil {
			return stdcli.Error(err)
		}

		defer terminal.Restore(int(fd), stdinState)

		w, h, err = terminal.GetSize(int(fd))
		if err != nil {
			return stdcli.Error(err)
		}
	}

	_, app, err := stdcli.DirApp(c, ".")
	if err != nil {
		return stdcli.Error(err)
	}

	if len(c.Args()) < 2 {
		stdcli.Usage(c, "exec")
		return nil
	}

	ps := c.Args()[0]

	code, err := rackClient(c).ExecProcessAttached(app, ps, strings.Join(c.Args()[1:], " "), os.Stdin, os.Stdout, h, w)
	if err != nil {
		return stdcli.Error(err)
	}

	return stdcli.Errorf("", code)
}
开发者ID:convox,项目名称:rack,代码行数:38,代码来源:exec.go


示例17: cmdExec

func cmdExec(c *cli.Context) {
	fd := os.Stdin.Fd()
	stdinState, err := terminal.GetState(int(fd))
	defer terminal.Restore(int(fd), stdinState)

	_, app, err := stdcli.DirApp(c, ".")

	if err != nil {
		stdcli.Error(err)
		return
	}

	if len(c.Args()) < 2 {
		stdcli.Usage(c, "exec")
		return
	}

	w, h, err := terminal.GetSize(int(fd))

	if err != nil {
		stdcli.Error(err)
		return
	}

	ps := c.Args()[0]

	code, err := rackClient(c).ExecProcessAttached(app, ps, strings.Join(c.Args()[1:], " "), os.Stdin, os.Stdout, h, w)
	terminal.Restore(int(fd), stdinState)

	if err != nil {
		stdcli.Error(err)
		return
	}

	os.Exit(code)
}
开发者ID:cleblanc87,项目名称:rack,代码行数:36,代码来源:exec.go



注:本文中的golang.org/x/crypto/ssh/terminal.GetState函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang terminal.IsTerminal函数代码示例发布时间:2022-05-28
下一篇:
Golang terminal.GetSize函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap