kaki-utils 0.1.4 → 0.1.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
  SHA256:
3
- metadata.gz: b5a39e1bfe3e48aab4cc88c3b3168c468ba5c35092e33cdf28a3a14579585b13
4
- data.tar.gz: e38a4c2feb70de6296fd6060b4d9e925449a7c8fada76e425f4aeacab2a31644
3
+ metadata.gz: 7d5d2f2816d7ba06f5e13facb8798434194bc13bd41c81177342cc84f75f76f7
4
+ data.tar.gz: e7fc93af05bd0319517ebd020c7e9e03e995821c2399ea84f980ee1cf2d71687
5
5
  SHA512:
6
- metadata.gz: 8de911d38ce91d248524d32a251a2607bfdd9a41105f833d0afca2ab574f153e8756a95e3c4b2e4a3183217ca005b146fad5371d10369ada4c526a2b79a2bfc3
7
- data.tar.gz: 3d45c2c8c9b77854a2b9735f9fe00b66e44ea6082862692974d3dda35d76d094e78c0d8019aaabe64be3243fe9f6a9836a4779799c26d6e5c6853d8ba7c388bc
6
+ metadata.gz: cd28126491e46ad6630b346b99c8328fd7d207a80148a709b0cd46a3e1c9b7a8cc912f647c8def6baf66581b417053dae16f695ae9e22728a18913f72d9f7a00
7
+ data.tar.gz: 63a830b5cc3deaa9aa9a67c8a1d5fa206de6826ddd92167b811af466fa3ff4b7b24d3360c657dd1c9106c0c3a4850edb3a6d6cdf57b3f4512af5baf79553cb9a
data/README.md CHANGED
@@ -9,7 +9,6 @@ methods
9
9
  * Utils.imgexist?(url)
10
10
  * Utils.getfile(url, filename, max=0)
11
11
  * Utils.key_wait
12
- * Utils.progress_bar
13
12
  * Utils.delete\_non\_img(file_name)
14
13
  * Utils.delete\_empty\_folder(folder_name = './')
15
14
  * Utils.bell    (only Ubuntu)
