Class: RandomWords::Terminal
- Inherits:
-
Object
- Object
- RandomWords::Terminal
- Defined in:
- lib/random-words/terminal.rb
Overview
Terminal methods for testing
Instance Method Summary collapse
- #colorize_text(text, color) ⇒ Object
- #colors ⇒ Object
-
#combined_sentences(length = nil) ⇒ Object
Generate and print random combined sentences.
- #counter(length) ⇒ Object
- #header1(text) ⇒ Object
- #header2(text) ⇒ Object
-
#initialize(source = :english) ⇒ Terminal
constructor
A new instance of Terminal.
- #language ⇒ Object
- #marker ⇒ Object
- #paragraphs(length, count = 3) ⇒ Object
- #print_test ⇒ Object
-
#random_characters ⇒ Object
Generate and print specified number of characters.
- #random_paragraphs ⇒ Object
- #random_sentences ⇒ Object
-
#random_words ⇒ Object
Generate and print specified number of words.
- #sentence(length) ⇒ Object
- #text(text) ⇒ Object
Constructor Details
#initialize(source = :english) ⇒ Terminal
Returns a new instance of Terminal.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/random-words/terminal.rb', line 6 def initialize(source = :english) # Create an instance of the generator @sentence_generator = RandomWords::Generator.new(source) @sentence_generator.use_extended_punctuation = true @colors = { text: :yellow, counter: :boldcyan, marker: :boldgreen, bracket: :cyan, language: :boldmagenta } @sources = @sentence_generator.sources.keys.map(&:to_s) end |
Instance Method Details
#colorize_text(text, color) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/random-words/terminal.rb', line 42 def colorize_text(text, color) return text unless $stdout.isatty return text unless colors.key?(color) color_code = colors[color] "\e[#{color_code}m#{text}\e[0m" end |
#colors ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/random-words/terminal.rb', line 20 def colors { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, boldblack: '1;30', boldred: '1;31', boldgreen: '1;32', boldyellow: '1;33', boldblue: '1;34', boldmagenta: '1;35', boldcyan: '1;36', boldwhite: '1;37', reset: 0 } end |
#combined_sentences(length = nil) ⇒ Object
Generate and print random combined sentences
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/random-words/terminal.rb', line 84 def combined_sentences(length = nil) number_of_sentences = length || 2 header1('Random Combined Sentences:') @sources.count.times do |i| @sentence_generator.source = @sources[i - 1] @sentence_generator.sentence_length = :medium @sentence_generator.paragraph_length = number_of_sentences @sentence_generator.sentences(number_of_sentences) s = @sentence_generator.sentence puts "#{marker} #{text(s)}" puts counter("#{s.split(/ /).count} words, #{s.length} characters") puts colorize_text(' ' * 2, :boldwhite) end end |
#counter(length) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/random-words/terminal.rb', line 170 def counter(length) "#{colorize_text('[', @colors[:bracket])}#{colorize_text(length.to_s, @colors[:counter])}#{colorize_text(']', @colors[:bracket])} #{language}" end |
#header1(text) ⇒ Object
52 53 54 55 56 |
# File 'lib/random-words/terminal.rb', line 52 def header1(text) puts colorize_text("\n\n#{text}", :boldgreen) puts colorize_text('=' * text.length, :boldgreen) puts "\n" end |
#header2(text) ⇒ Object
58 59 60 61 62 |
# File 'lib/random-words/terminal.rb', line 58 def header2(text) puts colorize_text("\n\n#{text}", :boldyellow) puts colorize_text('-' * text.length, :boldyellow) puts "\n" end |
#language ⇒ Object
163 164 165 166 167 168 |
# File 'lib/random-words/terminal.rb', line 163 def language "#{colorize_text('(', @colors[:bracket])}#{colorize_text(@sentence_generator.source, @colors[:language])}#{colorize_text(')', @colors[:bracket])}" end |
#marker ⇒ Object
155 156 157 |
# File 'lib/random-words/terminal.rb', line 155 def marker colorize_text('•', @colors[:marker]) end |
#paragraphs(length, count = 3) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/random-words/terminal.rb', line 64 def paragraphs(length, count = 3) @sentence_generator.sentence_length = length @sentence_generator.paragraph_length = count @sentence_generator.source = @sources.sample header2("Random Paragraph (#{@sentence_generator.paragraph_length} #{@sentence_generator.sentence_length} sentences)") graf = @sentence_generator.paragraph puts text(graf) puts counter("#{graf.split(/ /).count} words, #{graf.length} characters") end |
#print_test ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/random-words/terminal.rb', line 177 def print_test combined_sentences(3) random_sentences random_paragraphs random_words random_characters end |
#random_characters ⇒ Object
Generate and print specified number of characters
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/random-words/terminal.rb', line 134 def random_characters header1('Random Characters (exact length)') [20, 50, 120, 200, 500].each_with_index do |i, index| @sentence_generator.source = @sources[index] chars = @sentence_generator.characters(i, whole_words: true) puts "#{marker} #{colorize_text("#{i}:", :boldwhite)} #{text(chars)} #{counter(chars.length)}" end # Generate and print specified number of characters max_characters = [15, 25, 53, 110, 600] min_characters = [10, 20, 50, 100, 500] header1('Random Characters (length range)') max_characters.count.times do |i| @sentence_generator.source = @sources[i] chars = @sentence_generator.characters(min_characters[i], max_characters[i], whole_words: true) range = "#{marker} #{colorize_text("[#{min_characters[i]}-#{max_characters[i]}]: ", :boldwhite)}" puts "#{range}#{text(chars)} #{counter(chars.length)}" end end |
#random_paragraphs ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/random-words/terminal.rb', line 108 def random_paragraphs header1('Random Paragraphs') @sentence_generator.lengths.keys.each_with_index do |length, index| @sentence_generator.source = @sources[index] paragraphs(length, 3) end end |
#random_sentences ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/random-words/terminal.rb', line 100 def random_sentences header1('Random Sentences') @sentence_generator.lengths.keys.each_with_index do |length, index| @sentence_generator.source = @sources[index] sentence(length) end end |
#random_words ⇒ Object
Generate and print specified number of words
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/random-words/terminal.rb', line 117 def random_words number_of_words = [] @sources.count.times do |i| number_of_words << (((i * i) * 5) + 10) end header1("#{number_of_words} Random Words") @sources.each_with_index do |source, index| header2("#{number_of_words[index]} Random Words") @sentence_generator.source = source s = @sentence_generator.words(number_of_words[index]) puts "#{marker} #{text(s)} " puts counter("#{s.split(/ /).count} words, #{s.length} characters") end end |
#sentence(length) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/random-words/terminal.rb', line 75 def sentence(length) header2("Random #{length} sentence") @sentence_generator.sentence_length = length s = @sentence_generator.sentence puts text(s) puts counter("#{s.split(/ /).count} words, #{s.length} characters") end |
#text(text) ⇒ Object
159 160 161 |
# File 'lib/random-words/terminal.rb', line 159 def text(text) colorize_text(text, @colors[:text]) end |