ansi-select 0.2.4 → 0.2.5

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: 57447ede8f4f47c5c3d688e5bba3955d526c42be
4
- data.tar.gz: 233e93ab7cc78065f77bbeffd8e37f33036cb87b
3
+ metadata.gz: d261e8c4cb86463bfba558e2c3104c30679d5a02
4
+ data.tar.gz: 43851d45c17b1a4da2cce1da10952cceeea302ef
5
5
  SHA512:
6
- metadata.gz: 037cf9989e8c19b142355f9951fd6c5bc0679ce4677adc588f0dca34cad3ddc480158d035ea0398574e2df98b875f849ad2e44f2567c7abd2c439b65735a4a86
7
- data.tar.gz: 2c0a5b1562c9f7164bc19b486c3f187a54cc8a7605fff55ceb9ee9df8cb64dd6d3461defb902442954caacda5b70c4f5b625c7abe78d5cdba3fb08bdcde225ff
6
+ metadata.gz: 691c63cfcfad6589ad5d423e8e8fb3b6c204054a998a841728c5bb63dfd8260296fcfc09f5094f4e879fc0ba881cdc7c7a2fd08fbb870fd6a56d0c3e21159852
7
+ data.tar.gz: 3f5777142597d125447659e280377941481d78559863b12f5180df015de50e58c417ad02c9af6e6521a5c3daec06f68ba06f58b55153019267031089b569c758
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 0.2.5
2
+
3
+ * Allow to preselect options.
4
+ * Format columns.
5
+
1
6
  ### 0.2.4
2
7
 
3
8
  * Ability to pass a custom formatter.
data/bin/ansi-select CHANGED
File without changes
data/lib/ansi/selector.rb CHANGED
@@ -2,22 +2,24 @@ module Ansi
2
2
  class Selector
3
3
  # @param [Array<Object>] options
4
4
  # @param [Proc] formatter
5
+ # @param [Fixnum] preselected
5
6
  #
6
7
  # @return [Object] option
7
- def self.select(options, formatter = default_formatter)
8
+ def self.select(options, formatter: default_formatter, preselected: 0)
8
9
  require_relative "selector/single_impl"
9
10
 
10
- SingleImpl.new(options, formatter).select
11
+ SingleImpl.new(options, formatter, preselected).select
11
12
  end
12
13
 
13
14
  # @param [Array<Object>] options
14
15
  # @param [Proc] formatter
16
+ # @param [Array<Fixnum>] preselected
15
17
  #
16
18
  # @return [Array<Object>] option
17
- def self.multi_select(options, formatter = default_formatter)
19
+ def self.multi_select(options, formatter: default_formatter, preselected: [])
18
20
  require_relative "selector/multi_impl"
19
21
 
20
- MultiImpl.new(options, formatter).select
22
+ MultiImpl.new(options, formatter, preselected).select
21
23
  end
22
24
 
23
25
  private
@@ -11,12 +11,20 @@ module Ansi
11
11
  carriage_return_key: `tput cr`
12
12
  }
13
13
 
14
- def initialize(options, formatter)
14
+ def initialize(options, formatter, preselected)
15
15
  @options = options
16
16
  @formatter = formatter
17
-
18
17
  @highlighted_line_index = 0
19
18
  @cursor_line_index = 0
19
+
20
+ # Converts options to column-aligned strings.
21
+ formatted = @options.map(&@formatter).map(&method(:Array))
22
+ @formatted ||= formatted.map do |f|
23
+ f.map.with_index do |part, column|
24
+ width = formatted.map { |fm| fm[column] }.compact.map(&:size).max
25
+ part.to_s.ljust(width)
26
+ end.join(' ')
27
+ end
20
28
  end
21
29
 
22
30
  def select
@@ -88,11 +96,12 @@ module Ansi
88
96
  # @param [Boolean] highlight
89
97
  def print_line(index, highlight)
90
98
  go_to_line(index)
99
+ text = prefix(index) + @formatted[index]
91
100
 
92
101
  if highlight
93
- tty.print(CODES[:standout_mode] + prefix(index) + @formatter.call(@options[index]) + CODES[:exit_standout_mode])
102
+ tty.print(CODES[:standout_mode] + text + CODES[:exit_standout_mode])
94
103
  else
95
- tty.print(prefix(index) + @formatter.call(@options[index]))
104
+ tty.print(text)
96
105
  end
97
106
  end
98
107
 
@@ -3,11 +3,9 @@ require_relative 'impl'
3
3
  module Ansi
4
4
  class Selector
5
5
  class MultiImpl < Impl
6
- def initialize(options, formatter)
6
+ def initialize(options, formatter, preselected)
7
7
  super
8
-
9
- # @type [Array<Boolean>]
10
- @selected_options = []
8
+ @selected_options = preselected
11
9
  end
12
10
 
13
11
  private
@@ -3,6 +3,11 @@ require_relative 'impl'
3
3
  module Ansi
4
4
  class Selector
5
5
  class SingleImpl < Impl
6
+ def initialize(options, formatter, preselected)
7
+ super
8
+ @highlighted_line_index = preselected
9
+ end
10
+
6
11
  private
7
12
 
8
13
  def prefix(index)
@@ -1,5 +1,5 @@
1
1
  module Ansi
2
2
  class Selector
3
- VERSION = "0.2.4"
3
+ VERSION = "0.2.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansi-select
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodymyr Shatskyi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-05 00:00:00.000000000 Z
11
+ date: 2015-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler