tty-pager 0.7.1 → 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 708a7b7003e1bba7b41ded5cfec547b1291bebcb
4
- data.tar.gz: deac81e148568067aff03907e587942c8592d4cf
3
+ metadata.gz: f49536d3b9e164578f6d5ad3b561eacc9c2d6115
4
+ data.tar.gz: d5db6acf71e24ae57853b0623f49c18f0b05aedc
5
5
  SHA512:
6
- metadata.gz: 94c0e42d639c96705d4d33809fb9c5071e0747aa6c2cf8f29c5782f2d25d184fb4939038d0b2dfa13a234818ac0c01b263c2034f6820e51571fdd441e6fe8783
7
- data.tar.gz: 156fd7831660b6ff9963e044d28c7c93df7f91cd65d5a6f27c0bb5ce34f7c63f50df712fc9d96b883c5e68fdce20c49736192aee516ed103cf4393c43e3f91ac
6
+ metadata.gz: f5c1a7349501d0ab69a3b626356e9c81af6af26c52e44d0fae16f0612e203ac649dec918b99e06dff930ec145acdd90db54a55d228f6648c2961053551d70c29
7
+ data.tar.gz: d6b107adf86a78772c73012e1f28c61c482a470d9fd90a46706bb92e8ca3cece43a90d19c8d86fadec66ebedc8d803ec0ee5d57615f20e607e3b35548c542074
data/.travis.yml CHANGED
@@ -10,7 +10,7 @@ rvm:
10
10
  - 2.1.10
11
11
  - 2.2.6
12
12
  - 2.3.3
13
- - 2.4.0
13
+ - 2.4.1
14
14
  - ruby-head
15
15
  - jruby-9000
16
16
  - jruby-head
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.8.0] - 2017-07-14
4
+
5
+ ### Added
6
+ * Add :command option to SystemPager to enforce choice of pagination tool
7
+ * Add Error type for specific error notifications
8
+
9
+ ### Changed
10
+ * Change SystemPager to prevent initialization if pager isn't supported
11
+
12
+ ### Fixed
13
+ * Fix BasicPager to take terminal width into account when displaying page break messages
14
+ * Fix SystemPager on Windows by detecting fork implementation
15
+
3
16
  ## [v0.7.1] - 2017-04-09
4
17
 
5
18
  ### Fixed
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at [email]. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/README.md CHANGED
@@ -32,10 +32,15 @@ Or install it yourself as:
32
32
 
33
33
  $ gem install tty-pager
34
34
 
35
- ## 1. Usage
35
+
36
+ ## Overview
36
37
 
37
38
  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.
38
39
 
40
+ ## 1. Usage
41
+
42
+ In order to let **TTY::Pager** pick the best paging mechanism automatically do:
43
+
39
44
  ```ruby
40
45
  pager = TTY::Pager.new
41
46
  ```
@@ -52,19 +57,44 @@ If you want to use specific pager you can do so by invoking it directly
52
57
  pager = TTY::Pager::BasicPager.new
53
58
  ```
54
59
 
55
- If you want to disable the pager pass the `:enabled` option:
60
+ ## 2. Interface
61
+
62
+ ### :enabled
63
+
64
+ If you want to disable the pager pass the `:enabled` option set to `false`:
56
65
 
57
66
  ```ruby
58
67
  pager = TTY::Pager.new enabled: false
59
68
  ```
60
69
 
61
- For the `BasicPager` you can also pass a `:prompt` option to change the page break text:
70
+ ### :width
71
+
72
+ The `BasicPager` allows to wrap content at given width:
73
+
74
+ ```ruby
75
+ pager = TTY::Pager::BasicPager.new width: 80
76
+ ```
77
+
78
+ ### :prompt
79
+
80
+ For the `BasicPager` you can pass a `:prompt` option to change the page break text:
62
81
 
63
82
  ```ruby
64
83
  prompt = -> (page_num) { output.puts "Page -#{page_num}- Press enter to continue" }
65
84
  pager = TTY::Pager::BasicPager.new prompt: prompt
