Code cleanup

This commit is contained in:
Junegunn Choi 2023-02-01 18:16:58 +09:00
parent aa2b9ec476
commit f7447aece1
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

View File

@ -104,7 +104,6 @@ type previewer struct {
version int64 version int64
lines []string lines []string
offset int offset int
enabled bool
scrollable bool scrollable bool
final bool final bool
following resumableState following resumableState
@ -645,7 +644,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
reqBox: util.NewEventBox(), reqBox: util.NewEventBox(),
initialPreviewOpts: opts.Preview, initialPreviewOpts: opts.Preview,
previewOpts: opts.Preview, previewOpts: opts.Preview,
previewer: previewer{0, []string{}, 0, len(opts.Preview.command) > 0, false, true, disabledState, "", []bool{}}, previewer: previewer{0, []string{}, 0, false, true, disabledState, "", []bool{}},
previewed: previewed{0, 0, 0, false}, previewed: previewed{0, 0, 0, false},
previewBox: previewBox, previewBox: previewBox,
eventBox: eventBox, eventBox: eventBox,
@ -1034,7 +1033,7 @@ func (t *Terminal) adjustMarginAndPadding() (int, int, [4]int, [4]int) {
if t.noInfoLine() { if t.noInfoLine() {
minAreaHeight -= 1 minAreaHeight -= 1
} }
if t.mayNeedPreviewWindow() { if t.needPreviewWindow() {
minPreviewHeight := 1 + borderLines(t.previewOpts.border) minPreviewHeight := 1 + borderLines(t.previewOpts.border)
minPreviewWidth := 5 minPreviewWidth := 5
switch t.previewOpts.position { switch t.previewOpts.position {
@ -1117,7 +1116,7 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
// Set up preview window // Set up preview window
noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode) noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode)
if t.mayNeedPreviewWindow() { if forcePreview || t.needPreviewWindow() {
var resizePreviewWindows func(previewOpts *previewOpts) var resizePreviewWindows func(previewOpts *previewOpts)
resizePreviewWindows = func(previewOpts *previewOpts) { resizePreviewWindows = func(previewOpts *previewOpts) {
t.activePreviewOpts = previewOpts t.activePreviewOpts = previewOpts
@ -2277,13 +2276,13 @@ func (t *Terminal) hasPreviewer() bool {
return t.previewBox != nil return t.previewBox != nil
} }
func (t *Terminal) mayNeedPreviewWindow() bool { func (t *Terminal) needPreviewWindow() bool {
return t.hasPreviewer() && t.previewer.enabled && t.previewOpts.Visible() return t.hasPreviewer() && len(t.previewOpts.command) > 0 && t.previewOpts.Visible()
} }
// Check if previewer is currently in action (invisible previewer with size 0 or visible previewer) // Check if previewer is currently in action (invisible previewer with size 0 or visible previewer)
func (t *Terminal) isPreviewEnabled() bool { func (t *Terminal) canPreview() bool {
return t.hasPreviewer() && t.previewer.enabled && (!t.previewOpts.Visible() && !t.previewOpts.hidden || t.pwindow != nil) return t.hasPreviewer() && (!t.previewOpts.Visible() && !t.previewOpts.hidden || t.hasPreviewWindow())
} }
func (t *Terminal) hasPreviewWindow() bool { func (t *Terminal) hasPreviewWindow() bool {
@ -2391,7 +2390,7 @@ func (t *Terminal) Loop() {
pad := fitpad.pad pad := fitpad.pad
t.tui.Resize(func(termHeight int) int { t.tui.Resize(func(termHeight int) int {
contentHeight := fit + t.extraLines() contentHeight := fit + t.extraLines()
if t.mayNeedPreviewWindow() { if t.needPreviewWindow() {
if t.previewOpts.aboveOrBelow() { if t.previewOpts.aboveOrBelow() {
if t.previewOpts.size.percent { if t.previewOpts.size.percent {
newContentHeight := int(float64(contentHeight) * 100. / (100. - t.previewOpts.size.size)) newContentHeight := int(float64(contentHeight) * 100. / (100. - t.previewOpts.size.size))
@ -2625,7 +2624,7 @@ func (t *Terminal) Loop() {
} }
refreshPreview := func(command string) { refreshPreview := func(command string) {
if len(command) > 0 && t.isPreviewEnabled() { if len(command) > 0 && t.canPreview() {
_, list := t.buildPlusList(command, false, false) _, list := t.buildPlusList(command, false, false)
t.cancelPreview() t.cancelPreview()
t.previewBox.Set(reqPreviewEnqueue, previewRequest{command, t.pwindow, t.evaluateScrollOffset(), list}) t.previewBox.Set(reqPreviewEnqueue, previewRequest{command, t.pwindow, t.evaluateScrollOffset(), list})
@ -2700,7 +2699,7 @@ func (t *Terminal) Loop() {
case reqFullRedraw: case reqFullRedraw:
wasHidden := t.pwindow == nil wasHidden := t.pwindow == nil
t.redraw() t.redraw()
if wasHidden && t.pwindow != nil { if wasHidden && t.hasPreviewWindow() {
refreshPreview(t.previewOpts.command) refreshPreview(t.previewOpts.command)
} }
case reqClose: case reqClose:
@ -2881,7 +2880,7 @@ func (t *Terminal) Loop() {
if act { if act {
t.activePreviewOpts.Toggle() t.activePreviewOpts.Toggle()
updatePreviewWindow(false) updatePreviewWindow(false)
if t.isPreviewEnabled() { if t.canPreview() {
valid, list := t.buildPlusList(t.previewOpts.command, false, false) valid, list := t.buildPlusList(t.previewOpts.command, false, false)
if valid { if valid {
t.cancelPreview() t.cancelPreview()
@ -2977,7 +2976,6 @@ func (t *Terminal) Loop() {
t.prompt, t.promptLen = t.parsePrompt(a.a) t.prompt, t.promptLen = t.parsePrompt(a.a)
req(reqPrompt) req(reqPrompt)
case actPreview: case actPreview:
t.previewer.enabled = true
updatePreviewWindow(true) updatePreviewWindow(true)
refreshPreview(a.a) refreshPreview(a.a)
case actRefreshPreview: case actRefreshPreview:
@ -3390,9 +3388,8 @@ func (t *Terminal) Loop() {
} }
case actChangePreview: case actChangePreview:
if t.previewOpts.command != a.a { if t.previewOpts.command != a.a {
t.previewer.enabled = len(a.a) > 0
updatePreviewWindow(false)
t.previewOpts.command = a.a t.previewOpts.command = a.a
updatePreviewWindow(false)
refreshPreview(t.previewOpts.command) refreshPreview(t.previewOpts.command)
} }
case actChangePreviewWindow: case actChangePreviewWindow:
@ -3412,7 +3409,7 @@ func (t *Terminal) Loop() {
if !currentPreviewOpts.sameLayout(t.previewOpts) { if !currentPreviewOpts.sameLayout(t.previewOpts) {
wasHidden := t.pwindow == nil wasHidden := t.pwindow == nil
updatePreviewWindow(false) updatePreviewWindow(false)
if wasHidden && t.pwindow != nil { if wasHidden && t.hasPreviewWindow() {
refreshPreview(t.previewOpts.command) refreshPreview(t.previewOpts.command)
} else { } else {
req(reqPreviewRefresh) req(reqPreviewRefresh)
@ -3489,12 +3486,10 @@ func (t *Terminal) Loop() {
req(reqList) req(reqList)
} }
if queryChanged { if queryChanged && t.canPreview() && len(t.previewOpts.command) > 0 {
if t.isPreviewEnabled() { _, _, q := hasPreviewFlags(t.previewOpts.command)
_, _, q := hasPreviewFlags(t.previewOpts.command) if q {
if q { t.version++
t.version++
}
} }
} }