Skip to content

Instantly share code, notes, and snippets.

@LachyGroom
LachyGroom / gist:3935512
Created October 22, 2012 23:33 — forked from idlehands/gist:3934434
color using ANSI escape codes

I have the bad habit of always using puts when I probably use print. As a a result, I was having trouble with the gems that were usable for colorizing my ToDo 3.0. I figured out a gem-free solution, using ANSI escape codes, and thought I'd share.

WARNING: don't be surprised if someone comes along and tells you this is the worst thing you could ever do. I have NO idea if it is a kosher solution, but it worked and it required VERY little change to my code. Additionally, this method works on the systems we have at DBC (and on mine) but I don't know if it is universal (I "think" it is).

A little set-up: I decided that I wanted to handle the color coding of priorities(and the keeping of the priorities) in my Todo class and not my TodoList class. Each todo keeps track of its own priority level, so I just modified the to_s method to include the color.

ANSI escape codes are used to send things to the output that aren't printed, not unlike the \ before the n of a new line. I'll show my code and then explai

article = "happy happy Sad Happy Lost"
positive = ['happy']
negative = ['sad', 'lost']
def test(article, positive, negative)
pos = 0
neg = 0
pos << article.downcase.split.count(positive)
neg << article.downcase.split.count(negative)
puts pos
@LachyGroom
LachyGroom / hack.sh
Created March 31, 2012 22:45 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
// Place your application-specific JavaScript functions and classes here
$('.ajax-link').live('click', function(){
$.getScript($(this).attr('href'));
window.location.hash = decodeURIComponent($(this).attr('href'));
return false;
});
$(function() {
var screenWidth = $(window).width();
var containerWidth = $('#container').width();
<%- image_url = @ipad.images.first.attachment ? @ipad.images.first.attachment_url(:main) : 'default.png' %>
<%= image_tag(image_url, :id => 'main-image') if @ipad.images.first %>
<ul>
<% @ipad.images.each do |image| %>
<% if image.attachment %>
<li><%= link_to image_tag(image.attachment_url(:thumb)), image, :class => "ajax-link" %></li>
<% else %>
<li><%= image_tag('default.png') %></li>
<% end %>
<%= image_tag(@ipad.images.first.attachment_url(:main), :id => 'main-image') if @ipad.images.first %>
<ul>
<% @ipad.images.each do |image| %>
<% if image.attachment_url(:thumb) %>
<li><%= link_to image_tag(image.attachment_url(:thumb)), image, :class => "ajax-link" %></li>
<% end %>
<% end %>
<%= link_to image_tag("button-share-facebook.png"), "I've just listed my" + capacity_of @ipad + "GB" + model_of @ipad + "iPad on @AppleUpgrades http://facebook.com/sharer.php?u=" + ipad_url(@ipad) %>
<% if ipad.images.first.present? %>
<%= link_to 'Pictures', ipad_path(ipad) %>
<div class="listing-preview-holder">
<% ipad.images.each do |image| %>
<%= link_to(image_tag(image.attachment_url(:thumb)), image) if image.attachment_url(:thumb).present? %>
<% end %>
</div>
<% end %>
<% if ipad.images.first.present? %>
<%= link_to 'Pictures', ipad_path(ipad) %>
<div class="listing-preview-holder">
<% ipad.images.each do |image| %>
<%= link_to(image_tag(image.attachment_url(:thumb)), image) if image.attachment_url(:thumb).present? %>
<% end %>
</div>
<% end %>
<% if ipad.images.first.present? %>
<%= link_to 'Pictures', ipad_path(ipad) %>
<div class="listing-preview-holder">
<% ipad.images.each do |image| %>
<%= link_to(image_tag(image.attachment_url(:thumb)), image) if image.attachment_url(:thumb).present? %>
<% end %>
</div>
<% end %>