Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions lib/typewrite.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
class Typewrite
# frozen_string_literal: true

require_relative "typewrite/version"

# Prints console messages with a typewriter effect (letter-by-letter).
#
# @example Basic usage
# Typewrite.write("Hello, World!")
#
# @example Custom timing
# Typewrite.write("Fast typing", 0.05, 0.5, false)
#
module Typewrite
# Characters that trigger an extra pause
PUNCTUATION = [".", "?", "!"].freeze

# Prints a message with typewriter effect.
#
# @param input [String] the message to display
# @param type_rate [Float] seconds between each character (default: 0.1)
# @param punc_rate [Float] extra pause after punctuation (default: 1.5)
# @param line_break [Boolean] add newline at end (default: true)
# @return [void]
def self.write(input, type_rate = 0.1, punc_rate = 1.5, line_break = true)
input_array = input.split('')
input_array.each do |char|
input.each_char do |char|
print char
puncs = ['.','?','!',]
sleep(punc_rate) if puncs.include?(char)
sleep(punc_rate) if PUNCTUATION.include?(char)
sleep(type_rate)
end
print "\n" if line_break
Expand Down
5 changes: 5 additions & 0 deletions lib/typewrite/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module Typewrite
VERSION = "1.1.0"
end
43 changes: 31 additions & 12 deletions typewrite.gemspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
Gem::Specification.new do |s|
s.name = "typewrite"
s.version = "1.1.0"
s.summary = "Typewriter Effect!"
s.description = "A simple gem to add a typewriter effect to console output."
s.authors = ["Cory Davis"]
s.email = "CSDavisMusic@gmail.com"
s.files = ["lib/typewrite.rb"]
s.homepage =
"https://rubygems.org/gems/typewrite"
s.license = "MIT"
s.metadata = { "source_code_uri" => "https://github.com/CJGlitter/typewrite" }
# frozen_string_literal: true

require_relative "lib/typewrite/version"

Gem::Specification.new do |spec|
spec.name = "typewrite"
spec.version = Typewrite::VERSION
spec.authors = ["Cory Davis"]
spec.email = ["CSDavisMusic@gmail.com"]

spec.summary = "Typewriter effect for console output"
spec.description = "A simple gem to add a typewriter effect to console output."
spec.homepage = "https://github.com/CJGlitter/typewrite"
spec.license = "MIT"

spec.required_ruby_version = ">= 3.0.0"

spec.metadata = {
"homepage_uri" => spec.homepage,
"source_code_uri" => spec.homepage,
"changelog_uri" => "#{spec.homepage}/blob/main/CHANGELOG.md",
"bug_tracker_uri" => "#{spec.homepage}/issues",
"rubygems_mfa_required" => "true"
}

spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
f.match(%r{\A(?:test|spec|features)/})
end
end
spec.require_paths = ["lib"]
end