tty-pager 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/Gemfile +1 -1
- data/README.md +16 -3
- data/lib/tty/pager/system.rb +16 -15
- data/lib/tty/pager/version.rb +1 -1
- data/spec/unit/system/available_spec.rb +5 -0
- data/spec/unit/system/command_exists_spec.rb +15 -0
- data/spec/unit/system/page_spec.rb +19 -10
- data/tasks/console.rake +1 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90d482658df231505821c563a80f8e47c1e57d9b
|
4
|
+
data.tar.gz: 6b2bf29fc1e0b6d3dfc60f2ff9819507dc35c62c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 891ef637078cc33f4a3821a4ed847d8703a353803bfb2183016a818cf09bb0bd9a42e813a206660654973d8adaf43ec9b9e86476938d47f9b75addd6f1f8052d
|
7
|
+
data.tar.gz: 4239ae39b7dbfe716db15869fc0768cc6f5c941d7161eba78f14991cae2b20671567c9282e73a0431a60df666bcbc6adddce2cbdeda57fef3a29ed79c3c5b790
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -31,13 +31,13 @@ Or install it yourself as:
|
|
31
31
|
|
32
32
|
## 1. Usage
|
33
33
|
|
34
|
-
The **TTY::Pager**
|
34
|
+
The **TTY::Pager** on initialization will choose the best available pager out of `SystemPager`, `BasicPager` or `NullPager`. If paging is disabled then a `NullPager` is used and content is simply printed out to stdout, otherwise a check is performed to find system executable to perform pagination natively with `SystemPager`. However, if no system executable is found, a `BasicPager` is used which is a pure Ruby implementation that will work with any ruby interpreter.
|
35
35
|
|
36
36
|
```ruby
|
37
37
|
pager = TTY::Pager.new
|
38
38
|
```
|
39
39
|
|
40
|
-
Then to perform actual content pagination invoke `page`
|
40
|
+
Then to perform actual content pagination invoke `page` method with the content to paginate as the argument:
|
41
41
|
|
42
42
|
```ruby
|
43
43
|
pager.page("Very long text...")
|
@@ -55,13 +55,26 @@ If you want to disable the pager pass the `:enabled` option:
|
|
55
55
|
pager = TTY::Pager.new enabled: false
|
56
56
|
```
|
57
57
|
|
58
|
-
For the `BasicPager` you can also pass a `:prompt` option to change the page break
|
58
|
+
For the `BasicPager` you can also pass a `:prompt` option to change the page break text:
|
59
59
|
|
60
60
|
```ruby
|
61
61
|
prompt = -> (page_num) { output.puts "Page -#{page_num}- Press enter to continue" }
|
62
62
|
pager = TTY::Pager::BasicPager.new prompt: prompt
|
63
63
|
```
|
64
64
|
|
65
|
+
By default the `SystemPager` will check the `PAGER` environment variable, if not set it will try one of the `less`, `more`, `cat`, `pager`. Therefore, if you wish to set your prefered pager you can either set up your shell like so:
|
66
|
+
|
67
|
+
```bash
|
68
|
+
PAGER=less
|
69
|
+
export PAGER
|
70
|
+
```
|
71
|
+
|
72
|
+
or set `PAGER` in Ruby script:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
ENV['PAGER']='less'
|
76
|
+
```
|
77
|
+
|
65
78
|
## Contributing
|
66
79
|
|
67
80
|
1. Fork it ( https://github.com/peter-murach/tty-pager/fork )
|
data/lib/tty/pager/system.rb
CHANGED
@@ -43,19 +43,19 @@ module TTY
|
|
43
43
|
# Use system command to page output text
|
44
44
|
#
|
45
45
|
# @example
|
46
|
-
#
|
46
|
+
# page('some long text...')
|
47
47
|
#
|
48
48
|
# @param [String] text
|
49
49
|
# the text to paginate
|
50
50
|
#
|
51
|
-
# @return [
|
51
|
+
# @return [Boolean]
|
52
|
+
# the success status of launching a process
|
52
53
|
#
|
53
54
|
# @api public
|
54
55
|
def page(text, &callback)
|
55
56
|
read_io, write_io = IO.pipe
|
56
57
|
|
57
|
-
|
58
|
-
# parent process
|
58
|
+
pid = Kernel.fork do
|
59
59
|
write_io.close
|
60
60
|
input.reopen(read_io)
|
61
61
|
read_io.close
|
@@ -68,12 +68,14 @@ module TTY
|
|
68
68
|
rescue SystemCallError
|
69
69
|
exit 1
|
70
70
|
end
|
71
|
-
else
|
72
|
-
# child process
|
73
|
-
read_io.close
|
74
|
-
write_io.write(text)
|
75
|
-
write_io.close
|
76
71
|
end
|
72
|
+
|
73
|
+
read_io.close
|
74
|
+
write_io.write(text)
|
75
|
+
write_io.close
|
76
|
+
|
77
|
+
_, status = Process.waitpid2(pid)
|
78
|
+
status.success?
|
77
79
|
end
|
78
80
|
|
79
81
|
private
|
@@ -86,7 +88,7 @@ module TTY
|
|
86
88
|
def self.executables
|
87
89
|
[ENV['GIT_PAGER'], ENV['PAGER'],
|
88
90
|
`git config --get-all core.pager`.split.first,
|
89
|
-
|
91
|
+
'less', 'more', 'cat', 'pager']
|
90
92
|
end
|
91
93
|
private_class_method :executables
|
92
94
|
|
@@ -104,7 +106,6 @@ module TTY
|
|
104
106
|
def self.command_exists?(command)
|
105
107
|
!TTY::Which.which(command).nil?
|
106
108
|
end
|
107
|
-
private_class_method :command_exists?
|
108
109
|
|
109
110
|
# The pager command to run
|
110
111
|
#
|
@@ -114,10 +115,10 @@ module TTY
|
|
114
115
|
# @api private
|
115
116
|
def pager_command(*commands)
|
116
117
|
@pager_command = if @pager_command && commands.empty?
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
118
|
+
@pager_command
|
119
|
+
else
|
120
|
+
self.class.available(*commands)
|
121
|
+
end
|
121
122
|
end
|
122
123
|
end # SystemPager
|
123
124
|
end # Pager
|
data/lib/tty/pager/version.rb
CHANGED
@@ -22,4 +22,9 @@ RSpec.describe TTY::Pager::SystemPager, '#available' do
|
|
22
22
|
allow(pager).to receive(:command_exists?).with('more') { true }
|
23
23
|
expect(pager.available('more')).to eql('more')
|
24
24
|
end
|
25
|
+
|
26
|
+
it "allows to query for available command" do
|
27
|
+
allow(pager).to receive(:available).with('less') { true }
|
28
|
+
expect(pager.available?('less')).to eq(true)
|
29
|
+
end
|
25
30
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
RSpec.describe TTY::Pager::SystemPager, '#command_exists?' do
|
4
|
+
subject(:pager) { described_class }
|
5
|
+
|
6
|
+
it "successfully checks command exists on the system" do
|
7
|
+
allow(TTY::Which).to receive(:which).with('less').and_return('/usr/bin/less')
|
8
|
+
expect(pager.command_exists?('less')).to eq(true)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "fails to check command exists on the system" do
|
12
|
+
allow(TTY::Which).to receive(:which).with('less').and_return(nil)
|
13
|
+
expect(pager.command_exists?('less')).to eq(false)
|
14
|
+
end
|
15
|
+
end
|
@@ -1,20 +1,29 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
3
|
RSpec.describe TTY::Pager::SystemPager, '.page' do
|
4
|
-
let(:input)
|
5
|
-
let(:output)
|
4
|
+
let(:input) { StringIO.new }
|
5
|
+
let(:output) { StringIO.new }
|
6
6
|
|
7
7
|
it "executes the pager command in a subprocess" do
|
8
|
-
text
|
9
|
-
pager
|
8
|
+
text = "I try all things, I achieve what I can.\n"
|
9
|
+
pager = described_class.new(output: output, input: input)
|
10
|
+
read_io = spy
|
11
|
+
write_io = spy
|
10
12
|
|
11
|
-
allow(
|
12
|
-
allow(Kernel).to receive(:fork).and_return(true)
|
13
|
-
allow(pager).to receive(:pager_command).and_return('less')
|
14
|
-
allow(IO).to receive(:select)
|
15
|
-
allow(input).to receive(:reopen)
|
13
|
+
allow(IO).to receive(:pipe).and_return([read_io, write_io])
|
16
14
|
|
17
|
-
|
15
|
+
allow(Kernel).to receive(:fork) do |&block|
|
16
|
+
allow(input).to receive(:reopen)
|
17
|
+
allow(IO).to receive(:select)
|
18
|
+
allow(pager).to receive(:pager_command).and_return('less')
|
19
|
+
allow(pager).to receive(:exec)
|
20
|
+
block.call
|
21
|
+
end.and_return(12345)
|
22
|
+
|
23
|
+
status = double(:status, :success? => true)
|
24
|
+
allow(Process).to receive(:waitpid2).with(12345).and_return([1, status])
|
25
|
+
|
26
|
+
expect(pager.page(text)).to eq(true)
|
18
27
|
|
19
28
|
expect(IO).to have_received(:select).with([input])
|
20
29
|
expect(pager).to have_received(:exec).with('less')
|
data/tasks/console.rake
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-pager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-screen
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- .rspec
|
79
79
|
- .ruby-version
|
80
80
|
- .travis.yml
|
81
|
+
- CHANGELOG.md
|
81
82
|
- Gemfile
|
82
83
|
- LICENSE.txt
|
83
84
|
- README.md
|
@@ -93,6 +94,7 @@ files:
|
|
93
94
|
- spec/unit/null/page_spec.rb
|
94
95
|
- spec/unit/page_spec.rb
|
95
96
|
- spec/unit/system/available_spec.rb
|
97
|
+
- spec/unit/system/command_exists_spec.rb
|
96
98
|
- spec/unit/system/page_spec.rb
|
97
99
|
- tasks/console.rake
|
98
100
|
- tasks/coverage.rake
|
@@ -129,5 +131,6 @@ test_files:
|
|
129
131
|
- spec/unit/null/page_spec.rb
|
130
132
|
- spec/unit/page_spec.rb
|
131
133
|
- spec/unit/system/available_spec.rb
|
134
|
+
- spec/unit/system/command_exists_spec.rb
|
132
135
|
- spec/unit/system/page_spec.rb
|
133
136
|
has_rdoc:
|