This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpager.rb
More file actions
68 lines (56 loc) · 1.75 KB
/
pager.rb
File metadata and controls
68 lines (56 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
require "stringio"
module Git
module Pkgs
module Pager
# Returns the pager command following git's precedence:
# 1. GIT_PAGER environment variable
# 2. core.pager git config
# 3. PAGER environment variable
# 4. less as fallback
def git_pager
return ENV["GIT_PAGER"] if ENV["GIT_PAGER"] && !ENV["GIT_PAGER"].empty?
config_pager = `git config --get core.pager`.chomp
return config_pager unless config_pager.empty?
return ENV["PAGER"] if ENV["PAGER"] && !ENV["PAGER"].empty?
"less -FRSX"
end
# Returns true if paging is disabled (pager set to empty string or 'cat')
def pager_disabled?
pager = git_pager
pager.empty? || pager == "cat"
end
# Captures output from a block and sends it through the pager.
# Falls back to direct output if:
# - stdout is not a TTY
# - pager is disabled
# - pager command fails
def paginate
if !$stdout.tty? || pager_disabled? || paging_disabled?
yield
return
end
output = StringIO.new
old_stdout = $stdout
$stdout = output
begin
yield
ensure
$stdout = old_stdout
end
content = output.string
return if content.empty?
IO.popen(git_pager, "w") { |io| io.write(content) }
rescue Errno::EPIPE
# User quit pager early
rescue Errno::ENOENT
# Pager command not found, fall back to direct output
print content
end
# Check if paging was disabled via --no-pager option
def paging_disabled?
defined?(@options) && @options.is_a?(Hash) && @options[:no_pager]
end
end
end
end