tty 0.0.8 → 0.0.9
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.
- data/lib/tty.rb +3 -3
- data/lib/tty/shell.rb +13 -0
- data/lib/tty/support/equatable.rb +2 -6
- data/lib/tty/text.rb +16 -0
- data/lib/tty/text/distance.rb +73 -0
- data/lib/tty/version.rb +1 -1
- data/spec/tty/table/field/equality_spec.rb +2 -2
- data/spec/tty/table/renderer/basic/separator_spec.rb +0 -22
- data/spec/tty/text/distance/distance_spec.rb +63 -0
- data/spec/tty/text/distance/initialize_spec.rb +14 -0
- data/spec/tty/text/distance_spec.rb +11 -0
- data/spec/tty/text/truncation/truncate_spec.rb +2 -2
- metadata +17 -10
    
        data/lib/tty.rb
    CHANGED
    
    | @@ -37,13 +37,13 @@ require 'tty/terminal/home' | |
| 37 37 |  | 
| 38 38 | 
             
            require 'tty/system/which'
         | 
| 39 39 |  | 
| 40 | 
            -
            require 'tty/text/ | 
| 40 | 
            +
            require 'tty/text/distance'
         | 
| 41 41 | 
             
            require 'tty/text/truncation'
         | 
| 42 | 
            +
            require 'tty/text/wrapping'
         | 
| 42 43 |  | 
| 43 | 
            -
            require 'tty/table/field'
         | 
| 44 44 | 
             
            require 'tty/table/header'
         | 
| 45 45 | 
             
            require 'tty/table/row'
         | 
| 46 | 
            -
            require 'tty/table/ | 
| 46 | 
            +
            require 'tty/table/field'
         | 
| 47 47 |  | 
| 48 48 | 
             
            require 'tty/table/border'
         | 
| 49 49 | 
             
            require 'tty/table/border_dsl'
         | 
    
        data/lib/tty/shell.rb
    CHANGED
    
    | @@ -135,6 +135,19 @@ module TTY | |
| 135 135 | 
             
                  args.each { |message| say message, options.merge(:color => :red) }
         | 
| 136 136 | 
             
                end
         | 
| 137 137 |  | 
| 138 | 
            +
                def suggest(message, possibilities)
         | 
| 139 | 
            +
                  distances = Hash.new { |hash, key| hash[key] = [] }
         | 
| 140 | 
            +
             | 
| 141 | 
            +
                  possibilities.each do |possibility|
         | 
| 142 | 
            +
                    distances[Text.distance(message, possibility)] << possibility
         | 
| 143 | 
            +
                  end
         | 
| 144 | 
            +
             | 
| 145 | 
            +
                  minimum_distance = distances.keys.min
         | 
| 146 | 
            +
                  if minimum_distance < 4
         | 
| 147 | 
            +
                  else
         | 
| 148 | 
            +
                  end
         | 
| 149 | 
            +
                end
         | 
| 150 | 
            +
             | 
| 138 151 | 
             
                # Print a table to shell.
         | 
| 139 152 | 
             
                #
         | 
| 140 153 | 
             
                # @return [undefined]
         | 
| @@ -130,7 +130,7 @@ module TTY | |
| 130 130 | 
             
                  #
         | 
| 131 131 | 
             
                  # @api public
         | 
| 132 132 | 
             
                  def eql?(other)
         | 
| 133 | 
            -
                    instance_of?(other.class)  | 
| 133 | 
            +
                    instance_of?(other.class) && compare?(__method__, other)
         | 
| 134 134 | 
             
                  end
         | 
| 135 135 |  | 
| 136 136 | 
             
                  # Compare two objects for equality based on their value
         | 
| @@ -143,11 +143,7 @@ module TTY | |
| 143 143 | 
             
                  #
         | 
| 144 144 | 
             
                  # @api public
         | 
| 145 145 | 
             
                  def ==(other)
         | 
| 146 | 
            -
                     | 
| 147 | 
            -
                    p self
         | 