66
85
  ```
67
86
 
87
+ ### :command
88
+
89
+ You can force `SystemPager` to always use a specific paging tool by passing the `:command` option:
90
+
91
+ ```ruby
92
+ TTY::Pager.new command; 'less -R'
93
+ TTY::Pager::SystemPager.new command: 'less -R'
94
+ ```
95
+
96
+ ### PAGER
97
+
68
98
  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:
69
99
 
70
100
  ```bash
@@ -80,6 +110,8 @@ ENV['PAGER']='less'
80
110
 
81
111
  ## Contributing
82
112
 
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-pager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
114
+
83
115
  1. Fork it ( https://github.com/piotrmurach/tty-pager/fork )
84
116
  2. Create your feature branch (`git checkout -b my-new-feature`)
85
117
  3. Commit your changes (`git commit -am 'Add some feature'`)
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ require 'tty-pager'
4
+
5
+ pager = TTY::Pager::BasicPager.new(width: 80)
6
+ file = File.join(File.dirname(__FILE__), 'temp.txt')
7
+ pager.page(File.read(file))
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # coding: utf-8
2
3
 
3
4
  require 'verse'
@@ -9,6 +10,36 @@ module TTY
9
10
  #
10
11
  # @api public
11
12
  class BasicPager < Pager
13
+ PAGE_BREAK = "\n--- Page -%s- " \
14
+ "Press enter/return to continue " \
15
+ "(or q to quit) ---".freeze
16
+
17
+ # Create a basic pager
18
+ #
19
+ # @option options [Integer] :height
20
+ # the terminal height
21
+ # @option options [Integer] :width
22
+ # the terminal width
23
+ #
24
+ # @api public
25
+ def initialize(options = {})
26
+ super
27
+ @height = options.fetch(:height) { page_height }
28
+ @width = options.fetch(:width) { page_width }
29
+ @prompt = options.fetch(:prompt) { default_prompt }
30
+ prompt_height = PAGE_BREAK.lines.to_a.size
31
+ @height -= prompt_height
32
+ end
33
+
34
+ # Default prompt for paging
35
+ #
36
+ # @return [Proc]
37
+ #
38
+ # @api private
39
+ def default_prompt
40
+ proc { |page_num| output.puts Verse.wrap(PAGE_BREAK % page_num, @width) }
41
+ end
42
+
12
43
  # Page text
13
44
  #
14
45
  # @api public
@@ -9,6 +9,22 @@ module TTY
9
9
  #
10
10
  # @api public
11
11
  class SystemPager < Pager
12
+ # Create a system pager
13
+ #
14
+ # @param [Hash] options
15
+ # @option options [String] :command
16
+ # the command to use for paging
17
+ #
18
+ # @api public
19
+ def initialize(options = {})
20
+ super
21
+ @pager_command = options[:command]
22
+ unless self.class.can?
23
+ raise TTY::Pager::Error, "#{self.class.name} cannot be used on your" \
24
+ " system. Try using BasicPager instead."
25
+ end
26
+ end
27
+
12
28
  # Find first available system command for paging
13
29
  #
14
30
  # @example Basic usage
@@ -24,9 +40,9 @@ module TTY
24
40
  # @api public
25
41
  def self.available(*commands)
26
42
  commands = commands.empty? ? executables : commands
27
- commands.
28
- compact.map(&:strip).reject(&:empty?).uniq.
29
- find { |cmd| command_exists?(cmd.split.first) }
43
+ commands
44
+ .compact.map(&:strip).reject(&:empty?).uniq
45
+ .find { |cmd| command_exists?(cmd.split.first) }
30
46
  end
31
47
 
32
48
  # Check if command is available
@@ -44,6 +60,28 @@ module TTY
44
60
  !available(*commands).nil?
45
61
  end
46
62
 
63
+ # Check if fork is supported
64
+ #
65
+ # @return [Boolean]
66
+ #
67
+ # @api public
68
+ def self.fork?
69
+ pid = fork {}
70
+ exit unless pid
71
+ true
72
+ rescue NotImplementedError
73
+ false
74
+ end
75
+
76
+ # Check if fork & comman exist
77
+ #
78
+ # @return [Boolean]
79
+ #
80
+ # @api public
81
+ def self.can?
82
+ self.fork? && self.available?
83
+ end
84
+
47
85
  # Use system command to page output text
48
86
  #
49
87
  # @example
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Pager
5
- VERSION = "0.7.1"
5
+ VERSION = "0.8.0"
6
6
  end # Pager
7
7
  end # TTY
data/lib/tty/pager.rb CHANGED
@@ -9,11 +9,7 @@ require_relative "pager/version"
9
9
 
10
10
  module TTY
11
11
  class Pager
12
- PROMPT_HEIGHT = 2
13
-
14
- PAGE_BREAK = "\n--- Page -%s- " \
15
- "Press enter/return to continue " \
16
- "(or q to quit) ---".freeze
12
+ Error = Class.new(StandardError)
17
13
 
18
14
  # Create a pager
19
15
  #
@@ -24,38 +20,20 @@ module TTY
24
20
  # the object to send input to
25
21
  # @option options [IO] :output
26
22
  # the object to send output to
27
- # @option options [Integer] :height
28
- # the terminal height
29
- # @option options [Integer] :width
30
- # the terminal width
31
23
  # @option options [Boolean] :enabled
32
24
  # disable/enable text paging
33
25
  #
34
26
  # @api public
35
27
  def initialize(options = {})
36
- @height = options.fetch(:height) { page_height }
37
- @width = options.fetch(:width) { page_width }
38
28
  @input = options.fetch(:input) { $stdin }
39
29
  @output = options.fetch(:output) { $stdout }
40
30
  @enabled = options.fetch(:enabled) { true }
41
- @prompt = options.fetch(:prompt) { default_prompt }
42
- @height -= PROMPT_HEIGHT
43
- @output = output
44
31
 
45
32
  if self.class == TTY::Pager
46
- @pager = find_available
33
+ @pager = find_available(options)
47
34
  end
48
35
  end
49
36
 
50
- # Default prompt for paging
51
- #
52
- # @return [Proc]
53
- #
54
- # @api private
55
- def default_prompt
56
- proc { |page_num| output.puts PAGE_BREAK % page_num }
57
- end
58
-
59
37
  # Check if pager is enabled
60
38
  #
61
39
  # @return [Boolean]
@@ -111,13 +89,13 @@ module TTY
111
89
  # is pure Ruby implementation.
112
90
  #
113
91
  # @api private
114
- def find_available
92
+ def find_available(options)
115
93
  if !enabled?
116
94
  NullPager.new
117
- elsif SystemPager.available? && !Pager.jruby?
118
- SystemPager.new
95
+ elsif !Pager.jruby? && SystemPager.can?
96
+ SystemPager.new(options)
119
97
  else
120
- BasicPager.new
98
+ BasicPager.new(options)
121
99
  end
122
100
  end
123
101
 
@@ -31,7 +31,14 @@ RSpec.describe TTY::Pager::BasicPager, '.page' do
31
31
  "any map; ",
32
32
  "true ",
33
33
  "",
34
- "--- Page -1- Press enter/return to continue (or q to quit) ---",
34
+ "--- Page ",
35
+ "-1- Press ",
36
+ "e",
37
+ "nter/retur",
38
+ "n to ",
39
+ "continue ",
40
+ "(or q to ",
41
+ "quit) ---",
35
42
  "places ",
36
43
  "never are.\n"
37
44
  ].join("\n"))
