line-detector 0.2 → 0.3

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
  SHA1:
3
- metadata.gz: cc7429071fc4b90421939e9f2efa635af6706ffd
4
- data.tar.gz: 62d454bedea6b374d17ff18aa29aa5b7148ca624
3
+ metadata.gz: c363e46237d48d94fe3081fbfc6541f90febaabb
4
+ data.tar.gz: 487364d2c3cd7c916fea9d71a12043034d15b53f
5
5
  SHA512:
6
- metadata.gz: d1ef12f759a63cf88182305b5eb334d7fff9accf0fe08968084e7ac9a35be8844962a7ab473793b741477dc26b4887dc59b714255a8fd8ec5c78c343448834ed
7
- data.tar.gz: 0097e756cd16d68987f7a56fabb23f4cde41050de9d63dc86408f97d8b86c6634a35654cb423311c06b9188c07a39b3b79f2a2cd5d32e228a38287ffe95d110a
6
+ metadata.gz: 6fbac38b0ae97d7b874185df209847f45c1ecf574cad889358ad48c20111c4bdcbd3afc867a6e5c47648f4cecc4c760d21cef33e70e0b105f22a9bdf94704763
7
+ data.tar.gz: 0c2edf9190fc65496a7f7296ac4eb59395b3e056f7cf156597e095c98bbfec42c81299df7e36a1b3f64ede57da0dc58f4bc0b680f35d02681dd1c5ef42fa7062
@@ -28,11 +28,14 @@ def main
28
28
  filename = ARGV.first unless ARGV.empty?
29
29
 
30
30
  if filename == '-'
31
- puts LineDetector.detect_line_ending_of_text(STDIN.read).to_s
31
+ text = STDIN.read
32
+ report = LineDetector.report_of_text(text)
33
+ puts report
32
34
  elsif !File.exists?(filename)
33
35
  puts "#{filename} does not exist"
34
36
  else
35
- puts "#{filename}: #{LineDetector.detect_line_ending_of_file(filename).to_s}"
37
+ report = LineDetector.report_of_file(filename)
38
+ puts "#{filename}: #{report}"
36
39
  end
37
40
  end
38
41
 
@@ -53,6 +53,28 @@ module LineDetector
53
53
  end
54
54
  end
55
55
 
56
+ #
57
+ # Detects presence of final end of line in arbitrary text
58
+ #
59
+ # "abc\ndef\n" => true
60
+ # "abc\r\ndef\r\n" => true
61
+ # "abc\ndef" => false
62
+ # "abc\r\ndef\r" => false
63
+ # "abcdef" => false
64
+ # "" => false
65
+ #
66
+ def self.detect_final_eol_of_text(text)
67
+ line_ending = detect_line_ending_of_text(text)
68
+
69
+ if line_ending == :none
70
+ false
71
+ elsif line_ending == :mix
72
+ false
73
+ else
74
+ text.end_with?(NAME2EOL[line_ending])
75
+ end
76
+ end
77
+
56
78
  #
57
79
  # Detect line ending format of a file
58
80
  #
@@ -62,6 +84,54 @@ module LineDetector
62
84
  detect_line_ending_of_text(open(filename).read)
63
85
  end
64
86
 
87
+ #
88
+ # Detect presence of final end of line in a file
89
+ #
90
+ # Assumes file is a text file.
91
+ #
92
+ def self.detect_final_eol_of_file(filename)
93
+ detect_final_eol_of_text(open(filename).read)
94
+ end
95
+
96
+ class Report
97
+ attr_accessor :line_ending, :final_eol
98
+
99
+ def initialize(line_ending, final_eol)
100
+ @line_ending = line_ending
101
+ @final_eol = final_eol
102
+ end
103
+
104
+ def to_s
105
+ final_eol_piece =
106
+ if @final_eol
107
+ ', with final eol'
108
+ else
109
+ ', without final eol'
110
+ end
111
+
112
+ "#{@line_ending.to_s}#{final_eol_piece}"
113
+ end
114
+ end
115
+
116
+ #
117
+ # Report line ending and presence of final line ending of arbitrary text
118
+ #
119
+ def self.report_of_text(text)
120
+ Report.new(
121
+ detect_line_ending_of_text(text),
122
+ detect_final_eol_of_text(text)
123
+ )
124
+ end
125
+
126
+ #
127
+ # Detect presence of final end of line in a file
128
+ #
129
+ # Assumes file is a text file.
130
+ #
131
+ def self.report_of_file(filename)
132
+ report_of_text(open(filename).read)
133
+ end
134
+
65
135
  #
66
136
  # A more capable version of String#lines,
67
137
  # that handles some of the more obscure line ending formats.
@@ -2,5 +2,5 @@
2
2
  # LineDetector
3
3
  #
4
4
  module LineDetector
5
- VERSION = '0.2'
5
+ VERSION = '0.3'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: line-detector
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Pennebaker