tty-file 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/lib/tty/file.rb +31 -2
- data/lib/tty/file/version.rb +1 -1
- data/spec/spec_helper.rb +6 -6
- data/spec/unit/binary_spec.rb +6 -0
- data/spec/unit/read_to_char_spec.rb +24 -0
- data/tasks/console.rake +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff0231767c10a14f731dcf153855ef3c652f0db9996e0a9588ad17b8c6bb4bdc
|
4
|
+
data.tar.gz: a441339b4d10b0ab58e6775d1df7471c36a475700ffdd385ad18d75c0b28459b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 322c2d767842c0a315c98eb01175cb0b2ca1b32ce6a5d961ef274d67db9428c5c6b32a710c81345a6383ca92cd67862f2a3354a853f587ed00b47196eb75f664
|
7
|
+
data.tar.gz: 340eb4e7b495e9ed91224fd6eec7df4e4990c4ef5dd5024e08110285560bf3e1bf5dd6c33433493f145f8b5dc3f76aa60052fbca5933816dbca0569c60e2ef77
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.8.0] - 2019-07-25
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add #read_to_char for reading file content limited by bytes until a valid char
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
* Fix #binary? to correctly identify large UTF-8 files as non-binary
|
10
|
+
|
3
11
|
## [v0.7.1] - 2019-05-06
|
4
12
|
|
5
13
|
### Changed
|
@@ -88,6 +96,7 @@
|
|
88
96
|
|
89
97
|
* Initial implementation and release
|
90
98
|
|
99
|
+
[v0.8.0]: https://github.com/piotrmurach/tty-file/compare/v0.7.1...v0.8.0
|
91
100
|
[v0.7.1]: https://github.com/piotrmurach/tty-file/compare/v0.7.0...v0.7.1
|
92
101
|
[v0.7.0]: https://github.com/piotrmurach/tty-file/compare/v0.6.0...v0.7.0
|
93
102
|
[v0.6.0]: https://github.com/piotrmurach/tty-file/compare/v0.5.0...v0.6.0
|
data/lib/tty/file.rb
CHANGED
@@ -53,8 +53,8 @@ module TTY
|
|
53
53
|
def binary?(relative_path)
|
54
54
|
bytes = ::File.new(relative_path).size
|
55
55
|
bytes = 2**12 if bytes > 2**12
|
56
|
-
buffer =
|
57
|
-
|
56
|
+
buffer = read_to_char(relative_path, bytes, 0)
|
57
|
+
|
58
58
|
begin
|
59
59
|
return buffer !~ /\A[\s[[:print:]]]*\z/m
|
60
60
|
rescue ArgumentError => error
|
@@ -64,6 +64,35 @@ module TTY
|
|
64
64
|
end
|
65
65
|
module_function :binary?
|
66
66
|
|
67
|
+
# Read bytes from a file up to valid character
|
68
|
+
#
|
69
|
+
# @param [String, Pathname] relative_path
|
70
|
+
# the path to file
|
71
|
+
#
|
72
|
+
# @param [Integer] bytes
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
# TTY::File.read_to_char()
|
76
|
+
#
|
77
|
+
# @return [String]
|
78
|
+
#
|
79
|
+
# @api public
|
80
|
+
def read_to_char(relative_path, bytes = nil, offset = nil)
|
81
|
+
buffer = ""
|
82
|
+
::File.open(relative_path) do |file|
|
83
|
+
buffer = file.read(bytes) || ""
|
84
|
+
buffer = buffer.dup.force_encoding(Encoding.default_external)
|
85
|
+
|
86
|
+
while !file.eof? && !buffer.valid_encoding? &&
|
87
|
+
(buffer.bytesize < bytes + 10)
|
88
|
+
|
89
|
+
buffer += file.read(1).force_encoding(Encoding.default_external)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
buffer
|
93
|
+
end
|
94
|
+
module_function :read_to_char
|
95
|
+
|
67
96
|
# Create checksum for a file, io or string objects
|
68
97
|
#
|
69
98
|
# @param [File, IO, String, Pathname] source
|
data/lib/tty/file/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -30,16 +30,16 @@ module Helpers
|
|
30
30
|
::File.realpath(path)
|
31
31
|
end
|
32
32
|
|
33
|
-
def fixtures_path(
|
34
|
-
::File.join(dir_path('spec', 'fixtures'),
|
33
|
+
def fixtures_path(*args)
|
34
|
+
::File.join(dir_path('spec', 'fixtures'), *args)
|
35
35
|
end
|
36
36
|
|
37
|
-
def tmp_path(
|
38
|
-
::File.join(dir_path('tmp'),
|
37
|
+
def tmp_path(*args)
|
38
|
+
::File.join(dir_path('tmp'), *args)
|
39
39
|
end
|
40
40
|
|
41
|
-
def tmp_pathname(
|
42
|
-
Pathname.new(tmp_path(
|
41
|
+
def tmp_pathname(*args)
|
42
|
+
Pathname.new(tmp_path(*args))
|
43
43
|
end
|
44
44
|
|
45
45
|
def exists_and_identical?(source, dest)
|
data/spec/unit/binary_spec.rb
CHANGED
@@ -54,6 +54,12 @@ RSpec.describe TTY::File, '#binary?' do
|
|
54
54
|
expect(TTY::File.binary?(file)).to eq(false)
|
55
55
|
end
|
56
56
|
|
57
|
+
it "indentifies a file as non-binary when greater than 4096 bytes with unicode chars", unless: RSpec::Support::OS.windows? do
|
58
|
+
file = tmp_pathname("binary", "unicode.txt")
|
59
|
+
|
60
|
+
expect(TTY::File.binary?(file)).to eq(false)
|
61
|
+
end
|
62
|
+
|
57
63
|
it "indetifies a null-terminated string file as binary" do
|
58
64
|
Tempfile.open('tty-file-binary-spec') do |file|
|
59
65
|
file.write("Binary content.\0")
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::File, "#read_to_char" do
|
4
|
+
before do
|
5
|
+
@saved_verbosity = $VERBOSE
|
6
|
+
@saved_encoding = Encoding.default_external
|
7
|
+
$VERBOSE = nil
|
8
|
+
Encoding.default_external = Encoding::UTF_8
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
Encoding.default_external = @saved_encoding
|
13
|
+
$VERBOSE = @saved_verbosity
|
14
|
+
end
|
15
|
+
|
16
|
+
it "reads file up to valid char", unless: RSpec::Support::OS.windows? do
|
17
|
+
file = tmp_pathname("binary", "unicode.txt")
|
18
|
+
bytes = 4096
|
19
|
+
|
20
|
+
content = TTY::File.read_to_char(file, bytes)
|
21
|
+
|
22
|
+
expect(content.bytesize).to eq(bytes + 2)
|
23
|
+
end
|
24
|
+
end
|
data/tasks/console.rake
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-file
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pastel
|
@@ -149,6 +149,7 @@ files:
|
|
149
149
|
- spec/unit/escape_glob_path_spec.rb
|
150
150
|
- spec/unit/inject_into_file_spec.rb
|
151
151
|
- spec/unit/prepend_to_file_spec.rb
|
152
|
+
- spec/unit/read_to_char_spec.rb
|
152
153
|
- spec/unit/remove_file_spec.rb
|
153
154
|
- spec/unit/replace_in_file_spec.rb
|
154
155
|
- spec/unit/tail_file_spec.rb
|