riel 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -0
- data/lib/riel/ansicolor.rb +93 -0
- data/lib/riel/array.rb +20 -0
- data/lib/riel/command.rb +30 -0
- data/lib/riel/date.rb +16 -0
- data/lib/riel/dir.rb +90 -0
- data/lib/riel/enumerable.rb +66 -0
- data/lib/riel/env.rb +49 -0
- data/lib/riel/file.rb +212 -0
- data/lib/riel/filetype.rb +189 -0
- data/lib/riel/hash.rb +12 -0
- data/lib/riel/io.rb +20 -0
- data/lib/riel/log.rb +548 -0
- data/lib/riel/matchdata.rb +13 -0
- data/lib/riel/optproc.rb +369 -0
- data/lib/riel/pathname.rb +16 -0
- data/lib/riel/rcfile.rb +35 -0
- data/lib/riel/regexp.rb +152 -0
- data/lib/riel/setdiff.rb +53 -0
- data/lib/riel/size_converter.rb +62 -0
- data/lib/riel/string.rb +81 -0
- data/lib/riel/tempfile.rb +28 -0
- data/lib/riel/text.rb +408 -0
- data/lib/riel/timer.rb +52 -0
- data/lib/riel.rb +13 -0
- data/test/riel/array_test.rb +22 -0
- data/test/riel/command_test.rb +28 -0
- data/test/riel/date_test.rb +17 -0
- data/test/riel/dir_test.rb +98 -0
- data/test/riel/enumerable_test.rb +27 -0
- data/test/riel/env_test.rb +52 -0
- data/test/riel/file_test.rb +242 -0
- data/test/riel/filetype_test.rb +32 -0
- data/test/riel/hash_test.rb +12 -0
- data/test/riel/io_test.rb +22 -0
- data/test/riel/log_test.rb +184 -0
- data/test/riel/matchdata_test.rb +15 -0
- data/test/riel/optproc_test.rb +233 -0
- data/test/riel/pathname_test.rb +36 -0
- data/test/riel/rcfile_test.rb +44 -0
- data/test/riel/regexp_test.rb +24 -0
- data/test/riel/setdiff_test.rb +26 -0
- data/test/riel/size_converter_test.rb +64 -0
- data/test/riel/string_test.rb +58 -0
- data/test/riel/tempfile_test.rb +16 -0
- data/test/riel/text_test.rb +102 -0
- data/test/riel/timer_test.rb +43 -0
- metadata +134 -0
@@ -0,0 +1,189 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'singleton'
|
5
|
+
|
6
|
+
class FileType
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
BINARY = "binary"
|
10
|
+
DIRECTORY = "directory"
|
11
|
+
NONE = "none"
|
12
|
+
TEXT = "text"
|
13
|
+
UNKNOWN = "unknown"
|
14
|
+
UNREADABLE = "unreadable"
|
15
|
+
|
16
|
+
# extensions associated with files that are always text:
|
17
|
+
TEXT_EXTENSIONS = %w{
|
18
|
+
c
|
19
|
+
css
|
20
|
+
el
|
21
|
+
h
|
22
|
+
html
|
23
|
+
java
|
24
|
+
mk
|
25
|
+
php
|
26
|
+
pl
|
27
|
+
pm
|
28
|
+
rb
|
29
|
+
rbw
|
30
|
+
txt
|
31
|
+
xml
|
32
|
+
yml
|
33
|
+
yaml
|
34
|
+
}
|
35
|
+
|
36
|
+
# extensions associated with files that are never text:
|
37
|
+
NONTEXT_EXTENSIONS = %w{
|
38
|
+
Z
|
39
|
+
a
|
40
|
+
bz2
|
41
|
+
elc
|
42
|
+
gif
|
43
|
+
gz
|
44
|
+
jar
|
45
|
+
jpeg
|
46
|
+
jpg
|
47
|
+
mp3
|
48
|
+
mpeg
|
49
|
+
o
|
50
|
+
obj
|
51
|
+
pdf
|
52
|
+
png
|
53
|
+
ps
|
54
|
+
tar
|
55
|
+
wav
|
56
|
+
zip
|
57
|
+
}
|
58
|
+
|
59
|
+
EXTENSION_REGEXP = %r{ \. (\w+) $ }x
|
60
|
+
|
61
|
+
def initialize
|
62
|
+
# the percentage of characters that we allow to be odd in a text file
|
63
|
+
@odd_factor = 0.3
|
64
|
+
|
65
|
+
# how many bytes (characters) of a file we test
|
66
|
+
@test_length = 1024
|
67
|
+
|
68
|
+
@known = Hash.new
|
69
|
+
|
70
|
+
set_extensions(true, *TEXT_EXTENSIONS)
|
71
|
+
set_extensions(false, *NONTEXT_EXTENSIONS)
|
72
|
+
end
|
73
|
+
|
74
|
+
def ascii?(c)
|
75
|
+
# from ctype.h
|
76
|
+
(c.to_i & ~0x7f) == 0
|
77
|
+
end
|
78
|
+
|
79
|
+
def type(file)
|
80
|
+
begin
|
81
|
+
case File.stat(file).ftype
|
82
|
+
when "directory"
|
83
|
+
DIRECTORY
|
84
|
+
when "file"
|
85
|
+
if File.readable?(file)
|
86
|
+
text?(file) ? TEXT : BINARY
|
87
|
+
else
|
88
|
+
UNREADABLE
|
89
|
+
end
|
90
|
+
else
|
91
|
+
UNKNOWN
|
92
|
+
end
|
93
|
+
rescue Errno::ENOENT
|
94
|
+
NONE
|
95
|
+
rescue => e
|
96
|
+
warn "file not readable: #{file}; error: #{e}"
|
97
|
+
UNREADABLE
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def set_extensions(is_text, *exts)
|
102
|
+
exts.each do |ext|
|
103
|
+
@known[ext] = is_text
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def set_text(*ext)
|
108
|
+
@known[ext] = true
|
109
|
+
end
|
110
|
+
|
111
|
+
def set_nontext(ext)
|
112
|
+
@known[ext] = false
|
113
|
+
end
|
114
|
+
|
115
|
+
def text_extensions
|
116
|
+
@known.keys.select { |suf| @known[suf] }
|
117
|
+
end
|
118
|
+
|
119
|
+
def nontext_extensions
|
120
|
+
@known.keys.reject { |suf| @known[suf] }
|
121
|
+
end
|
122
|
+
|
123
|
+
def text?(file)
|
124
|
+
return false unless File.exists?(file)
|
125
|
+
|
126
|
+
if md = EXTENSION_REGEXP.match(file.to_s)
|
127
|
+
suffix = md[1]
|
128
|
+
if @known.include?(suffix)
|
129
|
+
return @known[suffix]
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
ntested = 0
|
134
|
+
nodd = 0
|
135
|
+
|
136
|
+
begin
|
137
|
+
File.open(file) do |f|
|
138
|
+
buf = f.read(@test_length)
|
139
|
+
if buf
|
140
|
+
buf.each_byte do |ch|
|
141
|
+
ntested += 1
|
142
|
+
|
143
|
+
# never allow null in a text file
|
144
|
+
return false if ch.to_i == 0
|
145
|
+
|
146
|
+
nodd += 1 unless ascii?(ch)
|
147
|
+
end
|
148
|
+
else
|
149
|
+
# file had length of 0:
|
150
|
+
return UNKNOWN
|
151
|
+
end
|
152
|
+
end
|
153
|
+
rescue => e
|
154
|
+
warn "file not readable: #{file}; error: #{e}"
|
155
|
+
return UNREADABLE
|
156
|
+
end
|
157
|
+
|
158
|
+
nodd < ntested * @odd_factor
|
159
|
+
end
|
160
|
+
|
161
|
+
def self.ascii?(c)
|
162
|
+
return self.instance.ascii?(c)
|
163
|
+
end
|
164
|
+
|
165
|
+
def self.type(file)
|
166
|
+
return self.instance.type(file)
|
167
|
+
end
|
168
|
+
|
169
|
+
def self.set_text(ext)
|
170
|
+
return self.instance.set_text(ext)
|
171
|
+
end
|
172
|
+
|
173
|
+
def self.set_nontext(ext)
|
174
|
+
return self.instance.set_nontext(ext)
|
175
|
+
end
|
176
|
+
|
177
|
+
def self.text_extensions
|
178
|
+
return self.instance.text_extensions
|
179
|
+
end
|
180
|
+
|
181
|
+
def self.nontext_extensions
|
182
|
+
return self.instance.nontext_extensions
|
183
|
+
end
|
184
|
+
|
185
|
+
def self.text?(file)
|
186
|
+
return self.instance.text?(file)
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
data/lib/riel/hash.rb
ADDED
data/lib/riel/io.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
class IO
|
5
|
+
|
6
|
+
$-w = false
|
7
|
+
|
8
|
+
# Reads the stream into an array. It works even when $/ == nil, which
|
9
|
+
# works around a problem in Ruby 1.8.1.
|
10
|
+
def readlines
|
11
|
+
contents = []
|
12
|
+
while ((line = gets) && line.length > 0)
|
13
|
+
contents << line
|
14
|
+
end
|
15
|
+
contents
|
16
|
+
end
|
17
|
+
|
18
|
+
$-w = true
|
19
|
+
|
20
|
+
end
|