| 148 | 
            -
                    p other
         | 
| 149 | 
            -
                    return false unless self.class <=> other.class
         | 
| 150 | 
            -
                    compare?(__method__, other)
         | 
| 146 | 
            +
                    kind_of?(other.class) && compare?(__method__, other)
         | 
| 151 147 | 
             
                  end
         | 
| 152 148 |  | 
| 153 149 | 
             
                end # Methods
         | 
    
        data/lib/tty/text.rb
    CHANGED
    
    | @@ -15,6 +15,22 @@ module TTY | |
| 15 15 | 
             
                def split_mode
         | 
| 16 16 | 
             
                end
         | 
| 17 17 |  | 
| 18 | 
            +
                # Calculate the distance between strings
         | 
| 19 | 
            +
                #
         | 
| 20 | 
            +
                # @param [String] first
         | 
| 21 | 
            +
                #   the first string for comparison
         | 
| 22 | 
            +
                #
         | 
| 23 | 
            +
                # @param [String] second
         | 
| 24 | 
            +
                #   the second string for comparison
         | 
| 25 | 
            +
                #
         | 
| 26 | 
            +
                # @example
         | 
| 27 | 
            +
                #   distance("which", "witch")
         | 
| 28 | 
            +
                #   # => 2
         | 
| 29 | 
            +
                #
         | 
| 30 | 
            +
                def self.distance(first, second, *args)
         | 
| 31 | 
            +
                  Distance.new(first, second, *args).distance
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 18 34 | 
             
                # Wrap a text into lines no longer than provided length
         | 
| 19 35 | 
             
                #
         | 
| 20 36 | 
             
                # @param [String] text
         | 
| @@ -0,0 +1,73 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module TTY
         | 
| 4 | 
            +
              class Text
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                # A class responsible for string comparison
         | 
| 7 | 
            +
                class Distance
         | 
| 8 | 
            +
                  include Unicode
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  attr_reader :first
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  attr_reader :second
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  # Initalize a Distance
         | 
| 15 | 
            +
                  #
         | 
| 16 | 
            +
                  # @param [String] first
         | 
| 17 | 
            +
                  #   the first string for comparision
         | 
| 18 | 
            +
                  #
         | 
| 19 | 
            +
                  # @param [String] second
         | 
| 20 | 
            +
                  #  the second string for comparison
         | 
| 21 | 
            +
                  #
         | 
| 22 | 
            +
                  # @api private
         | 
| 23 | 
            +
                  def initialize(first, second, *args)
         | 
| 24 | 
            +
                    options = Utils.extract_options!(args)
         | 
| 25 | 
            +
                    @first = first.to_s
         | 
| 26 | 
            +
                    @second = second.to_s
         | 
| 27 | 
            +
                    # TODO: add option to ignore case
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                  # Calculate the optimal string alignment distance
         | 
| 31 | 
            +
                  #
         | 
| 32 | 
            +
                  # @api private
         | 
| 33 | 
            +
                  def distance
         | 
| 34 | 
            +
                    distances = []
         | 
| 35 | 
            +
                    rows      = first.length
         | 
| 36 | 
            +
                    cols      = second.length
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                    0.upto(rows) do |index|
         | 
| 39 | 
            +
                      distances << [index] + [0] * cols
         | 
| 40 | 
            +
                    end
         | 
| 41 | 
            +
                    distances[0] = 0.upto(cols).to_a
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                    1.upto(rows) do |first_index|
         | 
| 44 | 
            +
                      1.upto(cols) do |second_index|
         | 
| 45 | 
            +
                        first_char  = first[first_index - 1]
         | 
| 46 | 
            +
                        second_char = second[second_index - 1]
         | 
| 47 | 
            +
                        cost        = first_char == second_char ? 0 : 1
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                        distances[first_index][second_index] = [
         | 
| 50 | 
            +
                          distances[first_index - 1][second_index], # deletion
         | 
| 51 | 
            +
                          distances[first_index][second_index - 1],     # insertion
         | 
| 52 | 
            +
                          distances[first_index - 1][second_index - 1]  # substitution
         | 
| 53 | 
            +
                        ].min + cost
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                        if first_index > 1 && second_index > 1
         | 