@@ -111,7 +118,7 @@ RSpec.describe TTY::Pager::BasicPager, '.page' do
111
118
  input << "\n\n\n"
112
119
  input.rewind
113
120
  pager = described_class.new(output: output, input: input,
114
- width: 1, height: 5)
121
+ width: 80, height: 5)
115
122
  pager.page(text)
116
123
  expect(output.string).to eq([
117
124
  "a",
@@ -16,7 +16,8 @@ RSpec.describe TTY::Pager, '.page' do
16
16
 
17
17
  it "selects basic pager on non tty systems" do
18
18
  basic_pager = spy(:basic_pager)
19
- allow(TTY::Pager::SystemPager).to receive(:available?) { false }
19
+ allow(described_class).to receive(:jruby?) { false }
20
+ allow(TTY::Pager::SystemPager).to receive(:can?) { false }
20
21
  allow(TTY::Pager::BasicPager).to receive(:new) { basic_pager }
21
22
 
22
23
  pager = described_class.new
@@ -28,8 +29,8 @@ RSpec.describe TTY::Pager, '.page' do
28
29
 
29
30
  it "selects system pager on systems with tty" do
30
31
  system_pager = spy(:system_pager)
31
- allow(TTY::Pager::SystemPager).to receive(:available?) { true }
32
32
  allow(described_class).to receive(:jruby?) { false }
33
+ allow(TTY::Pager::SystemPager).to receive(:can?) { true }
33
34
  allow(TTY::Pager::SystemPager).to receive(:new) { system_pager }
34
35
 
35
36
  pager = described_class.new
@@ -33,7 +33,8 @@ RSpec.describe TTY::Pager::SystemPager, '#available' do
33
33
 
34
34
  it "does not error" do
35
35
  allow(pager).to receive(:executables).and_return(execs)
36
- expect(pager.available).to eql("less")
36
+ allow(pager).to receive(:command_exists?).with('less') { true }
37
+ expect(pager.available).to eql('less')
37
38
  end
38
39
  end
39
40
 
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe TTY::Pager::SystemPager, '#new' do
4
+ it "raises error if system paging is not supported" do
5
+ allow(TTY::Pager::SystemPager).to receive(:can?).and_return(false)
6
+ expect {
7
+ TTY::Pager::SystemPager.new
8
+ }.to raise_error(TTY::Pager::Error, "TTY::Pager::SystemPager cannot be used on your system. Try using BasicPager instead.")
9
+ end
10
+ end
@@ -6,6 +6,7 @@ RSpec.describe TTY::Pager::SystemPager, '.page' do
6
6
 
7
7
  it "executes the pager command in a subprocess" do
8
8
  text = "I try all things, I achieve what I can.\n"
9
+ allow(TTY::Pager::SystemPager).to receive(:can?).and_return(true)
9
10
  pager = described_class.new(output: output, input: input)
10
11
  read_io = spy
11
12
  write_io = spy
data/tty-pager.gemspec CHANGED
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency 'bundler', '>= 1.5.0', '< 2.0'
26
26
  spec.add_development_dependency 'rake'
27
- spec.add_development_dependency 'rspec', '~> 3.5.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.6.0'
28
28
  end
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.7.1
4
+ version: 0.8.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: 2017-04-09 00:00:00.000000000 Z
11
+ date: 2017-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-screen
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: 3.5.0
95
+ version: 3.6.0
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: 3.5.0
102
+ version: 3.6.0
103
103
  description: Terminal output paging in a cross-platform way supporting all major ruby
104
104
  interpreters.
105
105
  email:
@@ -112,11 +112,13 @@ files:
112
112
  - ".rspec"
113
113
  - ".travis.yml"
114
114
  - CHANGELOG.md
115
+ - CODE_OF_CONDUCT.md
115
116
  - Gemfile
116
117
  - LICENSE.txt
117
118
  - README.md
118
119
  - Rakefile
119
120
  - appveyor.yml
121
+ - examples/basic_pager.rb
120
122
  - examples/system_pager.rb
121
123
  - examples/temp.txt
122
124
  - lib/tty-pager.rb
@@ -131,6 +133,7 @@ files:
131
133
  - spec/unit/page_spec.rb
132
134
  - spec/unit/system/available_spec.rb
133
135
  - spec/unit/system/command_exists_spec.rb
136
+ - spec/unit/system/new_spec.rb
134
137
  - spec/unit/system/page_spec.rb
135
138
  - tasks/console.rake
136
139
  - tasks/coverage.rake
@@ -168,5 +171,6 @@ test_files:
168
171
  - spec/unit/page_spec.rb
169
172
  - spec/unit/system/available_spec.rb
170
173
  - spec/unit/system/command_exists_spec.rb
174
+ - spec/unit/system/new_spec.rb
171
175
  - spec/unit/system/page_spec.rb
172
176
  has_rdoc: