Skip to content

Instantly share code, notes, and snippets.

@epitron
epitron / ansi2html.rb
Created January 28, 2012 10:02
Convert ANSI to HTML
require 'epitools'
class State
attr_accessor :fore, :back, :attrs
COLORS = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
ATTRS = {
0 => :reset,
@epitron
epitron / hash-sets.rb
Created January 18, 2012 18:29
Hash with set operations
#
# The original idea was to use set operations to help dealing with a method's "options" hash.
#
# However, since I can't override || and &&, I wasn't able to figure out reasonable semantics.
#
# Therefore, I implemented `with_defaults`, which is what you want most of the time, and then
# wrote some thoughts about how set operations might work.
#
class Hash
@epitron
epitron / sample-plugin.rb
Created January 10, 2012 03:28
An example of a non-class-based way of sticking metadata onto a command.
Pry.plugin("something") do
command "amazing" do
...
end.help do
short "blah"
long "A long description."
end
command /more-amazing!*/ do
#
# An Array-like class that's used for a very specific purpose: the `out[]` array
# in Pry. MRU stands for most-recently-used.)
#
# It acts like an array with a maximum size -- if you try to add more elements than
# the maximum, the elements at the beginning of the array are discarded.
#
# Also, it keeps track of which entries are most recently accessed, and throws
# out only the oldest entries.
#
require 'curses'
def init_screen
Curses.noecho # do not show typed keys
Curses.init_screen
Curses.start_color
Curses.stdscr.keypad(true) # enable arrow keys
Curses.curs_set(0)
Curses.ESCDELAY = 0
@epitron
epitron / pry-fancy_requre.rb
Last active September 25, 2015 15:37
A require command that also prints out all the modules/classes that were loaded into Ruby!
def req(mod)
puts " |_ #{mod}"
require mod
yield if block_given?
rescue Exception => e
p e
end
## Fancy Require w/ Modules
#
# Creates a new CommandSet and automatically mixes it into Pry::Commands.
#
Pry.plugin :test do
settings do
bool :make_awesome, :default => true
enum :goat_type, :default => "plain", :options => %w[ plain mountain billy ]
string :favorite_goat, :default => "Super Dave the Goat", :validate => proc { goat[/super/i] }
int :expire_goats, :default => 5.minutes
@epitron
epitron / plugin_example.rb
Created April 22, 2011 07:02
possible pry plugin parameterization
#
# The result is an anonymous module (named "name") that can be mixed into the
# Commands class.
#
Pry.plugin "name" do
## Settings
settings do
bool :make_awesome, :default => true
@epitron
epitron / gems-command.rb
Last active September 25, 2015 14:28
A "gems" command for Pry
command "gems", "List/search installed gems. (Optional parameter: a regexp to limit the search)" do |arg|
gems = Gem.source_index.gems.values.group_by(&:name)
if arg
query = Regexp.new(arg, Regexp::IGNORECASE)
gems = gems.select { |gemname, specs| gemname =~ query }
end
gems.each do |gemname, specs|
versions = specs.map(&:version).sort.reverse.map(&:to_s)
versions = [bright_green(versions.first)] +
command "require", "Require gem(s)" do |*gems|
gems = gems.join(' ').gsub(',', '').split(/\s+/)
gems.each do |gem|
begin
if require gem
output.puts "#{gem.yellow} loaded"
else
output.puts "#{gem.white} already loaded"
end
rescue LoadError => e