coolline 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c9e0491e7e65c920ecee01ffb8f7817a861a7ea
4
- data.tar.gz: 8441446e2d59571b45220fffce3988aa77e43c57
3
+ metadata.gz: d5c06bef3a232dd46d512291be86f29a0a203dfc
4
+ data.tar.gz: 4c1e3321aa186997373bcadd3d69cbc9662bc139
5
5
  SHA512:
6
- metadata.gz: ee8ec55d743a03a2317c60a17503e1869df2dd110fc666f255b7bd4654935de4fc1c8d18aef12bd71ffa790d6cdc89981087fbfff7e2e762c4fd55bf9164a1cf
7
- data.tar.gz: 0709d7d1450462b3e4728e5496fd4481542486aeb2478b8b303de211044f76267bf41a7ded72d08d0ddf2b30a48dab57459009ce117d0b668fd6ac78a6d62164
6
+ metadata.gz: 69910fdbadffda0f8cfd76af7ee224988cb1d25f4a2c9ff4fe6720a1457a6de9b6375471c38451caa8dcd4d8a963e0a7238f5a46074a4acd87944ef8e1c514c0
7
+ data.tar.gz: 2c80c94cb3711eb172ed0fb3ca26d868dbdb2a3146d96c9d94fb54ce3d56b4026ac78bb9217d4b606af1b68e9cf894731139b506916a22f12c3b3f26adfea15f
@@ -9,10 +9,10 @@ class Coolline
9
9
  module ANSI
