lib/fd.rb in fd-0.5.0 vs lib/fd.rb in fd-0.6.0
- old
+ new
@@ -5,10 +5,12 @@
# Fd dumps the content of a file to the standard output.
#
class Fd
class Error < StandardError; end
+ ESCAPE_CHARACTERS = %w[␀ ␇ ␈ ␉ ␊ ␋ ␌ ␍ ␡ ␛ ␠]
+
attr_reader :line_length, :char_table
# _line_length_ sets how many characters are displayed per @line.
# Some <i>special non-printable/invisible characters</i> are displayed as their names.
#
@@ -42,10 +44,14 @@
@char_table[16] = '␡'
@char_table[27] = '␛'
@char_table[32] = '␠'
end
+ def add_esc_sequence(chr)
+ "\e[31m#{chr}\e[0m"
+ end
+
# dumps the given file _file_name_ to stdout.
def dump(content)
raise "Not the expected encoding of UFT-8, got #{content.encoding}" unless content.encoding == Encoding::UTF_8
initialize_fields(content)
@@ -102,8 +108,10 @@
def enough_space_in_line?(byte_count_in_line, bytes)
byte_count_in_line + bytes.size <= line_length
end
def print_single_line
- puts("#{format("%#{(3 * line_length) - 1}s", hex_values.join(' '))} |#{format("%#{2 * line_length}s", line)}")
+ puts("#{format("%#{(3 * line_length) - 1}s", hex_values.join(' '))} |#{format("%#{2 * line_length}s", line)}".
+ gsub(/([#{ESCAPE_CHARACTERS.join('')}])/, add_esc_sequence('\1'))
+ )
end
end