fd 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f47b8695d25dbba024c4a5e44d471179919de3f7888a7dfac299d4b2f6e94224
4
- data.tar.gz: 6f3fc8a90d2a5a355cfd3971fb795506ae875d80c1358687bcb299cad1c65052
3
+ metadata.gz: 67057a4915f19574440eb06dbfd3c3ebd6f59a8246d7a7760698e36781ea1162
4
+ data.tar.gz: 5b145d3b97f0172cf9c30b7e074c4329455c2153350964bcbacce2f190de7508
5
5
  SHA512:
6
- metadata.gz: 388945f903ac60a83c25d0ee8e5ed51ebabe28b9889bc138daa30f2af1e9c1eab8e603cece9f788782e7316ac7c3e154fad5ea6bc42cf039c8635d07555b78c6
7
- data.tar.gz: d0c163a40a65fbc22059b6a116531e6a759dfa5b422e32c36deb5c53a12ab7b8f135295934616a84304b4fc63cd46502e8fb02d700d7b282ad69877a2cd4ed82
6
+ metadata.gz: 988fb2280aa7ba07a6881055a753b9fbc2fc911ed72e78497c64720306af54d95b07fabab67e8b6ed7fd101c83922fc7170bc02099e08a36e0a5e23bf35ae13e
7
+ data.tar.gz: 28b4fc04117655e26e73a9a6d3e24ebfc104feb9d09b611dfbddcb76c4602cd356a449db5bbc58e7fb36b44d4bc1d2a1c07605fddb5d8f6bc2792b52eb09d9d5
@@ -1,4 +1,4 @@
1
- name: Ruby
1
+ name: Ruby Tests
2
2
 
3
3
  on: [push,pull_request]
4
4
 
data/README.md CHANGED
@@ -1,28 +1,30 @@
1
1
  # fd - A simple file dumping tool
2
2
 
3
- ## GitHub Actions
3
+ ## Badges!
4
4
 
