Class: Base85::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/base85/cli.rb

Overview

Base85 comman line interface

Author:

  • sd77

Constant Summary collapse

DEFAULT_WRAP =

Default wrap column count

76
READ_MAX_SIZE =
4096
ALPHABETS =

Known alphabet codes for –alphabet option

Base85::SUPPORTED_ALPHABETS.map(&:to_s).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeself



36
37
38
39
40
# File 'lib/base85/cli.rb', line 36

def initialize
  @decode = false
  @alphabet = :standard
  @wrap = DEFAULT_WRAP
end

Instance Attribute Details

#alphabet:standard, :ascii85 :z85 :rfc1924

Alpahbet to use

Returns:

  • (:standard, :ascii85 :z85 :rfc1924)


44
45
46
# File 'lib/base85/cli.rb', line 44

def alphabet
  @alphabet.to_sym
end

#decodeBoolean

Decode (true) or encode (false)

Returns:

  • (Boolean)


20
21
22
# File 'lib/base85/cli.rb', line 20

def decode
  @decode
end

#wrapInteger

column count after which output is wrapped

Returns:

  • (Integer)


26
27
28
# File 'lib/base85/cli.rb', line 26

def wrap
  @wrap
end

Class Method Details

.start(argv) ⇒ CLI

Create a CLI object and parse arguments

Parameters:

  • argv (Array[String])

Returns:



31
32
33
# File 'lib/base85/cli.rb', line 31

def self.start(argv)
  new.start(argv)
end

Instance Method Details

#decode_data(data) ⇒ String

Returns decoded data.

Parameters:

  • data (String)

Returns:

  • (String)

    decoded data



122
123
124
# File 'lib/base85/cli.rb', line 122

def decode_data(data)
  Base85.decode(data, alphabet: alphabet)
end

#decode_input(input) ⇒ void

This method returns an undefined value.

Decode input and print it to stdout

Parameters:

  • input (IO)


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/base85/cli.rb', line 108

def decode_input(input)
  rest = +""
  until input.eof?
    data = rest << input.read(READ_MAX_SIZE)
    ascii85_remove_markers(data)
    mod = data.size % 5
    rest = mod.zero? ? +"" : data.slice!(-mod..)
    print decode_data(data)
  end
  print decode_data(rest)
end

#encode_data(data) ⇒ String

Returns encoded data.

Parameters:

  • data (String)

Returns:

  • (String)

    encoded data



100
101
102
103
# File 'lib/base85/cli.rb', line 100

def encode_data(data)
  encoded = Base85.encode(data, alphabet: alphabet)
  ascii85_remove_markers(encoded)
end

#encode_input(input) ⇒ void

This method returns an undefined value.

Encode input and print it to stdout

Parameters:

  • input (IO)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/base85/cli.rb', line 84

def encode_input(input)
  rest = +""
  current_col = handle_ascii85_start
  until input.eof?
    data = rest << input.read(READ_MAX_SIZE)
    mod = data.size % 4
    rest = mod.zero? ? +"" : data.slice!(-mod..)
    current_col = print_wrapped(encode_data(data), current_col)
  end

  encoded_rest = handle_ascii85_end(encode_data(rest))
  print_wrapped(encoded_rest, current_col)
end

#process(input) ⇒ void

This method returns an undefined value.

Process input

Parameters:

  • input (IO)


73
74
75
76
77
78
79
# File 'lib/base85/cli.rb', line 73

def process(input)
  if decode
    decode_input(input)
  else
    encode_input(input)
  end
end

#start(argv) ⇒ self

Parse arguments

Parameters:

  • argv (Array[String])

Returns:

  • (self)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/base85/cli.rb', line 51

def start(argv)
  parser = create_option_parser
  parser.parse!(argv)

  abort("---wrap may only be used when encoding") if decode && (wrap != DEFAULT_WRAP)

  if argv.empty?
    process($stdin)
  elsif argv.size == 1
    File.open(argv.shift) do |file|
      process(file)
    end
  else
    parser.abort("Too much operands")
  end

  self
end