Properly truncate long query string

This commit is contained in:
Junegunn Choi 2017-02-18 23:17:29 +09:00
parent c2db67c1c0
commit 309e1d8619
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 22 additions and 11 deletions

View File

@ -18,10 +18,9 @@ const (
readerBufferSize = 64 * 1024 readerBufferSize = 64 * 1024
// Terminal // Terminal
initialDelay = 20 * time.Millisecond initialDelay = 20 * time.Millisecond
initialDelayTac = 100 * time.Millisecond initialDelayTac = 100 * time.Millisecond
spinnerDuration = 200 * time.Millisecond spinnerDuration = 200 * time.Millisecond
maxPatternLength = 100
// Matcher // Matcher
numPartitionsMultiplier = 8 numPartitionsMultiplier = 8

View File

@ -603,6 +603,7 @@ func (t *Terminal) resizeWindows() {
t.window.MoveAndClear(i, 0) t.window.MoveAndClear(i, 0)
} }
} }
t.truncateQuery()
} }
func (t *Terminal) move(y int, x int, clear bool) { func (t *Terminal) move(y int, x int, clear bool) {
@ -628,13 +629,19 @@ func (t *Terminal) printPrompt() {
} }
func (t *Terminal) printInfo() { func (t *Terminal) printInfo() {
pos := 0
if t.inlineInfo { if t.inlineInfo {
t.move(0, t.displayWidth([]rune(t.prompt))+t.displayWidth(t.input)+1, true) pos = t.displayWidth([]rune(t.prompt)) + t.displayWidth(t.input) + 1
if pos+len(" < ") > t.window.Width() {
return
}
t.move(0, pos, true)
if t.reading { if t.reading {
t.window.CPrint(tui.ColSpinner, t.strong, " < ") t.window.CPrint(tui.ColSpinner, t.strong, " < ")
} else { } else {
t.window.CPrint(tui.ColPrompt, t.strong, " < ") t.window.CPrint(tui.ColPrompt, t.strong, " < ")
} }
pos += len(" < ")
} else { } else {
t.move(1, 0, true) t.move(1, 0, true)
if t.reading { if t.reading {
@ -643,6 +650,7 @@ func (t *Terminal) printInfo() {
t.window.CPrint(tui.ColSpinner, t.strong, _spinner[idx]) t.window.CPrint(tui.ColSpinner, t.strong, _spinner[idx])
} }
t.move(1, 2, false) t.move(1, 2, false)
pos = 2
} }
output := fmt.Sprintf("%d/%d", t.merger.Length(), t.count) output := fmt.Sprintf("%d/%d", t.merger.Length(), t.count)
@ -659,7 +667,9 @@ func (t *Terminal) printInfo() {
if t.progress > 0 && t.progress < 100 { if t.progress > 0 && t.progress < 100 {
output += fmt.Sprintf(" (%d%%)", t.progress) output += fmt.Sprintf(" (%d%%)", t.progress)
} }
t.window.CPrint(tui.ColInfo, 0, output) if pos+len(output) <= t.window.Width() {
t.window.CPrint(tui.ColInfo, 0, output)
}
} }
func (t *Terminal) printHeader() { func (t *Terminal) printHeader() {
@ -1210,6 +1220,12 @@ func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item
return true, sels return true, sels
} }
func (t *Terminal) truncateQuery() {
maxPatternLength := util.Max(1, t.window.Width()-t.displayWidth([]rune(t.prompt))-1)
t.input, _ = t.trimRight(t.input, maxPatternLength)
t.cx = util.Constrain(t.cx, 0, len(t.input))
}
// Loop is called to start Terminal I/O // Loop is called to start Terminal I/O
func (t *Terminal) Loop() { func (t *Terminal) Loop() {
// prof := profile.Start(profile.ProfilePath("/tmp/")) // prof := profile.Start(profile.ProfilePath("/tmp/"))
@ -1688,11 +1704,7 @@ func (t *Terminal) Loop() {
if !doActions(actions, mapkey) { if !doActions(actions, mapkey) {
continue continue
} }
// Truncate the query if it's too long t.truncateQuery()
if len(t.input) > maxPatternLength {
t.input = t.input[:maxPatternLength]
t.cx = util.Constrain(t.cx, 0, maxPatternLength)
}
changed = string(previousInput) != string(t.input) changed = string(previousInput) != string(t.input)
} else { } else {
if mapkey == tui.Rune { if mapkey == tui.Rune {