5
- [![Main workflow: unit tests](https://github.com/s2k/fd/actions/workflows/main.yml/badge.svg)](https://github.com/s2k/fd/actions) <sup style="font-size:125%;">᛫</sup> [![CodeQL for 'fd'](https://github.com/s2k/fd/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/s2k/fd/actions/workflows/codeql-analysis.yml)
5
+ [![Main workflow: unit tests](https://github.com/s2k/fd/actions/workflows/main.yml/badge.svg)](https://github.com/s2k/fd/actions) <sup style="font-size:125%;">᛫</sup> [![CodeQL for 'fd'](https://github.com/s2k/fd/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/s2k/fd/actions/workflows/codeql-analysis.yml) <sup style="font-size:125%;">᛫</sup> [![Gem Version](https://badge.fury.io/rb/fd.svg)](https://badge.fury.io/rb/fd) <sup style="font-size:125%;">᛫</sup> [![Maintainability](https://api.codeclimate.com/v1/badges/a85527d101c9ed8f581b/maintainability)](https://codeclimate.com/github/s2k/fd/maintainability)
6
6
 
7
7
  ## TL;DR
8
8
 
9
- `fd` reads the file _as a sequence of bytes_ and dumps the file content to _STDOUT_, thus the name `fd`. It does so in two columns, the left one will display the hex values of the bytes in the file, the right one will display the characters.
9
+ `fd` reads files _as a sequence of (UTF-8) characters_ and dumps the content to _STDOUT_, thus the name `fd`. It does so in two columns, the left one will display the hex values of the bytes in the file, the right one will display the characters.
10
10
 
11
- ## Warning
11
+ ## History
12
+
13
+ I created this tool **back in 2004**. It was programmed in a different world: On another operating system, using another file system (which most notably didn't have the concept of case-sensitive file names). This version originally used ISO-8859-1 as the default (and only) encoding, while it now assumes UTF-8.
12
14
 
13
- This is, essentially, the same version I created **back in 2004**. It was programmed in a different world: On another operating system, using another file system (which most notably didn't have the concept of case-sensitive file names). This version originally used ISO-8859-1 as the default encoding, while it's now UTF-8. But since it reads the file byte wise, some characters will not be displayed as you would see them in a modern text editor.
15
+ The early versions had it easy: Each character was assumed to use one byte in the file. This made displaying it in rows with a constant number of characters easy. Nowadays, however, a singe character may be composed of a (varying) number of bytes, making it impossible to always display the same number of characters in each row.
16
+ This is just a small aspect of how character encoding is more complicated (and more complex).
14
17
 
15
- In other words: It's a work in progress.
16
18
 
17
19
  ## Installation
18
20
 
19
- Add this line to your application's Gemfile:
21
+ If you're using a `Gemfile`, add this line to make the tool available in your project:
20
22
 
21
23
  ```ruby
22
24
  gem 'fd'
23
25
  ```
24
26
 
25
- And then execute:
27
+ Then execute:
26
28
 
27
29
  $ bundle install
28
30
 
@@ -30,24 +32,15 @@ Or install it yourself as:
30
32
 
31
33
  $ gem install fd
32
34
 
33
-
34
35
  ## Usage
35
36
 
36
37
  ```
37
- fd.rb [-w _number_] <em>file_name(s)</em>
38
- ```
39
-
40
- Depending on your operating system, you may have to type a bit more...
41
-
42
- ```
43
- ruby fd.rb [-w _number_] file_name(s)
38
+ fd [-w _number_] file_name [file_name]
44
39
  ```
45
40
 
46
41
  ## Development
47
42
 
48
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
-
50
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
44
 
52
45
  ## Contributing
53
46
 
@@ -55,4 +48,4 @@ Bug reports and pull requests are welcome on GitHub at <https://github.com/s2k/f
55
48
 
56
49
  ## License
57
50
 
58
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+ The gem is available under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/lib/fd/version.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # All other code is in lib/fd.rb
5
5
  #
6
6
  class Fd
7
- VERSION = '0.3.0'
7
+ VERSION = '0.3.1'
8
8
 
9
9
  def self.version
10
10
  VERSION
data/lib/fd.rb CHANGED
@@ -9,7 +9,7 @@ class Fd
9
9
 
10
10
  attr_reader :line_length, :char_table
11
11
 
12
- # _line_length_ sets how many characters are displayed per line.
12
+ # _line_length_ sets how many characters are displayed per @line.
13
13
  # Some <i>special non-printable/invisible characters</i> are displayed as their names.
14
14
  #
15
15
  # Name :: Char val
@@ -50,39 +50,54 @@ class Fd
50
50
  content = File.read(file_name)
51
51
  raise "Not the expected encoding of UFT-8, got #{content.encoding}" unless content.encoding == Encoding::UTF_8
52
52
 
53
- chars = content.chars
54
- byte_count_in_line = 0
55
- line = ''
56
- hex_values = []
57
- char_index = 0
58
- while char_index < chars.size
59
- char = chars[char_index]
60
- bytes = char.bytes
61
- if enough_space_in_line?(byte_count_in_line, bytes)
62
- # Next char fits in line => Add hex values & character to line
63
- byte_count_in_line += bytes.size
64
- bytes.each { |bt| hex_values << format('%02x', bt) }
65
- line += format('%5s', (char_table[char.ord] || char))
66
- char_index += 1
67
- else
68
- # Print a new line…
69
- print_single_line(hex_values, line)
70
- # …and reset line internal values
71
- byte_count_in_line = 0
72
- hex_values.clear
73
- line = ''
74
- end
53
+ initialize_fields(content)
54
+ while @char_index < @chars.size
55
+ process_current_character
75
56
  end
76
- print_single_line(hex_values, line) unless line.empty?
57
+ print_single_line unless line.empty?
77
58
  end
78
59
 
79
60
  private
80
61
 
62
+ def process_current_character
63
+ char = @chars[@char_index]
64
+ bytes = char.bytes
65
+ if enough_space_in_line?(@byte_count_in_line, bytes)
66
+ append_to_line(bytes, char)
67
+ else
68
+ print_single_line
69
+ reset_line
70
+ end
71
+ end
72
+
73
+ def initialize_fields(content)
74
+ @chars = content.chars
75
+ @byte_count_in_line = 0
76
+ @line = ''
77
+ @hex_values = []
78
+ @char_index = 0
79
+ end
80
+
81
+ attr_reader :hex_values, :line
82
+
83
+ def reset_line
84
+ @byte_count_in_line = 0
85
+ @hex_values.clear
86
+ @line = ''
87
+ end
88
+
89
+ def append_to_line(bytes, char)
90
+ @byte_count_in_line += bytes.size
91
+ bytes.each { |bt| @hex_values << format('%02x', bt) }
92
+ @line += format('%5s', (char_table[char.ord] || char))
93
+ @char_index += 1
94
+ end
95
+
81
96
  def enough_space_in_line?(byte_count_in_line, bytes)
82
97
  byte_count_in_line + bytes.size <= line_length
83
98
  end
84
99
 
85
- def print_single_line(hex_values, line)
100
+ def print_single_line
86
101
  puts("#{format("%#{(3 * line_length) - 1}s", hex_values.join(' '))} |#{format("%#{5 * line_length}s", line)}")
87
102
  end
88
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kämper