builtinextension 0.0.1 → 0.0.2

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "builtinextension"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["ippei94da"]
@@ -25,9 +25,20 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "builtinextension.gemspec",
28
+ "lib/array_include_eql.rb",
29
+ "lib/array_select_indices.rb",
30
+ "lib/string_color.rb",
28
31
  "lib/string_escape_zsh.rb",
32
+ "lib/string_mismatch.rb",
33
+ "lib/string_width.rb",
34
+ "spec/array_include_eql_spec.rb",
35
+ "spec/array_select_indices_spec.rb",
36
+ "spec/print_color",
29
37
  "spec/spec_helper.rb",
30
- "spec/string_escape_zsh_spec.rb"
38
+ "spec/string_color_spec.rb",
39
+ "spec/string_escape_zsh_spec.rb",
40
+ "spec/string_mismatch_spec.rb",
41
+ "spec/string_width_spec.rb"
31
42
  ]
32
43
  s.homepage = "http://github.com/ippei94da/builtinextension"
33
44
  s.licenses = ["MIT"]
@@ -0,0 +1,22 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ #
5
+ #
6
+ #
7
+ class Array
8
+ # 通常の include? は == で判定するが、
9
+ # これではなく eql? を使って判定するようにしたもの。
10
+ def include_eql?(other)
11
+ result = false
12
+ each do |i|
13
+ if i.eql?(other)
14
+ result ||= true
15
+ break
16
+ end
17
+ end
18
+ return result
19
+ end
20
+
21
+ end
22
+
@@ -0,0 +1,18 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ #
5
+ #
6
+ #
7
+ class Array
8
+ # 受け取ったブロックが true を返す要素のインデックスをソート済配列にして返す。
9
+ def select_indices
10
+ results = []
11
+ each_with_index { |item, index|
12
+ results << index if yield(item)
13
+ }
14
+ return results
15
+ end
16
+
17
+ end
18
+
@@ -0,0 +1,96 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ #文字列の前後にエスケープシーケンスを加えて表示色などを変更する。
5
+ #部分的に変更とかややこしい機能はつけず、
6
+ #単純に self 全体の色を変更する。
7
+ #
8
+ #以下はボツ案。
9
+ # 文字列に表示色情報を加えたクラスを用意すべきか。
10
+ # 表示色は出力デバイス依存なので、String クラスに直接入れるべきではないとは思う。
11
+ # また、文字の境界にエスケープシーケンスが入ることでパターンマッチに障害が生じる。
12
+ # しかし文字列に色をつけるのは殆どの場合表示直前の最終工程なので問題になることは少ないだろう。
13
+ #
14
+ # #"" + i + "" }
15
+ # #^[[35;46m
16
+ #
17
+ #EscCode = ""
18
+ #
19
+ class String
20
+ #色情報を付加。
21
+ #foreground, background はシンボルで指定。
22
+ # 0:黒 :black
23
+ # 1:赤 :red
24
+ # 2:緑 :green
25
+ # 3:黄 :yellow
26
+ # 4:青 :blue
27
+ # 5:紫 :magenta
28
+ # 6:空 :cyan
29
+ # 7:白 :white
30
+ # デフォルト :default
31
+ COLOR_CODE = {
32
+ :black => 0, :red => 1, :green => 2, :yellow => 3,
33
+ :blue => 4, :magenta => 5, :cyan => 6, :white => 7,
34
+ :default => 9,
35
+ }
36
+
37
+ #self の先頭と末尾にカラー情報を入れたエスケープシーケンスをいれる。
38
+ #bg: background を省略するとデフォルトカラーが入る。
39
+ #fg: foreground を省略するとデフォルトカラーは入らない。
40
+ #両方デフォルトカラーというのは意味がないだろうから。
41
+ #デフォルトカラーが指定されてもエスケープシーケンスを省略したりしない。
42
+ def color( fg, bg = :default )
43
+ "\e[3#{COLOR_CODE[ fg ]};4#{COLOR_CODE[ bg ]}m" + self + "\e[39;49m"
44
+ end
45
+
46
+ end
47
+
48
+ #class ColoredString
49
+ # def initialize(str, fg_color = nil, bg_color = nil)
50
+ # @string = str
51
+ # @fg_color = fg_color
52
+ # @bg_color = bg_color
53
+ # end
54
+ #
55
+ # def setFgColor(color)
56
+ # @fg_color = color
57
+ # end
58
+ #
59
+ # def setBgColor(color)
60
+ # @bg_color = color
61
+ # end
62
+ #
63
+ #
64
+ # def getString
65
+ # if ((@fg_color != nil) && (@bg_color != nil))
66
+ # fg_colorCode = symbol2ColorCode(@fg_color)
67
+ # bg_colorCode = symbol2ColorCode(@bg_color)
68
+ # string = "[3#{fg_colorCode};4#{bg_colorCode}m" + @string + ""
69
+ # elsif (@fg_color != nil)
70
+ # fg_colorCode = symbol2ColorCode(@fg_color)
71
+ # string = "[3#{fg_colorCode}m" + @string + ""
72
+ # elsif (@bg_color != nil)
73
+ # bg_colorCode = symbol2ColorCode(@bg_color)
74
+ # string = "[4#{bg_colorCode}m" + @string + ""
75
+ # else #both of them are not colored
76
+ # string = @string
77
+ # end
78
+ # string
79
+ # end
80
+ #
81
+ # private
82
+ # def symbol2ColorCode(color)
83
+ # case color
84
+ # when :black then colorCode = 0
85
+ # when :red then colorCode = 1
86
+ # when :green then colorCode = 2
87
+ # when :yellow then colorCode = 3
88
+ # when :blue then colorCode = 4
89
+ # when :magenta then colorCode = 5
90
+ # when :cyan then colorCode = 6
91
+ # when :white then colorCode = 7
92
+ # end
93
+ # colorCode
94
+ # end
95
+ #end
96
+
@@ -0,0 +1,23 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ class String
5
+
6
+ #self と str を比較して、最初に異なったインデックスを返す。
7
+ #self のサイズ以上のインデックスが返ることがある。e.g.,
8
+ # "abc".mismatch( "abcd" ) #=> 3
9
+ #
10
+ #完全に一致していれば nil を返す。
11
+ def mismatch( str )
12
+ ary1 = self.split( // )
13
+ ary2 = str.split( // )
14
+ index = nil
15
+ [ ary1.size, ary2.size ].max.times do |i|
16
+ if ( ary1[i] != ary2[i] )
17
+ index = i
18
+ break
19
+ end
20
+ end
21
+ index
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ class String
5
+ # 端末で表示されるときの文字幅。
6
+ # 端末ごとの細かい制御とかは対応していない。
7
+ # UTF-8 の ambiguous width 対応の対応は必要に応じて足していく。
8
+ def width
9
+ chars = ' -~' + '’”' #1文字幅の文字
10
+ num_ascii = count(chars)
11
+ num_wide = size - num_ascii
12
+ return 2 * num_wide + num_ascii
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,40 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
+ require "array_include_eql.rb"
6
+
7
+ class Klass
8
+ def initialize(val)
9
+ @val = (val)
10
+ end
11
+
12
+ def eql?(other)
13
+ return @val.floor == other.floor
14
+ end
15
+ end
16
+
17
+
18
+ describe Array, "including items with eql? method" do
19
+ #class TC_Array < Test::Unit::TestCase
20
+ before do
21
+ @a00 = [
22
+ Klass.new(0.0),
23
+ Klass.new(1.0),
24
+ Klass.new(2.0),
25
+ ]
26
+ end
27
+
28
+ it "should be true" do
29
+ @a00.include_eql?( 0.1).should be_true
30
+ @a00.include_eql?( 1.1).should be_true
31
+ @a00.include_eql?( 2.1).should be_true
32
+ end
33
+
34
+ it "should be false" do
35
+ @a00.include_eql?( 3.1).should be_false
36
+ @a00.include_eql?(-0.9).should be_false
37
+ end
38
+
39
+ end
40
+
@@ -0,0 +1,27 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
+ require "array_select_indices.rb"
6
+
7
+
8
+ describe Array, "including items" do
9
+
10
+ it "should do for empty array." do
11
+ [].select_indices{ false } .should == []
12
+ [].select_indices{ true } .should == []
13
+ end
14
+
15
+ it "should do for array of String." do
16
+ %w[ a b c ].select_indices{ false } .should == []
17
+ %w[ a b c ].select_indices{ true } .should == [0,1,2]
18
+ end
19
+
20
+ it "should do for empty array of Integer." do
21
+ [0,1,2,3].select_indices{ false } .should == []
22
+ [0,1,2,3].select_indices{ true } .should == [0,1,2,3]
23
+ [0,1,2,3].select_indices{ |i| i < 2 } .should == [0,1]
24
+ end
25
+
26
+ end
27
+
data/spec/print_color ADDED
@@ -0,0 +1,29 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ puts "\e[30m\e[39m"
5
+ puts "\e[31m\e[39m"
6
+ puts "\e[32m\e[39m"
7
+ puts "\e[33m\e[39m"
8
+ puts "\e[34m\e[39m"
9
+ puts "\e[35m\e[39m"
10
+ puts "\e[36m\e[39m"
11
+ puts "\e[37m\e[39m"
12
+ puts "\e[40mabc\e[49m"
13
+ puts "\e[41mabc\e[49m"
14
+ puts "\e[42mabc\e[49m"
15
+ puts "\e[43mabc\e[49m"
16
+ puts "\e[44mabc\e[49m"
17
+ puts "\e[45mabc\e[49m"
18
+ puts "\e[46mabc\e[49m"
19
+ puts "\e[47mabc\e[49m"
20
+ puts "\e[49mabc\e[49m"
21
+ puts "\e[39;40mabc\e[39;49m"
22
+ puts "\e[39;41mabc\e[39;49m"
23
+ puts "\e[39;42mabc\e[39;49m"
24
+ puts "\e[39;43mabc\e[39;49m"
25
+ puts "\e[39;44mabc\e[39;49m"
26
+ puts "\e[39;45mabc\e[39;49m"
27
+ puts "\e[39;46mabc\e[39;49m"
28
+ puts "\e[39;47mabc\e[39;49m"
29
+ puts "\e[39;49mabc\e[39;49m"
@@ -0,0 +1,69 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
+ require 'string_color.rb'
6
+
7
+
8
+ describe String, "to be added color information" do
9
+ before do
10
+ @s00 = ""
11
+ @s01 = "abc"
12
+ end
13
+
14
+ it "should be sandwitched escape chars of color" do
15
+ @s00.color( :black ) .should == "\e[30;49m\e[39;49m"
16
+ @s00.color( :red ) .should == "\e[31;49m\e[39;49m"
17
+ @s00.color( :green ) .should == "\e[32;49m\e[39;49m"
18
+ @s00.color( :yellow ) .should == "\e[33;49m\e[39;49m"
19
+ @s00.color( :blue ) .should == "\e[34;49m\e[39;49m"
20
+ @s00.color( :magenta ) .should == "\e[35;49m\e[39;49m"
21
+ @s00.color( :cyan ) .should == "\e[36;49m\e[39;49m"
22
+ @s00.color( :white ) .should == "\e[37;49m\e[39;49m"
23
+ @s00.color( :default ) .should == "\e[39;49m\e[39;49m"
24
+ @s00.color( :default, :black ) .should == "\e[39;40m\e[39;49m"
25
+ @s00.color( :default, :red ) .should == "\e[39;41m\e[39;49m"
26
+ @s00.color( :default, :green ) .should == "\e[39;42m\e[39;49m"
27
+ @s00.color( :default, :yellow ) .should == "\e[39;43m\e[39;49m"
28
+ @s00.color( :default, :blue ) .should == "\e[39;44m\e[39;49m"
29
+ @s00.color( :default, :magenta ) .should == "\e[39;45m\e[39;49m"
30
+ @s00.color( :default, :cyan ) .should == "\e[39;46m\e[39;49m"
31
+ @s00.color( :default, :white ) .should == "\e[39;47m\e[39;49m"
32
+ @s00.color( :default, :default ) .should == "\e[39;49m\e[39;49m"
33
+ @s00.color( :white , :black ) .should == "\e[37;40m\e[39;49m"
34
+ @s00.color( :cyan , :red ) .should == "\e[36;41m\e[39;49m"
35
+ @s00.color( :magenta, :green ) .should == "\e[35;42m\e[39;49m"
36
+ @s00.color( :blue , :yellow ) .should == "\e[34;43m\e[39;49m"
37
+ @s00.color( :yellow , :blue ) .should == "\e[33;44m\e[39;49m"
38
+ @s00.color( :green , :magenta ) .should == "\e[32;45m\e[39;49m"
39
+ @s00.color( :red , :cyan ) .should == "\e[31;46m\e[39;49m"
40
+ @s00.color( :black , :white ) .should == "\e[30;47m\e[39;49m"
41
+ @s01.color( :black ) .should == "\e[30;49mabc\e[39;49m"
42
+ @s01.color( :red ) .should == "\e[31;49mabc\e[39;49m"
43
+ @s01.color( :green ) .should == "\e[32;49mabc\e[39;49m"
44
+ @s01.color( :yellow ) .should == "\e[33;49mabc\e[39;49m"
45
+ @s01.color( :blue ) .should == "\e[34;49mabc\e[39;49m"
46
+ @s01.color( :magenta ) .should == "\e[35;49mabc\e[39;49m"
47
+ @s01.color( :cyan ) .should == "\e[36;49mabc\e[39;49m"
48
+ @s01.color( :white ) .should == "\e[37;49mabc\e[39;49m"
49
+ @s01.color( :default ) .should == "\e[39;49mabc\e[39;49m"
50
+ @s01.color( :default, :black ) .should == "\e[39;40mabc\e[39;49m"
51
+ @s01.color( :default, :red ) .should == "\e[39;41mabc\e[39;49m"
52
+ @s01.color( :default, :green ) .should == "\e[39;42mabc\e[39;49m"
53
+ @s01.color( :default, :yellow ) .should == "\e[39;43mabc\e[39;49m"
54
+ @s01.color( :default, :blue ) .should == "\e[39;44mabc\e[39;49m"
55
+ @s01.color( :default, :magenta ) .should == "\e[39;45mabc\e[39;49m"
56
+ @s01.color( :default, :cyan ) .should == "\e[39;46mabc\e[39;49m"
57
+ @s01.color( :default, :white ) .should == "\e[39;47mabc\e[39;49m"
58
+ @s01.color( :default, :default ) .should == "\e[39;49mabc\e[39;49m"
59
+ @s01.color( :white , :black ) .should == "\e[37;40mabc\e[39;49m"
60
+ @s01.color( :cyan , :red ) .should == "\e[36;41mabc\e[39;49m"
61
+ @s01.color( :magenta, :green ) .should == "\e[35;42mabc\e[39;49m"
62
+ @s01.color( :blue , :yellow ) .should == "\e[34;43mabc\e[39;49m"
63
+ @s01.color( :yellow , :blue ) .should == "\e[33;44mabc\e[39;49m"
64
+ @s01.color( :green , :magenta ) .should == "\e[32;45mabc\e[39;49m"
65
+ @s01.color( :red , :cyan ) .should == "\e[31;46mabc\e[39;49m"
66
+ @s01.color( :black , :white ) .should == "\e[30;47mabc\e[39;49m"
67
+ end
68
+
69
+ end
@@ -1,11 +1,9 @@
1
1
  #! /usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
-
5
4
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
6
5
 
7
6
  require 'string_escape_zsh.rb'
8
- #require 'test/unit'
9
7
 
10
8
  describe String, "with chars to be escaped" do
11
9
  before do
@@ -0,0 +1,29 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
+ require 'string_mismatch.rb'
6
+
7
+
8
+ describe String, "for mismatch check" do
9
+ before do
10
+ @s00 = 'abcdefghij'
11
+ @s01 = 'あいうえお'
12
+ end
13
+
14
+ it "should be indicated first mismatch char index" do
15
+ @s00.mismatch( "abcdefghij" ) .should == nil
16
+ @s00.mismatch( "_bcdefghij" ) .should == 0
17
+ @s00.mismatch( "abcd_fghij" ) .should == 4
18
+ @s00.mismatch( "abcdefghijk" ) .should == 10
19
+ @s00.mismatch( "abc" ) .should == 3
20
+
21
+ @s01.mismatch( "あいうえお" ) .should == nil
22
+ @s01.mismatch( "_いうえお" ) .should == 0
23
+ @s01.mismatch( "あいうえ_" ) .should == 4
24
+ @s01.mismatch( "あいうえおか" ) .should == 5
25
+ @s01.mismatch( "あいう" ) .should == 3
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,17 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
+ require "string_width.rb"
6
+
7
+ describe String, "for width in displayed terminal" do
8
+ it do
9
+ "abc".width .should == 3
10
+ "あいう".width .should == 6
11
+ "あaいbうc".width .should == 9
12
+ "’".width .should == 1
13
+ "”".width .should == 1
14
+ end
15
+
16
+ end
17
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: builtinextension
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -110,9 +110,20 @@ files:
110
110
  - Rakefile
111
111
  - VERSION
112
112
  - builtinextension.gemspec
113
+ - lib/array_include_eql.rb
114
+ - lib/array_select_indices.rb
115
+ - lib/string_color.rb
113
116
  - lib/string_escape_zsh.rb
117
+ - lib/string_mismatch.rb
118
+ - lib/string_width.rb
119
+ - spec/array_include_eql_spec.rb
120
+ - spec/array_select_indices_spec.rb
121
+ - spec/print_color
114
122
  - spec/spec_helper.rb
123
+ - spec/string_color_spec.rb
115
124
  - spec/string_escape_zsh_spec.rb
125
+ - spec/string_mismatch_spec.rb
126
+ - spec/string_width_spec.rb
116
127
  homepage: http://github.com/ippei94da/builtinextension
117
128
  licenses:
118
129
  - MIT
@@ -128,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
139
  version: '0'
129
140
  segments:
130
141
  - 0
131
- hash: -978337805
142
+ hash: -312578157
132
143
  required_rubygems_version: !ruby/object:Gem::Requirement
133
144
  none: false
134
145
  requirements: