From f6b19620566a299d8813ee51e02c26d21525d339 Mon Sep 17 00:00:00 2001
From: Junegunn Choi <junegunn.c@gmail.com>
Date: Fri, 1 Dec 2017 03:27:58 +0900
Subject: [PATCH] Inject $LINES and $COLUMNS when running preview command

Close #1168
---
 man/man1/fzf.1  | 7 +++++--
 src/terminal.go | 6 ++++++
 test/test_go.rb | 6 +++---
 3 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/man/man1/fzf.1 b/man/man1/fzf.1
index aea5fea7..89d7f93f 100644
--- a/man/man1/fzf.1
+++ b/man/man1/fzf.1
@@ -272,14 +272,17 @@ string, specify field index expressions between the braces (See \fBFIELD INDEX
 EXPRESSION\fR for the details).
 
 .RS
-e.g. \fBfzf --preview="head -$LINES {}"\fR
+e.g. \fBfzf --preview='head -$LINES {}'\fR
      \fBls -l | fzf --preview="echo user={3} when={-4..-2}; cat {-1}" --header-lines=1\fR
 
+fzf overrides \fB$LINES\fR and \fB$COLUMNS\fR so that they represent the exact
+size of the preview window.
+
 A placeholder expression starting with \fB+\fR flag will be replaced to the
 space-separated list of the selected lines (or the current line if no selection
 was made) individually quoted.
 
-e.g. \fBfzf --multi --preview="head -10 {+}"\fR
+e.g. \fBfzf --multi --preview='head -10 {+}'\fR
      \fBgit log --oneline | fzf --multi --preview 'git show {+1}'\fR
 
 Also, \fB{q}\fR is replaced to the current query string.
diff --git a/src/terminal.go b/src/terminal.go
index bc1c2ae9..2da2e4c6 100644
--- a/src/terminal.go
+++ b/src/terminal.go
@@ -1368,6 +1368,12 @@ func (t *Terminal) Loop() {
 					command := replacePlaceholder(t.preview.command,
 						t.ansi, t.delimiter, false, string(t.input), request)
 					cmd := util.ExecCommand(command)
+					if t.pwindow != nil {
+						env := os.Environ()
+						env = append(env, fmt.Sprintf("LINES=%d", t.pwindow.Height()))
+						env = append(env, fmt.Sprintf("COLUMNS=%d", t.pwindow.Width()))
+						cmd.Env = env
+					}
 					out, _ := cmd.CombinedOutput()
 					t.reqBox.Set(reqPreviewDisplay, string(out))
 				} else {
diff --git a/test/test_go.rb b/test/test_go.rb
index ec495c6a..b3dc1c04 100644
--- a/test/test_go.rb
+++ b/test/test_go.rb
@@ -1319,12 +1319,12 @@ class TestGoFZF < TestBase
   end
 
   def test_preview_hidden
-    tmux.send_keys %(seq 1000 | #{FZF} --preview 'echo {{}-{}}' --preview-window down:1:hidden --bind ?:toggle-preview), :Enter
+    tmux.send_keys %(seq 1000 | #{FZF} --preview 'echo {{}-{}-\\$LINES-\\$COLUMNS}' --preview-window down:1:hidden --bind ?:toggle-preview), :Enter
     tmux.until { |lines| lines[-1] == '>' }
     tmux.send_keys '?'
-    tmux.until { |lines| lines[-2].include?(' {1-1}') }
+    tmux.until { |lines| lines[-2].match?(/ {1-1-1-[0-9]+}/) }
     tmux.send_keys '555'
-    tmux.until { |lines| lines[-2].include?(' {555-555}') }
+    tmux.until { |lines| lines[-2].match?(/ {555-555-1-[0-9]+}/) }
     tmux.send_keys '?'
     tmux.until { |lines| lines[-1] == '> 555' }
   end
-- 
GitLab