From 2db523a8be228211a0124578bcf0fb7ebd7f7518 Mon Sep 17 00:00:00 2001 From: Austin Kabiru Date: Fri, 10 Aug 2018 11:50:15 +0300 Subject: [PATCH] =?UTF-8?q?feat(output):=20Add=20pagination=20=F0=9F=93=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile.lock | 15 ++++++++++++++- README.md | 7 ++++++- fakerbot.gemspec | 2 ++ lib/fakerbot/commands/search.rb | 22 +++++++++++++++++++--- lib/fakerbot/version.rb | 2 +- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index bfc25b9..ac300ad 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,10 +1,12 @@ PATH remote: . specs: - fakerbot (0.2.3) + fakerbot (0.2.4) faker pastel (~> 0.7.2) thor (~> 0.20.0) + tty-pager + tty-screen tty-tree GEM @@ -65,15 +67,26 @@ GEM json (>= 1.8, < 3) simplecov-html (~> 0.10.0) simplecov-html (0.10.2) + strings (0.1.1) + unicode-display_width (~> 1.3.0) + unicode_utils (~> 1.4.0) term-ansicolor (1.6.0) tins (~> 1.0) thor (0.20.0) tins (1.16.3) tty-color (0.4.3) + tty-pager (0.11.0) + strings (~> 0.1.0) + tty-screen (~> 0.6.4) + tty-which (~> 0.3.0) + tty-screen (0.6.5) tty-tree (0.1.0) + tty-which (0.3.0) unf (0.1.4) unf_ext unf_ext (0.0.7.5) + unicode-display_width (1.3.3) + unicode_utils (1.4.0) PLATFORMS ruby diff --git a/README.md b/README.md index b5225c4..fbbadaf 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,8 @@ $ fakerbot search name ## New Features! :sunglasses: :dancers: - [x] List classes with methods E.g. Faker::FunnyName should list `.name`,`.first_name` e.t.c. -- [x] Expand search to Faker::Base sub classes i.e."fakerbot search email" should include `Faker::Internet.email` +- [x] Expand search to Faker::Base sub classes +- [x] Paginate results :book: ![screen shot 2018-08-09 at 20 21 06](https://user-images.githubusercontent.com/17295175/43914887-c9a99d84-9c11-11e8-9686-85f584412b27.jpg) @@ -53,6 +54,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To Bug reports and pull requests are welcome on GitHub at https://github.com/akabiru/fakerbot. 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. +## Credits + +Terminal awesomeness made easy by the [TTY Toolkit](http://piotrmurach.github.io/tty/). :beer: + ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/fakerbot.gemspec b/fakerbot.gemspec index 7c8bb8c..b7ada0e 100644 --- a/fakerbot.gemspec +++ b/fakerbot.gemspec @@ -22,6 +22,8 @@ Gem::Specification.new do |spec| spec.add_dependency "faker" spec.add_dependency "pastel", "~> 0.7.2" spec.add_dependency "thor", "~> 0.20.0" + spec.add_dependency "tty-pager" + spec.add_dependency "tty-screen" spec.add_dependency "tty-tree" spec.add_development_dependency "bundler", "~> 1.16" diff --git a/lib/fakerbot/commands/search.rb b/lib/fakerbot/commands/search.rb index a8288f0..3e754ee 100644 --- a/lib/fakerbot/commands/search.rb +++ b/lib/fakerbot/commands/search.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'pastel' +require 'tty/pager' require 'tty/tree' require 'fakerbot/bot' @@ -9,11 +10,26 @@ module Commands class Search def initialize(options) @options = options + @pager = TTY::Pager.new(command: 'less -R') + @screen = TTY::Screen end def execute(input) - result = FakerBot::Bot.find(input) - puts result.empty? ? not_found : tree(result).render + render FakerBot::Bot.find(input) + end + + private + + attr_reader :screen, :pager + + def render(result) + return not_found if result.empty? + output = tree(result) + if screen.height < output.nodes.size + pager.page output.render + else + puts output.render + end end def tree(input) @@ -27,7 +43,7 @@ def tree(input) end def not_found - "\n ☹️ Sorry, we couldn't find a match\n" + puts "\nSorry, we couldn't find a match 😢", "\n" end end end diff --git a/lib/fakerbot/version.rb b/lib/fakerbot/version.rb index 0741fa4..f074552 100644 --- a/lib/fakerbot/version.rb +++ b/lib/fakerbot/version.rb @@ -1,3 +1,3 @@ module FakerBot - VERSION = '0.2.3'.freeze + VERSION = '0.2.4'.freeze end