Skip to content

Commit

Permalink
Unify seed values for displacement and replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Aug 7, 2017
1 parent b68bcbc commit 3711112
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/typhar/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Typhar
end

class Encode < ::Cli::Command
@seed : UInt32?

class Options
arg "source_text_file", required: true, desc: "source text file"
arg "message", required: true, desc: "message"
Expand Down Expand Up @@ -47,29 +49,23 @@ module Typhar
caption "encode message"
end

def run
def run(io = STDOUT)
plaintext_scanner = ::IO::Memory.new(args.message)
displacement_strategy = options.displacement_strategy
replacement_strategy = options.replacement_strategy
source_file = File.open(args.source_text_file)

begin
source_file = File.open(args.source_text_file)

transformer = Typhar::Transformer.new(
plaintext_scanner,
source_file,
displacement_strategy_for(displacement_strategy, plaintext_scanner, options),
replacement_strategy_for(replacement_strategy, options)
).transform(STDOUT)
puts
ensure
source_file.close if source_file
end
transformer = Typhar::Transformer.new(
plaintext_scanner,
source_file,
displacement_strategy_for(displacement_strategy, plaintext_scanner, options),
replacement_strategy_for(replacement_strategy, options)
).transform(io)
ensure
source_file.close if source_file
end

private def displacement_strategy_for(strategy, plaintext_scanner, options)
seed = options.seed.empty? ? generate_seed : options.seed.to_u32

case strategy
when "word-offset"
word_offset = options.word_offset.to_i
Expand All @@ -83,8 +79,6 @@ module Typhar
end

private def replacement_strategy_for(strategy, options)
seed = options.seed.empty? ? generate_seed : options.seed.to_u32

case strategy
when "n-shifter"
codepoint_shift = options.codepoint_shift.to_i
Expand All @@ -94,8 +88,14 @@ module Typhar
end
end

private def seed
@seed ||= options.seed.empty? ? generate_seed : options.seed.to_u32
end

private def generate_seed
SecureRandom.hex(4).to_u32(16)
s = SecureRandom.hex(4).to_u32(16)
STDERR.puts "INFO: Using #{s} as seed"
s
end
end

Expand Down

0 comments on commit 3711112

Please sign in to comment.