34 Commits

Author SHA1 Message Date
Josh Bleecher Snyder
943f25d359 x/term: handle transpose
The behavior implemented here matches readline and libedit.

Updates golang/go#76826

Change-Id: I893677f9bceaf75aa1dada7d893845728e07057e
Reviewed-on: https://go-review.googlesource.com/c/term/+/730441
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: David Chase <drchase@google.com>
2025-12-17 11:04:51 -08:00
Josh Bleecher Snyder
9b991dd831 x/term: handle delete key
Fixes golang/go#76826

Change-Id: I80dc9468c2cc716dab10a8e165ca5f72817ef4b9
Reviewed-on: https://go-review.googlesource.com/c/term/+/730440
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-12-16 13:07:46 -08:00
cuishuang
3475bc8ef1 term: fix some comments
Change-Id: I8211158071d0a657d0097aba242278fa4bc7f653
Reviewed-on: https://go-review.googlesource.com/c/term/+/713420
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2025-10-23 11:33:45 -07:00
Laurent Demailly
4f53e0cd39 term: allow multi-line bracketed paste to not create single line with verbatim LFs
Treat "\n" (LF) like "Enter" (CR)

Avoids that when pasting 3 lines
(with a terminal like kitty, ghostty, alacritty that do not change the clipboard
in bracketed paste mode)
it turns into 1 prompt looking like:

Test> line one
..............line.two
......................line.three

Fixes golang/go#74600

Change-Id: I4a86044a4a175eccb3a96dbf7021fee97a5940ce
GitHub-Last-Rev: 0cf26df9ae
GitHub-Pull-Request: golang/term#21
Reviewed-on: https://go-review.googlesource.com/c/term/+/687755
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-07-21 12:13:29 -07:00
Laurent Demailly
a809085bff term: support pluggable history
Expose a new History interface that allows replacement of the default
ring buffer to customize what gets added or not; as well as to allow
saving/restoring history on either the default ringbuffer or a custom
replacement.

Fixes golang/go#68780

Change-Id: I7e61dc6bb438749c8502223705518ef8ff9025b4
GitHub-Last-Rev: 621281355f
GitHub-Pull-Request: golang/term#20
Reviewed-on: https://go-review.googlesource.com/c/term/+/659835
Reviewed-by: Michael Pratt <mpratt@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Austin Clements <austin@google.com>
2025-04-21 12:30:57 -07:00
Dragos Vingarzan
e770dddbf5 x/term: disabling auto-completion around GetPassword()
Triggering the completion during password input might cause some
unintended behavior around handling of TAB, or maybe the auto-completion
functionality would review the secret input. Hence simply
disabling/re-enabling it around the t.readLine call.

Fixes #72736

Change-Id: I64270e8570086247247466afb2536b2581d6af25
Reviewed-on: https://go-review.googlesource.com/c/term/+/607115
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-03-15 12:59:10 -07:00
M. J. Fromberger
f6f2839df8 term: remove unused variable
The eraseUnderCursor variable is unused, it came in with the import
of the x/crypto/ssh/terminal package at d7a7210 but is not referenced
here.

When this package is vendored, the unused unexported name trips warnings
from tools like staticcheck.

Change-Id: I4289239edcdfec434ec26b164ae6c0a9b9ee7067
GitHub-Last-Rev: a4ab5868b4
GitHub-Pull-Request: golang/term#10
Reviewed-on: https://go-review.googlesource.com/c/term/+/446455
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
2022-11-02 18:10:37 +00:00
ardnew
065cf7ba24 x/term: prevent invalid indexing into stRingBuffer
The exported method (*stRingBuffer).NthPreviousEntry does not correctly
handle arguments with negative values. A negative value will index
beyond slice boundaries in most cases (unless size = max = INT_MAX) and
cause an access violation at runtime.

This change adds a condition to return ok = false for all negatively
valued arguments, which is the same behavior that occurs with positively
valued arguments exceeding buffer length.

