Пример #1
0
Файл: read.go Проект: kless/term
// NewDefaultLine returns a line type using the prompt by default, and setting
// the terminal to raw mode.
// If the history is nil then it is not used.
func NewDefaultLine(hist *history) (*Line, error) {
	ter, err := term.New()
	if err != nil {
		return nil, err
	}
	if err = ter.RawMode(); err != nil {
		return nil, err
	}

	_, col, err := ter.GetSize()
	if err != nil {
		return nil, err
	}

	buf := newBuffer(len(PS1), col)
	buf.insertRunes([]rune(PS1))

	return &Line{
		ter:  ter,
		buf:  buf,
		hist: hist,

		ps1:    PS1,
		ps2:    PS2,
		lenPS1: len(PS1),

		useHistory: hasHistory(hist),
	}, nil
}
Пример #2
0
func TestEchoMode() {
	fmt.Print("\n=== RUN TestEchoMode\n")

	ter, _ := term.New()
	defer func() {
		if err := ter.Restore(); err != nil {
			log.Print(err)
		}
	}()

	if err := ter.EchoMode(false); err != nil {
		log.Print("expected to set echo mode:", err)
		return
	}
	fmt.Print(" + Mode to echo off\n")
	buf := bufio.NewReader(term.Input)

	if !*IsInteractive {
		go func() {
			time.Sleep(time.Duration(*Time) * time.Second)
			fmt.Fprint(pw, "Karma\n")
		}()
	}
	fmt.Print(" Write (enter to finish): ")
	line, err := buf.ReadString('\n')
	if err != nil {
		log.Print(err)
		return
	}
	fmt.Printf("\n entered: %q\n", line)

	ter.EchoMode(true)
	fmt.Print("\n + Mode to echo on\n")

	if !*IsInteractive {
		go func() {
			time.Sleep(time.Duration(*Time) * time.Second)
			fmt.Fprint(pw, "hotel\n")
		}()
	}
	fmt.Print(" Write (enter to finish): ")
	line, _ = buf.ReadString('\n')
	if !*IsInteractive {
		fmt.Println()
	}
	fmt.Printf(" entered: %q\n", line)
}
Пример #3
0
// Lookup prints the decimal code at pressing a key.
func Lookup() {
	ter, err := term.New()
	if err != nil {
		log.Print(err)
		return
	}
	defer func() {
		if err = ter.Restore(); err != nil {
			log.Print(err)
		}
	}()

	if err = ter.RawMode(); err != nil {
		log.Print(err)
		return
	} else {
		buf := bufio.NewReader(term.Input)
		runes := make([]int32, 0)
		chars := make([]string, 0)

		fmt.Print("[Press Enter to exit]\r\n")
		fmt.Print("> ")

	L:
		for {
			rune_, _, err := buf.ReadRune()
			if err != nil {
				log.Print(err)
				continue
			}

			switch rune_ {
			default:
				fmt.Print(rune_, " ")
				runes = append(runes, rune_)
				char := strconv.QuoteRune(rune_)
				chars = append(chars, char[1:len(char)-1])
				continue

			case 13:
				fmt.Printf("\r\n\r\n%v\r\n\"%s\"\r\n", runes, strings.Join(chars, " "))
				break L
			}
		}
	}
}
Пример #4
0
// TestCharMode tests terminal set to single character mode.
func TestCharMode() {
	fmt.Print("\n=== RUN TestCharMode\n")

	ter, _ := term.New()
	defer func() {
		if err := ter.Restore(); err != nil {
			log.Print(err)
		}
	}()

	if err := ter.CharMode(); err != nil {
		log.Print("expected to set character mode:", err)
		return
	}

	buf := bufio.NewReaderSize(term.Input, 4)
	reply := []string{"a", "€", "~"}

	if !*IsInteractive {
		go func() {
			for _, r := range reply {
				time.Sleep(time.Duration(*Time) * time.Second)
				fmt.Fprint(pw, r)
			}
		}()
	}

	for i := 1; ; i++ {
		fmt.Print(" Press key: ")
		rune, _, err := buf.ReadRune()
		if err != nil {
			log.Print(err)
			return
		}
		fmt.Printf("\n pressed: %q\n", string(rune))

		if *IsInteractive || i == len(reply) {
			break
		}
	}
}
Пример #5
0
func TestDetectSize() {
	fmt.Print("\n=== RUN TestDetectSize\n")

	ter, _ := term.New()
	defer func() {
		if err := ter.Restore(); err != nil {
			log.Print(err)
		}
	}()

	row, col, err := ter.GetSize()
	if err != nil {
		panic(err)
	}

	winSize := term.DetectWinSize()
	fmt.Println("[Change the size of the terminal]")

	// I want to finish the test.
	go func() {
		time.Sleep(10 * time.Second)
		winSize.Change <- true
	}()

	<-winSize.Change
	winSize.Close()

	row2, col2, err := ter.GetSize()
	if err != nil {
		panic(err)
	}
	if row == row2 && col == col2 {
		log.Print("the terminal size got the same value")
		return
	}
}