| 56 | 
            +
                          first_previous_char = first[first_index - 2]
         | 
| 57 | 
            +
                          second_previous_char = second[second_index - 2]
         | 
| 58 | 
            +
                          if first_char == second_previous_char && second_char == first_previous_char
         | 
| 59 | 
            +
                            distances[first_index][second_index] = [
         | 
| 60 | 
            +
                              distances[first_index][second_index],
         | 
| 61 | 
            +
                              distances[first_index - 2][second_index - 2] + 1 # transposition
         | 
| 62 | 
            +
                            ].min
         | 
| 63 | 
            +
                          end
         | 
| 64 | 
            +
                        end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                      end
         | 
| 67 | 
            +
                    end
         | 
| 68 | 
            +
                    distances[rows][cols]
         | 
| 69 | 
            +
                  end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                end # Distance
         | 
| 72 | 
            +
              end # Text
         | 
| 73 | 
            +
            end # TTY
         | 
    
        data/lib/tty/version.rb
    CHANGED
    
    
| @@ -31,10 +31,10 @@ describe TTY::Table::Field, '#==' do | |
| 31 31 | 
             
              context 'with an equivalent object of subclass' do
         | 
| 32 32 | 
             
                let(:other) { Class.new(described_class).new(value) }
         | 
| 33 33 |  | 
| 34 | 
            -
                it { should  | 
| 34 | 
            +
                it { should be_false }
         | 
| 35 35 |  | 
| 36 36 | 
             
                it 'is symmetric' do
         | 
| 37 | 
            -
                   | 
| 37 | 
            +
                  should_not eql(other == object)
         | 
| 38 38 | 
             
                end
         | 
| 39 39 | 
             
              end
         | 
| 40 40 |  | 
| @@ -55,28 +55,6 @@ describe TTY::Table, 'with separator' do | |
| 55 55 | 
             
                    +--+--+--+
         | 
| 56 56 | 
             
                  EOS
         | 
| 57 57 | 
             
                end
         | 
| 58 | 
            -
             | 
| 59 | 
            -
                context 'with specific separator' do
         | 
| 60 | 
            -
                  let(:header) { ['h1', 'h2'] }
         | 
| 61 | 
            -
                  let(:rows) { [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']] }
         | 
| 62 | 
            -
             | 
| 63 | 
            -
                  it 'custom separator' do
         | 
| 64 | 
            -
                    pending
         | 
| 65 | 
            -
                    table.border.separator= [2]
         | 
| 66 | 
            -
                    table.to_s.should == <<-EOS.normalize
         | 
| 67 | 
            -
                      +--+--+
         | 
| 68 | 
            -
                      |h1|h2|
         | 
| 69 | 
            -
                      +--+--+
         | 
| 70 | 
            -
                      |a1|a2|
         | 
| 71 | 
            -
                      |b1|b2|
         | 
| 72 | 
            -
                      +--+--+
         | 
| 73 | 
            -
                      |c1|c2|
         | 
| 74 | 
            -
                      +--+--+
         | 
| 75 | 
            -
                    EOS
         | 
| 76 | 
            -
                  end
         | 
| 77 | 
            -
             | 
| 78 | 
            -
                end
         | 
| 79 | 
            -
             | 
| 80 58 | 
             
              end
         | 
| 81 59 |  | 
| 82 60 | 
             
              context 'when unicode' do
         | 
| @@ -0,0 +1,63 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'spec_helper'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe TTY::Text::Distance, '#distance' do
         | 
| 6 | 
            +
              let(:object) { described_class.new(*strings) }
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              subject { object.distance }
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              context 'when nil' do
         | 
| 11 | 
            +
                let(:strings) { [nil, nil] }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                it { should eql(0) }
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              context 'when empty' do
         | 
| 17 | 
            +
                let(:strings) { ['', ''] }
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                it { should eql(0) }
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              context 'with one non empty' do
         | 
| 23 | 
            +
                let(:strings) { ['abc', ''] }
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                it { should eql(3) }
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              context 'when similar' do
         | 
| 29 | 
            +
                let(:strings) { ['abc', 'abc'] }
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                it { should eql(0) }
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              context 'when similar' do
         | 
| 35 | 
            +
                let(:strings) { ['abc', 'acb'] }
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                it { should eql(1) }
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              context 'when end similar' do
         | 
| 41 | 
            +
                let(:strings) { ['saturday', 'sunday'] }
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                it { should eql(3) }
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              context 'when contain similar' do
         | 
| 47 | 
            +
                let(:strings) { ['which', 'witch'] }
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                it { should eql(2) }
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              context 'when similar' do
         | 
| 53 | 
            +
                let(:strings) { ['smellyfish','jellyfish'] }
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                it { should eql(2) }
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
              context 'when unicode' do
         | 
| 59 | 
            +
                let(:strings) { ['マラソン五輪代表', 'ララソン五輪代表'] }
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                it { should eql(1) }
         | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
            end
         | 
| @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'spec_helper'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe TTY::Text::Distance, '#initialize' do
         | 
| 6 | 
            +
              let(:strings) { ['abc', 'acb'] }
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              subject { described_class.new(*strings) }
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              its(:first) { should == strings.first }
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              its(:second) { should == strings.last }
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            end # initialize
         | 
| @@ -3,12 +3,12 @@ | |
| 3 3 | 
             
            require 'spec_helper'
         | 
| 4 4 |  | 
| 5 5 | 
             
            describe TTY::Text::Truncation, '#truncate' do
         | 
| 6 | 
            -
              let(: | 
| 6 | 
            +
              let(:object) { described_class.new(text, options) }
         | 
| 7 7 | 
             
              let(:separator) { nil }
         | 
| 8 8 | 
             
              let(:trailing) { '…' }
         | 
| 9 9 | 
             
              let(:options) { { :length => length, :separator => separator, :trailing => trailing }  }
         | 
| 10 10 |  | 
| 11 | 
            -
              subject {  | 
| 11 | 
            +
              subject { object.truncate }
         | 
| 12 12 |  | 
| 13 13 | 
             
              context 'unicode support' do
         | 
| 14 14 | 
             
                let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: tty
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.9
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,11 +9,11 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2013- | 
| 12 | 
            +
            date: 2013-06-11 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rspec
         | 
| 16 | 
            -
              requirement: & | 
| 16 | 
            +
              requirement: &2156006680 !ruby/object:Gem::Requirement
         | 
| 17 17 | 
             
                none: false
         | 
| 18 18 | 
             
                requirements:
         | 
| 19 19 | 
             
                - - ! '>='
         | 
| @@ -21,10 +21,10 @@ dependencies: | |
| 21 21 | 
             
                    version: '0'
         | 
| 22 22 | 
             
              type: :development
         | 
| 23 23 | 
             
              prerelease: false
         | 
| 24 | 
            -
              version_requirements: * | 
| 24 | 
            +
              version_requirements: *2156006680
         | 
| 25 25 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 26 26 | 
             
              name: rake
         | 
| 27 | 
            -
              requirement: & | 
| 27 | 
            +
              requirement: &2156005760 !ruby/object:Gem::Requirement
         | 
| 28 28 | 
             
                none: false
         | 
| 29 29 | 
             
                requirements:
         | 
| 30 30 | 
             
                - - ! '>='
         | 
| @@ -32,10 +32,10 @@ dependencies: | |
| 32 32 | 
             
                    version: '0'
         | 
| 33 33 | 
             
              type: :development
         | 
| 34 34 | 
             
              prerelease: false
         | 
| 35 | 
            -
              version_requirements: * | 
| 35 | 
            +
              version_requirements: *2156005760
         | 
| 36 36 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 37 37 | 
             
              name: yard
         | 
| 38 | 
            -
              requirement: & | 
| 38 | 
            +
              requirement: &2156004720 !ruby/object:Gem::Requirement
         | 
| 39 39 | 
             
                none: false
         | 
| 40 40 | 
             
                requirements:
         | 
| 41 41 | 
             
                - - ! '>='
         | 
| @@ -43,10 +43,10 @@ dependencies: | |
| 43 43 | 
             
                    version: '0'
         | 
| 44 44 | 
             
              type: :development
         | 
| 45 45 | 
             
              prerelease: false
         | 
| 46 | 
            -
              version_requirements: * | 
| 46 | 
            +
              version_requirements: *2156004720
         | 
| 47 47 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 48 48 | 
             
              name: benchmark_suite
         | 
| 49 | 
            -
              requirement: & | 
| 49 | 
            +
              requirement: &2156003760 !ruby/object:Gem::Requirement
         | 
| 50 50 | 
             
                none: false
         | 
| 51 51 | 
             
                requirements:
         | 
| 52 52 | 
             
                - - ! '>='
         | 
| @@ -54,7 +54,7 @@ dependencies: | |
| 54 54 | 
             
                    version: '0'
         | 
| 55 55 | 
             
              type: :development
         | 
| 56 56 | 
             
              prerelease: false
         | 
| 57 | 
            -
              version_requirements: * | 
| 57 | 
            +
              version_requirements: *2156003760
         | 
| 58 58 | 
             
            description: Toolbox for developing CLI clients
         | 
| 59 59 | 
             
            email:
         | 
| 60 60 | 
             
            - ''
         | 
| @@ -127,6 +127,7 @@ files: | |
| 127 127 | 
             
            - lib/tty/terminal/echo.rb
         | 
| 128 128 | 
             
            - lib/tty/terminal/home.rb
         | 
| 129 129 | 
             
            - lib/tty/text.rb
         | 
| 130 | 
            +
            - lib/tty/text/distance.rb
         | 
| 130 131 | 
             
            - lib/tty/text/truncation.rb
         | 
| 131 132 | 
             
            - lib/tty/text/wrapping.rb
         | 
| 132 133 | 
             
            - lib/tty/vector.rb
         | 
| @@ -238,6 +239,9 @@ files: | |
| 238 239 | 
             
            - spec/tty/terminal/echo_spec.rb
         | 
| 239 240 | 
             
            - spec/tty/terminal/home_spec.rb
         | 
| 240 241 | 
             
            - spec/tty/terminal/size_spec.rb
         | 
| 242 | 
            +
            - spec/tty/text/distance/distance_spec.rb
         | 
| 243 | 
            +
            - spec/tty/text/distance/initialize_spec.rb
         | 
| 244 | 
            +
            - spec/tty/text/distance_spec.rb
         | 
| 241 245 | 
             
            - spec/tty/text/truncate_spec.rb
         | 
| 242 246 | 
             
            - spec/tty/text/truncation/initialize_spec.rb
         | 
| 243 247 | 
             
            - spec/tty/text/truncation/truncate_spec.rb
         | 
| @@ -381,6 +385,9 @@ test_files: | |
| 381 385 | 
             
            - spec/tty/terminal/echo_spec.rb
         | 
| 382 386 | 
             
            - spec/tty/terminal/home_spec.rb
         | 
| 383 387 | 
             
            - spec/tty/terminal/size_spec.rb
         | 
| 388 | 
            +
            - spec/tty/text/distance/distance_spec.rb
         | 
| 389 | 
            +
            - spec/tty/text/distance/initialize_spec.rb
         | 
| 390 | 
            +
            - spec/tty/text/distance_spec.rb
         | 
| 384 391 | 
             
            - spec/tty/text/truncate_spec.rb
         | 
| 385 392 | 
             
            - spec/tty/text/truncation/initialize_spec.rb
         | 
| 386 393 | 
             
            - spec/tty/text/truncation/truncate_spec.rb
         |