Adding the capability to index backwards (from the end of the slice)
does not seem like the intent of this method, and it would not improve
or simplify existing functionality. It would also be inconsistent with
the handling of positive values out-of-bounds.

Change-Id: Ib4113330f0044dd5b73c7d75c5cdcdd27d60ee77
GitHub-Last-Rev: fec355f536
GitHub-Pull-Request: golang/term#8
Reviewed-on: https://go-review.googlesource.com/c/term/+/408754
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
2022-05-26 00:47:31 +00:00
Filippo Valsorda
d7a72108b8 all: import golang.org/x/crypto/ssh/terminal
This merges all code from x/crypto/ssh/terminal at commit
0c6587e931a935f573efb6988c8470cd8c0a58de with no refactoring,
simply mapping util_* files to term_*.

The x/crypto history for the merge was rewritten with

    git filter-repo --path ssh/terminal/ --path-rename ssh/terminal/:

and then stitched together with "git replace --graft".

The merge preserves git blame visibility.

Updates #31044

Change-Id: Ic51fc8ec20b3a384cdd8868fde06fe69b6317695
2020-11-14 00:47:01 +01:00
Hana (Hyang-Ah) Kim
07bee379ff ssh/terminal: handle ctrl+C, ctrl+F, ctrl+B
ctrl+C: terminate readline, which may result in application termination.
ctrl+F: keyRight
ctrl+B: keyLeft

Update golang/go#27147

Change-Id: If319ef79708b98c030cbce102400a785d15137f8
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/228223
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2020-04-14 15:58:20 +00:00
Alex Brainman
1a268e5305 ssh/terminal: adjust ReadConsole rules on windows
CL 212377 changed end of input character on windows - from \n to \r.
But CL 212377 did not adjust ReadConsole accordingly. For example,
after CL 212377 \n was still used to end of password processing,
and \r was ignored.

This CL swaps these rules - \r is now used to end password processing,
and \n are ignored. The change only affects windows, all non windows
code should work as before.

This CL also adjusts TestReadPasswordLineEnd to fit new rules.

Fixes golang/go#36609

Change-Id: I027bf80d10e7d4d4b17ff0264935d14b8bea9097
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/215417
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
2020-02-08 06:05:01 +00:00
Yasuhiro Matsumoto
31072773c2 ssh/terminal: stop using ENABLE_LINE_INPUT
ReadConsole does not read more than 254 bytes when ENABLE_LINE_INPUT is
enabled.

Fixes golang/go#36071

Change-Id: If5c160404b855387a80f1d57638aac3f2db1a097
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/212377
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2020-01-15 08:54:10 +00:00
Andrey Petrov
c5b4c79d1f ssh/terminal: Use move-N sequences for >1 cursor moves
Before, we emitted N single-move sequences on a cursor move. For
example, "move 4 left" would emit "^[[D^[[D^[[D^[[D". With this change,
it would emit "^[[4D".

Using variable move sequences when possible reduces the amount of
rendering output that the terminal implementation produces. This can
have some low-level performance benefits, but also helps consumers
reason through the produced output.

Includes a test with a couple of cases.

