Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Created Aug 8, 2012
Embed
What would you like to do?
# To install prerequisites for this sample code, run
# the following commands in a directory with all the
# files in this gist:
#
# > gem install bundler
# > bundler install
source "http://rubygems.org/"
gem "thor", "0.15.4"
gem "lolcat", "42.0.99"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
sollicitudin erat sed justo elementum nec dictum nibh placerat. Integer
scelerisque tortor nec dui hendrerit nec ullamcorper quam gravida. In sit amet
turpis erat, non rhoncus nisi. Aliquam erat volutpat. Nulla ac ligula purus, et
aliquet dui. Ut nec vestibulum nisl. Morbi malesuada arcu nec sapien pulvinar
non ultrices orci tincidunt. Sed eleifend, urna non gravida porttitor, elit
ligula eleifend sapien, eu sodales ligula velit eget lorem. Sed consequat
convallis dolor sed porttitor. In at nibh ac elit mattis varius.
In hac habitasse platea dictumst. Mauris blandit accumsan nibh, a suscipit
lectus ornare sed. Nunc lacinia, odio nec euismod varius, risus augue laoreet
orci, semper semper felis urna quis diam. Nulla vitae quam quis massa bibendum
hendrerit. Nulla luctus dictum egestas. Nulla id nulla nec mauris tincidunt
sodales quis ac sapien. Sed quis ipsum mauris.
Etiam viverra mollis neque, eget bibendum odio mattis sed. Etiam nisi eros,
molestie ac consequat id, suscipit nec lorem. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Cras vestibulum dolor eu tortor dictum ac viverra
nisl tincidunt. Donec et libero leo, quis sollicitudin augue. Vivamus justo
elit, laoreet sit amet egestas non, blandit vitae metus. Curabitur sit amet
nunc nec leo laoreet eleifend. Aenean venenatis erat a lacus luctus nec
porttitor ipsum laoreet. Proin hendrerit mattis rutrum. Vestibulum ante ipsum
primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin euismod
elementum enim id facilisis. Praesent egestas auctor diam, ut eleifend mi
auctor vitae.
Mauris nec magna sed tellus porttitor porttitor id sed augue. Nunc luctus
lectus eget purus posuere lobortis. Mauris aliquam ligula sit amet sem tempor
quis bibendum nulla tincidunt. Integer venenatis convallis semper. Suspendisse
lacinia felis nec diam pellentesque eu accumsan leo ultricies. Morbi molestie
pulvinar rutrum. Vivamus ultricies purus sed purus consequat volutpat nec et
erat. Quisque quis ligula est. Sed gravida sollicitudin sem, et iaculis quam
eleifend ut. Nulla viverra sagittis tellus, ut imperdiet mauris auctor id.
Donec viverra lorem sit amet velit bibendum vel tristique urna dignissim.
Nullam posuere fringilla orci, sit amet euismod quam aliquet quis. Duis
fermentum mauris orci. Suspendisse a dui ante, ut pellentesque sem. Sed euismod
pellentesque volutpat. Nullam pellentesque leo in orci elementum blandit. Nam
fermentum condimentum elit, porta vehicula massa lobortis tempor. Nullam
eleifend ultrices sapien, non adipiscing ligula luctus at. Suspendisse
molestie, elit vitae vehicula rhoncus, purus nunc consequat arcu, at venenatis
dui lectus sit amet orci. Sed mattis aliquam consequat. Phasellus aliquam quam
ut elit semper at commodo dui tincidunt. Aenean at nunc in velit vulputate
pharetra. Suspendisse luctus adipiscing massa, non porttitor lorem porta sed.
module Tools
module_function
def grep(file, pattern, options={})
regex = /#{pattern}/
lines = []
File.open(file, "r").lines.each_with_index do |line, index|
if xor(line.match(regex), options[:invert])
lines << sprintf("%3d: %s", index + 1, line)
end
end
output(lines.join, options[:moar_lols])
end
def xor(a,b)
(a and (not b)) or ((not a) and b)
end
def output(str, with_lols = false)
if with_lols
require 'lolcat'
Lol.cat(StringIO.new(str), {
animate: false,
duration: 12,
os: 0,
speed: 20,
spread: 8.0,
freq: 0.3
})
else
puts str
end
end
end
#!/usr/bin/env ruby
$LOAD_PATH.unshift(".")
require 'thor'
require 'tools'
class ToolsApp < Thor
def initialize(*args)
super
# This is optional. If you need to execute something when the program
# starts, you can stuff it after the `super` call.
end
class_option :moar_lols, type: :boolean,
default: false,
desc: "Turn on moar lols."
desc "grep [file] [pattern]", "Print lines from [file] matching [pattern]"
method_option :invert, type: :boolean,
default: false,
desc: "Only print lines that don't match pattern."
def grep(file, pattern)
Tools.grep(file, pattern, options)
end
end
ToolsApp.start
@safarista

This comment has been minimized.

Copy link

@safarista safarista commented Sep 17, 2012

As a learner, how do I run this scripts? How do I make them come alive. Almost all the demo's on Thor, Clamp etc do not say how the scripts are executed.

Do i run them in a terminal or do I include them in other scripts? That is the part that is missing or ignored assuming the readers of this wonderful tool are pro programmers.

Hope you can help.

@safarista

This comment has been minimized.

Copy link

@safarista safarista commented Sep 17, 2012

As a learner, how do I run this scripts? How do I make them come alive. Almost all the demo's on Thor, Clamp etc do not say how the scripts are executed.

Do i run them in a terminal or do I include them in other scripts? That is the part that is missing or ignored assuming the readers of this wonderful tool are pro programmers.

Hope you can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment