summaryrefslogtreecommitdiff
path: root/vendor/github.com/lunixbochs/vtclean/line.go
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2020-10-07 16:58:53 +0200
committerValentin Rothberg <rothberg@redhat.com>2020-11-13 15:40:06 +0100
commit8e4a42aa429c6dec0d5face7c69554d8a0677e96 (patch)
treebbfff77e7b32a8b46af6f57d42965a7751bec18e /vendor/github.com/lunixbochs/vtclean/line.go
parent0b1a60ec27928a40ac827148c1517098612616bd (diff)
downloadpodman-8e4a42aa429c6dec0d5face7c69554d8a0677e96.tar.gz
podman-8e4a42aa429c6dec0d5face7c69554d8a0677e96.tar.bz2
podman-8e4a42aa429c6dec0d5face7c69554d8a0677e96.zip
short-name aliasing
Add support for short-name aliasing. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'vendor/github.com/lunixbochs/vtclean/line.go')
-rw-r--r--vendor/github.com/lunixbochs/vtclean/line.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/vendor/github.com/lunixbochs/vtclean/line.go b/vendor/github.com/lunixbochs/vtclean/line.go
new file mode 100644
index 000000000..66ee990be
--- /dev/null
+++ b/vendor/github.com/lunixbochs/vtclean/line.go
@@ -0,0 +1,113 @@
+package vtclean
+
+type char struct {
+ char byte
+ vt100 []byte
+}
+
+func chars(p []byte) []char {
+ tmp := make([]char, len(p))
+ for i, v := range p {
+ tmp[i].char = v
+ }
+ return tmp
+}
+
+type lineEdit struct {
+ buf []char
+ pos, size int
+ vt100 []byte
+}
+
+func newLineEdit(length int) *lineEdit {
+ return &lineEdit{buf: make([]char, length)}
+}
+
+func (l *lineEdit) Vt100(p []byte) {
+ l.vt100 = p
+}
+
+func (l *lineEdit) Move(x int) {
+ if x < 0 && l.pos <= -x {
+ l.pos = 0
+ } else if x > 0 && l.pos+x > l.size {
+ l.pos = l.size
+ } else {
+ l.pos += x
+ }
+}
+
+func (l *lineEdit) MoveAbs(x int) {
+ if x < l.size {
+ l.pos = x
+ }
+}
+
+func (l *lineEdit) Write(p []byte) {
+ c := chars(p)
+ if len(c) > 0 {
+ c[0].vt100 = l.vt100
+ l.vt100 = nil
+ }
+ if len(l.buf)-l.pos < len(c) {
+ l.buf = append(l.buf[:l.pos], c...)
+ } else {
+ copy(l.buf[l.pos:], c)
+ }
+ l.pos += len(c)
+ if l.pos > l.size {
+ l.size = l.pos
+ }
+}
+
+func (l *lineEdit) Insert(p []byte) {
+ c := chars(p)
+ if len(c) > 0 {
+ c[0].vt100 = l.vt100
+ l.vt100 = nil
+ }
+ l.size += len(c)
+ c = append(c, l.buf[l.pos:]...)
+ l.buf = append(l.buf[:l.pos], c...)
+}
+
+func (l *lineEdit) Delete(n int) {
+ most := l.size - l.pos
+ if n > most {
+ n = most
+ }
+ copy(l.buf[l.pos:], l.buf[l.pos+n:])
+ l.size -= n
+}
+
+func (l *lineEdit) Clear() {
+ for i := 0; i < len(l.buf); i++ {
+ l.buf[i].char = ' '
+ }
+}
+func (l *lineEdit) ClearLeft() {
+ for i := 0; i < l.pos+1; i++ {
+ l.buf[i].char = ' '
+ }
+}
+func (l *lineEdit) ClearRight() {
+ l.size = l.pos
+}
+
+func (l *lineEdit) Bytes() []byte {
+ length := 0
+ buf := l.buf[:l.size]
+ for _, v := range buf {
+ length += 1 + len(v.vt100)
+ }
+ tmp := make([]byte, 0, length)
+ for _, v := range buf {
+ tmp = append(tmp, v.vt100...)
+ tmp = append(tmp, v.char)
+ }
+ return tmp
+}
+
+func (l *lineEdit) String() string {
+ return string(l.Bytes())
+}