Note: The old implementation used ^[[D instead of ^[D which is also
valid. This is true in several unrelated places, so this implementation
continues to use ^[[D for consistency.

Change-Id: If38eaaed8fb4075499fdda54c06681dc34c3ad70
GitHub-Last-Rev: 92ef2538d33a9493f3df09984c277dfd8bf0abf4
GitHub-Pull-Request: golang/crypto#82
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/169077
Reviewed-by: Adam Langley <agl@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-03-25 15:42:30 +00:00
Michal Bohuslávek
1fb3bd0768 ssh/terminal: support ^N and ^P
This makes it possible to navigate the history without leaving
the home row on the keyboard.

Change-Id: Id24c43f8eb6090520ab37bf8126264901b70c489
Reviewed-on: https://go-review.googlesource.com/c/33618
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-01-28 19:33:16 +00:00
Kevin Burke
f344d0325d all: fix errors reported by vet, golint
None are "wrong" per se, but there are a lot of good suggestions and
in one case a docstring that was not present in godoc due to the
presence of an extra newline.

Changed "Id" in struct properties to "ID" in some non-exported
structs. Removed a trailing period from some error messages; I believe
the exact contents of error strings are not covered by the Go
compatibility promise.

Change-Id: I7c620582dc247396f72c52d38c909ccc0ec87b83
Reviewed-on: https://go-review.googlesource.com/80145
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-11-28 17:48:09 +00:00
Adam Langley
efcab1a52e ssh/terminal: consume data before checking for an error.
According to the io.Reader docs, Alex had it right the first time. (See
discussion on https://golang.org/cl/25355.)

Change-Id: Ib6fb9dfb99009e034263574e82d7e9d4828df38f
Reviewed-on: https://go-review.googlesource.com/35242
TryBot-Result: Gobot Gobot <gobot@golang.org>
Run-TryBot: Adam Langley <agl@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Adam Langley <agl@golang.org>
2017-01-17 00:48:27 +00:00
Alex Brainman
038bd0bb6d ssh/terminal: fix line endings handling in ReadPassword
Fixes golang/go#16552

Change-Id: I18a9c9b42fe042c4871b3efb3f51bef7cca335d0
Reviewed-on: https://go-review.googlesource.com/25355
Reviewed-by: Adam Langley <alangley@gmail.com>
Reviewed-by: Adam Langley <agl@golang.org>
2017-01-16 17:16:45 +00:00
Joe Tsai
3acf1ca968 all: fix some vet warnings
Change-Id: I85c2912a6862c6c251450f2a0926ecd33a9fb8e7
Reviewed-on: https://go-review.googlesource.com/34815
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-01-04 23:09:04 +00:00
Mikio Hara
426ef97d77 ssh/terminal: fix a typo
Change-Id: Iafe2ebb6d37afd2a64aa72750a722d4860bb735e
Reviewed-on: https://go-review.googlesource.com/34535
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-12-19 06:01:35 +00:00
Adam Langley
3ea8c23763 x/crypto/ssh/terminal: replace \n with \r\n.
18e6eb769a made MakeRaw match C's behaviour. This included clearing the
OPOST flag, which means that one now needs to write \r\n for a newline,
otherwise the cursor doesn't move back to the beginning and the terminal
prints a staircase.

(Dear god, we're still emulating line printers.)

This change causes the terminal package to do the required
transformation.

Fixes golang/go#17364.

Change-Id: Ida15d3cf701a21eaa59161ab61b3ed4dee2ded46
Reviewed-on: https://go-review.googlesource.com/33902
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-12-07 22:17:40 +00:00
Derek Che
fd5a52a5d8 ssh/terminal: fix SetSize when nothing on current line
SetSize has a problem may cause the following ReadPassword setting
temporary prompt not working, when changing width the current
SetSize will call clearAndRepaintLinePlusNPrevious which would
print an old prompt whatever the current line has, causing a following
ReadPassword with temporary prompt not printing the different prompt.

When running code like this, the nt.SetSize prints a "> " as
prompt then the temporary "Password: " prompt would never show up.

```go
        oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
        width, height, _ = terminal.GetSize(int(os.Stdin.Fd()))
        nt := terminal.NewTerminal(os.Stdin, "> ")
        nt.SetSize(width, height)
        password, err = nt.ReadPassword("Password: ")
```

the new test cases is to test SetSize with different terminal sizes,
either shrinking or expanding, a following ReadPassword should get the
correct temporary prompt.

Change-Id: I33d13b2c732997c0c88670d53545b8c0048b94b6
Reviewed-on: https://go-review.googlesource.com/1861
Reviewed-by: Adam Langley <agl@golang.org>
2014-12-19 22:48:49 +00:00
Adam Langley
d037c2cd54 go.crypto/ssh/terminal: support bracketed paste mode.
Some terminals support a mode where pasted text is bracketed by escape sequences. This is very useful for terminal applications that otherwise have no good way to tell pastes and typed text apart.

This change allows applications to enable this mode and, if the terminal supports it, will suppress autocompletes during pastes and indicate to the caller that a line came entirely from pasted text.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/171330043
2014-11-16 14:01:45 -08:00
Adam Langley
21bda07dd6 go.crypto/ssh/terminal: fix Home and End.
In my notes I had Home and End down as OH and OF. But that's nonsense, they are [H and ]F.
I never noticed before because I don't have Home and End keys on my keyboard.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/172100043
2014-11-07 19:20:14 -08:00
Adam Langley
2a0b1644c0 go.crypto/ssh/terminal: fix crash when terminal narrower than prompt.
Previously, if the current line was "empty", resizes wouldn't trigger
repaints. However, the line can be empty when the prompt is non-empty
and the code would then panic after a resize because the cursor position
was outside of the terminal.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/158090043
2014-10-25 11:16:08 -07:00
Adam Langley
e0a9d256f7 go.crypto/ssh/terminal: better handling of window resizing.
There doesn't appear to be perfect behaviour for line editing
code in the face of terminal resizing. But this change works
pretty well on xterm and gnome-terminal and certainly a lot
better than it used to.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/105580043
2014-08-01 11:22:47 -07:00
Adam Langley
d47901289b go.crypto/ssh/terminal: support ^U, ^D and ^L.
LGTM=bradfitz
R=bradfitz, marios.nikolaou
CC=golang-codereviews
https://golang.org/cl/92220043
2014-05-27 19:45:07 -07:00
Frithjof Schulze
879e955024 go.crypto/ssh/terminal: Allow ^A and ^E as synonyms for Home and End.
I understand that ssh/terminal can't implement everybodys
favorite keyboard shortcuts, but I think these are very
widespread. They exist not only in Emacs or Readline, but also
in Acme and Sam. Also they almost come for free.

R=golang-dev
CC=agl, golang-dev
https://golang.org/cl/13839047
2013-09-30 16:06:07 -04:00
Adam Langley
4986295c47 go.crypto/ssh/terminal: fix non-ASCII history.
The length of history buffer entries (which are stored as strings) was
being used as the number of runes. This was correct until ff9ce887b46b,
which allowed unicode entry, but can now cause a panic when editing
history that contains non-ASCII codepoints.

R=golang-dev, sfrithjof, r
CC=golang-dev
https://golang.org/cl/13255050
2013-09-15 12:48:02 -04:00
Adam Langley
1b08228a8a go.crypto/ssh/terminal: support Unicode entry.
Previously, terminal only supported ASCII characters. This change
alters some []byte to []rune so that the full range of Unicode is
supported. The only thing that doesn't appear to work correctly are
grapheme clusters as the code still assumes one rune per glyph. Still,
this change allows many more languages to work than did previously.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/13704043
2013-09-13 14:33:00 -04:00
Adam Langley
78276a84ee go.crypto/ssh/terminal: handle ^W, ^K and ^H
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/13207043
2013-08-28 07:36:04 -04:00
Adam Langley
89c74edf66 go.crypto/ssh/terminal: support Go 1.0.
For those still stuck on Go 1.0.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/11297043
2013-07-15 18:01:31 -04:00
Adam Langley
3293448389 go.crypto/ssh/terminal: don't save passwords in history.
The history buffer would recall previously entered lines: including passwords. With this change, lines entered while echo is disabled are no longer put into the history.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/10853043
2013-07-02 19:46:13 -04:00
Adam Langley
7d4f6f0986 ssh/terminal: support home, end, up and down keys.
R=golang-dev, dave
CC=golang-dev
https://golang.org/cl/9777043
2013-06-07 10:21:53 -04:00
David Symonds
34e6ae6050 go.crypto: add exp/terminal as code.google.com/p/go.crypto/ssh/terminal.
This removes the sole "exp/foo" import in the Go subrepos.
A separate CL will remove exp/terminal from the standard Go repository.

R=golang-dev, dave, r
CC=golang-dev
https://golang.org/cl/5966045
2012-03-30 15:27:01 +11:00