10
10
  Code = %r{(\e\[\??\d+(?:[;\d]*)\w)}
11
11
 
12
- # @return [Integer] Amount of characters within the string, disregarding
13
- # color codes.
12
+ # @return [Integer] Display width taken up by the string, where
13
+ # color codes take up no width at all.
14
14
  def ansi_length(string)
15
- strip_ansi_codes(string).length
15
+ UnicodeUtils.display_width strip_ansi_codes(string)
16
16
  end
17
17
 
18
18
  # @return [String] The initial string without ANSI codes.
@@ -31,21 +31,21 @@ class Coolline
31
31
  #
32
32
  # @param [String] string
33
33
  # @param [Integer] start
34
- # @param [Integer] stop Stop index, excluded from the range.
34
+ # @param [Integer] stop Stop column index, excluded from the range.
35
35
  def ansi_print(string, start, stop)
36
36
  i = 0
37
37
  string.split(Code).each do |str|
38
38
  if start_with_ansi_code? str
39
39
  print str
40
40
  else
41
- if i >= start
42
- print str[0..(stop - i - 1)]
43
- elsif i < start && i + str.size >= start
44
- print str[(start - i), stop - start - 1]
45
- end
41
+ width = UnicodeUtils.display_width str
46
42
 
47
- i += str.size
48
- break if i >= stop
43
+ UnicodeUtils.each_grapheme(str) { |g|
44
+ width = UnicodeUtils.display_width g
45
+ print g if i >= start && i + width <= stop
46
+ i += width
47
+ break if i >= stop
48
+ }
49
49
  end
50
50
  end
51
51
  end
@@ -1,4 +1,5 @@
1
1
  require 'io/console'
2
+ require 'unicode_utils'
2
3
 
3
4
  class Coolline
4
5
  include ANSI
@@ -27,6 +28,7 @@ class Coolline
27
28
  # @return [Hash] All the defaults settings
28
29
  Settings = {
29
30
  :word_boundaries => [" ", "-", "_"],
31
+ :completion_word_boundaries => [" ", "-", "_"],
30
32
 
31
33
  :handlers =>
32
34
  [
@@ -123,7 +125,9 @@ class Coolline
123
125
  @input = STDIN # must be the actual IO object
124
126
  @output = $stdout
125
127
 
126
- self.word_boundaries = Settings[:word_boundaries].dup
128
+ self.word_boundaries = Settings[:word_boundaries].dup
129
+ self.completion_word_boundaries = Settings[:completion_word_boundaries].dup
130
+
127
131
  self.handlers = Settings[:handlers].dup
128
132
  self.transform_proc = Settings[:transform_proc]
129
133
  self.unknown_char_proc = Settings[:unknown_char_proc]
@@ -141,7 +145,8 @@ class Coolline
141
145
  # @return [IO]
142
146
  attr_accessor :input, :output
143
147
 
144
- # @return [Array<String, Regexp>] Expressions detected as word boundaries
148
+ # @return [Array<String, Regexp>] Expressions detected as word boundaries for
149
+ # the purpose of motion commands.
145
150
  attr_reader :word_boundaries
146
151
 
147
152
  # @return [Regexp] Regular expression to match word boundaries
@@ -152,6 +157,10 @@ class Coolline
152
157
  @word_boundaries_regexp = /\A#{Regexp.union(*array)}\z/
153
158
  end
154
159
 
160
+ # @return [Array<String, Regexp>] Expressions detected as word boundaries for
161
+ # the purpose of autocompletion.
162
+ attr_accessor :completion_word_boundaries
163
+
155
164
  # @return [Proc] Proc called to change the way a line is displayed
156
165
  attr_accessor :transform_proc
157
166
 
@@ -265,6 +274,9 @@ class Coolline
265
274
 
266
275
  reset_line
267
276
 
277
+ width_before = UnicodeUtils.display_width @line[0..@pos]
278
+ width_before += 1 if @pos >= @line.length
279
+
268
280
  if ansi_length(@prompt + line) <= width
269
281
  print @prompt + line
270
282
  else
@@ -272,14 +284,15 @@ class Coolline
272
284
 
273
285
  left_width = width - prompt_size
274
286
 
275
- start_index = [@pos - left_width + 1, 0].max
287
+ start_index = [width_before - left_width, 0].max
276
288
  end_index = start_index + left_width
277
289
 
278
290
  ansi_print(line, start_index, end_index)
279
291
  end
280
292
 
281
293
  @menu.display
282
- go_to_col [prompt_size + @pos + 1, width].min
294
+ go_to_col [prompt_size + width_before, width].
295
+ min
283
296
  end
284
297
 
285
298
  # Reads a line with no prompt
@@ -414,6 +427,9 @@ class Coolline
414
427
 
415
428
  # Tries to complete the current word
416
429
  def complete
430
+ old_word_boundaries = word_boundaries
431
+ self.word_boundaries = completion_word_boundaries
432
+
417
433
  return if word_boundary? line[pos - 1]
418
434
 
419
435
  completions = @completion_proc.call(self)
@@ -424,6 +440,8 @@ class Coolline
424
440
  menu.list = completions
425
441
  self.completed_word = common_beginning(completions)
426
442
  end
443
+ ensure
444
+ self.word_boundaries = old_word_boundaries
427
445
  end
428
446
 
429
447
  def word_boundary?(char)
@@ -1,3 +1,3 @@
1
1
  class Coolline
2
- Version = "0.4.4"
2
+ Version = "0.5.0"
3
3
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.expand_path("helpers.rb", File.dirname(__FILE__))
2
3
 
3
4
  context "a user uses colors for live code highlighting" do
@@ -28,3 +29,30 @@ context "a user uses non-tty input" do
28
29
  asserts(:readline).equals "p 82"
29
30
  asserts(:readline).nil
30
31
  end
32
+
33
+ context "printing part of a string" do
34
+ setup {
35
+ $stdout = StringIO.new
36
+ extend Coolline::ANSI
37
+ }
38
+
39
+ context "with ASCII characters" do
40
+ hookup { ansi_print("Hello World", 2, 9) }
41
+ asserts { $stdout.string }.equals "llo Wor"
42
+ end
43
+
44
+ context "with non-ASCII characters" do
45
+ hookup { ansi_print("Hellô Wörld", 3, 7) }
46
+ asserts { $stdout.string }.equals "lô W"
47
+ end
48
+
49
+ context "with full-width characters" do
50
+ hookup { ansi_print("日本e本語語本語本語", 3, 9) }
51
+ asserts { $stdout.string }.equals "e本語"
52
+ end
53
+
54
+ context "with ANSI codes interleaved" do
55
+ hookup { ansi_print("\e[3mH\e[42mello W\e[41morld\e[0m", 2, 9) }
56
+ asserts { $stdout.string }.equals "\e[3m\e[42mllo W\e[41mor\e[0m"
57
+ end
58
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coolline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mon ouie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-26 00:00:00.000000000 Z
11
+ date: 2014-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unicode_utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: riot
15
29
  requirement: !ruby/object:Gem::Requirement