@@ -75,8 +74,8 @@ require "kaki/utils/add_methods"
75
74
  ```
76
75
  methods
77
76
 
78
- * Object#add_methods(module_to_include, *method_names)
79
- * Module#register_all_methods
77
+ * Object#add\_methods(module\_to\_include, *method\_names)
78
+ * Module#register\_all\_methods
80
79
 
81
80
  ## instance method of "p"
82
81
 
@@ -86,3 +85,33 @@ require "kaki/utils/p"
86
85
  method
87
86
 
88
87
  * Object#p
88
+
89
+ ## safe making directory
90
+
91
+ ```ruby
92
+ require "kaki/utils/safe_mkdir"
93
+ ```
94
+ method
95
+
96
+ * Dir.safe\_mkdir(dname)
97
+
98
+ ## handle escape sequence
99
+
100
+ ```ruby
101
+ require "kaki/utils/es"
102
+ ```
103
+ methods
104
+
105
+ * ES.color(col, opt = nil)
106
+ * ES.csi(*args)
107
+ * ES.cmd(given)
108
+ * ES.esc(str)
109
+ * ES.clear, down, up, reset, top, home, push, pop
110
+ * ES.cursor(x, y = nil)
111
+ * ES.cursor\_r(x, y)
112
+ * ES.console\_size, cursor_position
113
+ * ES.scroll(rn = nil)
114
+ * ES.safe_scroll(n)
115
+ * ES.scroll\_up(n)
116
+ * ES.clear\_below
117
+
@@ -0,0 +1,147 @@
1
+ require 'io/console'
2
+
3
+ module ES
4
+ S = "\e["
5
+ Col = %i(black red green yellow blue magenta cyan white ex_c ex_b reset style code)
6
+ Cmd1 = %i(CUU CUD CUF CUB CNL CPL CHA CHT CBT ECH DCH IL DL SU SD REP DSR)
7
+ Do1 = %w(A B C D E F G I Z X P L M S T b n)
8
+ Cmd2 = %i(DECSC DECRC RIS DECALN IND NEL HTS VTS PLD PLU RI DSC SOS ST)
9
+ Do2 = %w(7 8 c #8 D E H J K L M P X /)
10
+
11
+ def color(col, opt = nil)
12
+ ch = Col.map.with_index {|c, i| [c, i + 30]}.to_h[col]
13
+ case ch
14
+ when 38
15
+ return S + "38;5;#{opt.to_i}m"
16
+ when 39
17
+ return S + "48;5;#{opt.to_i}m"
18
+ when 40
19
+ return S + "39;40m"
20
+ when 41
21
+ n = case opt
22
+ when :bold then 1
23
+ when :italic then 3
24
+ when :blink then 5
25
+ when :reverse then 7
26
+ when :blink_stop then 25
27
+ when :underline then 4
28
+ when :bold_stop then 22
29
+ when :underline_stop then 24
30
+ else opt
31
+ end
32
+ return S + "#{n}m"
33
+ when 42
34
+ return S + "#{opt}m"
35
+ end
36
+ raise "Undefind color name: #{col}" unless ch
37
+ m = case opt
38
+ when :background then 10
39
+ when :bright then 60
40
+ when :bright_background then 70
41
+ else 0
42
+ end
43
+ S + (ch + m).to_s + "m"
44
+ end
45
+
46
+ def csi(*args)
47
+ cm = Cmd1.zip(Do1).to_h
48
+ if (a = cm[args[0]])
49
+ S + args[1].to_s + a
50
+ else
51
+ case args[0]
52
+ when :CUP then S + "#{args[1]};#{args[2]}H"
53
+ when :ED then S + (args[1] ? args[1].to_s : "") + "J"
54
+ when :EL then S + (args[1] ? args[1].to_s : "") + "K"
55
+ when :TBC then S + (args[1] ? args[1].to_s : "") + "g"
56
+ when :DECSTBM
57
+ S + (args[1] ? "#{args[1]};#{args[2]}" : "") + "r"
58
+ when :DECTCEM
59
+ S + "?25" + args[1]
60
+ else
61
+ raise "#{args[0]} is undefined CSI."
62
+ end
63
+ end
64
+ end
65
+
66
+ def cmd(given)
67
+ cm = Cmd2.zip(Do2).to_h[given]
68
+ cm ? "\e" + cm : raise("#{given} is undefined command.")
69
+ end
70
+
71
+ def esc(str)
72
+ "\e" + str
73
+ end
74
+
75
+ def clear() ES.csi(:ED, 2) + ES.csi(:CUP, 1, 1) end
76
+ def down() ES.cmd(:NEL) end
77
+ def up() ES.cmd(:RI) end
78
+ def reset() S + "0m" end
79
+ def top() ES.csi(:CHA, 1) end
80
+ def home() ES.cursor(0, 0) end
81
+ def push() ES.cmd(:DECSC) end
82
+ def pop() ES.cmd(:DECRC) end
83
+
84
+ def cursor(x, y = nil)
85
+ y ? ES.csi(:CUP, y, x) : ES.csi(:CHA, x)
86
+ end
87
+
88
+ def cursor_r(x, y)
89
+ st = ""
90
+ st += if x > 0
91
+ ES.csi(:CUF, x)
92
+ elsif x < 0
93
+ ES.csi(:CUB, -x)
94
+ else
95
+ ""
96
+ end
97
+ st += if y > 0
98
+ ES.csi(:CUD, y)
99
+ elsif y < 0
100
+ ES.csi(:CUU, -y)
101
+ else
102
+ ""
103
+ end
104
+ end
105
+
106
+ def console_size
107
+ [`tput cols`, `tput lines`].map(&:to_i)
108
+ end
109
+
110
+ def cursor_position
111
+ puts "\x1B[6n"
112
+ res = ""
113
+ STDIN.raw do |io|
114
+ until (c = io.getc) == 'R'
115
+ res << c if c
116
+ end
117
+ end
118
+ m = /(\d+);(\d+)/.match(res)
119
+ [m[2], m[1]].map(&:to_i)
120
+ end
121
+
122
+ def scroll(rn = nil)
123
+ return case rn
124
+ when Range then ES.csi(:DECSTBM, rn.first, rn.max)
125
+ when 0 then ES.csi(:DECSTBM)
126
+ else ES.push + ES.csi(:DECSTBM) + ES.pop
127
+ end
128
+ end
129
+
130
+ def safe_scroll(n)
131
+ return "" if n <= 0
132
+ str = ""
133
+ y = ES.cursor_position[1]
134
+ if (h = ES.console_size[1]) < y + n
135
+ str = ES.scroll_up(n - 1)
136
+ y = h - n
137
+ end
138
+ str + ES.cursor(1, y)
139
+ end
140
+
141
+ def scroll_up(n) "\n" * n end
142
+ def clear_below() ES.csi(:ED) end
143
+
144
+ module_function :color, :csi, :cmd, :clear, :down, :up, :reset, :top,
145
+ :cursor, :cursor_r, :home, :push, :pop, :esc, :console_size, :scroll,
146
+ :scroll_up, :cursor_position, :clear_below, :safe_scroll
147
+ end
@@ -0,0 +1,10 @@
1
+ class Dir
2
+ #名前がdnameのファイルかディレクトリが存在していれば何もしない でなければディレクトリdnameを作る
3
+ # @return [Boolean]
4
+ def self.safe_mkdir(dname)
5
+ return false if File.exist?(dname)
6
+ Dir.mkdir(dname)
7
+ true
8
+ end
9
+ end
10
+
data/lib/kaki/utils.rb CHANGED
@@ -2,7 +2,6 @@ require 'open-uri'
2
2
  require 'fileutils'
3
3
  require 'fastimage'
4
4
  require "io/console"
5
- require 'gtk2'
6
5
  require 'set'
7
6
 
8
7
  module Utils
@@ -14,11 +13,12 @@ module Utils
14
13
 
15
14
  #Web上のバイナリファイルの保存
16
15
  # @return [Boolean]
17
- def getfile(url, filename, max=0)
16
+ def getfile(url, filename, max = 0, ua = nil)
18
17
  count = 0
18
+ opt = ua ? {'User-Agent' => ua} : {}
19
19
  begin
20
20
  open(filename, 'wb') do |file|
21
- open(url) {|data| file.write(data.read)}
21
+ open(url, opt) {|data| file.write(data.read)}
22
22
  end
23
23
  true
24
24
  rescue
@@ -45,27 +45,6 @@ module Utils
45
45
  module_function :key_wait
46
46
 
47
47
 
48
- #プログレスバーの表示
49
- def progress_bar
50
- w = Gtk::Window.new
51
- w.signal_connect("destroy") {Gtk.main_quit}
52
- w.set_size_request(300, 50)
53
- w.border_width = 10
54
- w.title = "Progress"
55
-
56
- bar = Gtk::ProgressBar.new
57
- w.add(bar)
58
-
59
- Thread.new(bar) do |bar|
60
- yield(bar)
61
- end
62
-
63
- w.show_all
64
- Gtk.main
65
- end
66
- module_function :progress_bar
67
-
68
-
69
48
  #画像ファイルでなければ削除
70
49
  # @return [Boolean]
71
50
  def delete_non_img(fname)
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaki-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - obelisk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-17 00:00:00.000000000 Z
11
+ date: 2018-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: gtk2
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: fastimage
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -48,11 +34,13 @@ files:
48
34
  - lib/kaki/utils.rb
49
35
  - lib/kaki/utils/add_methods.rb
50
36
  - lib/kaki/utils/check_fname_overlapping.rb
37
+ - lib/kaki/utils/es.rb
51
38
  - lib/kaki/utils/imgsuffix.rb
52
39
  - lib/kaki/utils/nest_loop.rb
53
40
  - lib/kaki/utils/p.rb
54
41
  - lib/kaki/utils/rec_decimal.rb
55
42
  - lib/kaki/utils/retry.rb
43
+ - lib/kaki/utils/safe_mkdir.rb
56
44
  homepage:
57
45
  licenses:
58
46
  - MIT