Exemple #1
0
func TestRawMode(t *testing.T) {
	term, err := terminal.New(INPUT_FD)
	if err != nil {
		t.Fatal(err)
	}

	//	oldState := term.oldState

	if err = term.RawMode(); err != nil {
		t.Error("expected set raw mode:", err)
	}

	/*name, err := term.GetName()
	  if err != nil {
	  	log.Print(err)
	  }
	  println("Console", name)*/

	if err = term.Restore(); err != nil {
		t.Error("expected to restore:", err)
	}

	/*	lastState := term.State
		if oldState != lastState {
			t.Error("expected to restore settings")
		}
	*/
	// Restore from a saved state
	term, _ = terminal.New(INPUT_FD)
	state := term.OriginalState()

	if err = terminal.Restore(INPUT_FD, state); err != nil {
		t.Error("expected to restore from saved state:", err)
	}
}
Exemple #2
0
// NewLine returns a line using both prompts ps1 and ps2, and setting the TTY to
// raw mode.
// lenAnsi is the length of ANSI codes that the prompt ps1 could have.
// If the history is nil then it is not used.
func NewLine(ps1, ps2 string, lenAnsi int, hist *history, echoRune rune) (*Line, error) {
	term, err := terminal.New(InputFd)
	if err != nil {
		return nil, err
	}
	if err = term.RawMode(); err != nil {
		return nil, err
	}

	lenPS1 := len(ps1) - lenAnsi
	_, col, err := term.GetSize()
	if err != nil {
		return nil, err
	}

	buf := newBuffer(lenPS1, col, echoRune)
	buf.insertRunes([]rune(ps1))

	return &Line{
		hasHistory(hist),
		lenPS1,
		ps1,
		ps2,
		buf,
		hist,
		term,
	}, nil
}
Exemple #3
0
// NewDefaultLine returns a line type using the prompt by default, and setting
// the TTY to raw mode.
// If the history is nil then it is not used.
func NewDefaultLine(hist *history) (*Line, error) {
	term, err := terminal.New(InputFd)
	if err != nil {
		return nil, err
	}
	if err = term.RawMode(); err != nil {
		return nil, err
	}

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

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

	return &Line{
		hasHistory(hist),
		len(_PS1),
		_PS1,
		_PS2,
		buf,
		hist,
		term,
	}, nil
}
Exemple #4
0
func TestInformation(t *testing.T) {
	term, _ := terminal.New(INPUT_FD)
	defer term.Restore()

	/*	if !CheckANSI() {
			t.Error("expected to support this terminal")
		}
	*/
	/*	if !terminal.IsTerminal(syscall.Handle(3)) {
		t.Error("expected to be a terminal")
	}*/

	/*if _, err := TTYName(term.fd); err != nil {
		t.Error("expected to get the terminal name", err)
	}*/
}
Exemple #5
0
// New returns a Question with the given arguments.
//
// prefix is the text placed before of the prompt, errPrefix is placed before of
// show any error.
//
// trueString and falseString are the strings to be showed when the question
// needs a boolean like answer and it is being used a default value.
// It is already handled the next strings like boolean values (from validate.Atob):
//   1, t, T, TRUE, true, True, y, Y, yes, YES, Yes
//   0, f, F, FALSE, false, False, n, N, no, NO, No
func New(prefix, errPrefix, trueString, falseString string) *Question {
	term, err := terminal.New(editline.InputFd)
	if err != nil {
		panic(err)
	}

	extraBool := make(map[string]bool)
	val := validate.New(validate.Bool, validate.NONE)

	// Add strings of boolean values if there are not validated like boolean.
	if _, err = val.Atob(trueString); err != nil {
		extraBool = map[string]bool{
			strings.ToLower(trueString): true,
			strings.ToUpper(trueString): true,
			strings.Title(trueString):   true,
		}
	}
	if _, err = val.Atob(falseString); err != nil {
		extraBool[strings.ToLower(falseString)] = false
		extraBool[strings.ToUpper(falseString)] = false
		extraBool[strings.Title(falseString)] = false
	}

	return &Question{
		false,
		NONE,

		prefix,
		errPrefix,
		"",

		"",
		nil,

		trueString,
		falseString,
		extraBool,

		0,

		term.Fd(),
		term.OriginalState(),
	}
}
Exemple #6
0
// TestLookup prints the decimal code at pressing a key.
func TestLookup(t *testing.T) {
	term, err := terminal.New(InputFd)
	if err != nil {
		t.Fatal(err)
	}
	defer term.Restore()

	if err = term.MakeRaw(); err != nil {
		t.Error(err)
	} else {
		buf := bufio.NewReader(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 {
				t.Error(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\r\n", runes, strings.Join(chars, " "))
				break L
			}
		}
	}
}
Exemple #7
0
func TestModes(t *testing.T) {
	term, _ := terminal.New(INPUT_FD)

	// Single-character

	err := term.CharMode()
	if err != nil {
		log.Print("expected to set mode:", err)
	} else {
		buf := bufio.NewReaderSize(INPUT, 4)
		exit := false

		fmt.Print("\n + Mode to single character\n")

		if !*fInteractive {
			reply := []string{
				"a",
				"€",
				"~",
			}

			go func() {
				for _, r := range reply {
					time.Sleep(time.Duration(*fTime) * time.Second)
					fmt.Fprint(OUTPUT, r)
				}
				exit = true
			}()
		}

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

			if *fInteractive && i == 1 {
				break
			}
			if exit {
				break
			}
		}
	}
	term.Restore()

	// Echo

	if err := term.EchoMode(false); err != nil {
		log.Print("expected to set mode:", err)
	} else {
		buf := bufio.NewReader(INPUT)

		fmt.Print("\n + Mode to echo off\n")

		if !*fInteractive {
			go func() {
				time.Sleep(time.Duration(*fTime) * time.Second)
				fmt.Fprint(OUTPUT, "Karma\r\n")
			}()
		}
		fmt.Print(" Write (enter to finish): ")
		line, err := buf.ReadString('\n')
		if err != nil {
			term.Restore()
			log.Fatal(err)
		}
		fmt.Printf("\n entered: %q\n", line)

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

		if !*fInteractive {
			go func() {
				time.Sleep(time.Duration(*fTime) * time.Second)
				fmt.Fprint(OUTPUT, "hotel\r\n")
			}()
		}
		fmt.Print(" Write (enter to finish): ")
		line, _ = buf.ReadString('\n')
		if !*fInteractive {
			fmt.Println()
		}
		fmt.Printf(" entered: %q\n", line)
	}

	term.Restore()

	// Password

	/*if !*fInteractive {
		go func() {
			time.Sleep(time.Duration(*fTime) * time.Second)
			fmt.Fprint(OUTPUT, "Parallel universe\n\n")
		}()
	}*/
	/*
		if *fInteractive {
			fmt.Print("\n Password: "******"\n entered: %q\n number: %d\n", pass, n)
		}

		fmt.Println()*/
}