Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
add color switching functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hubsmoke committed Jan 28, 2014
2 parents 259122b + 4b10921 commit a7180f3
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.gem

.rspec
.rvmrc
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
branches:
only:
- examples
- f/colour_switch
gemfile: Gemfile
notifications:
recipients:
- ben.biddington@gmail.com
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
source 'https://rubygems.org'

gemspec

group :development do
gem 'rspec'
gem 'rake'
end
14 changes: 13 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
bropages (0.0.16)
bropages (0.1.0)
commander (= 4.1.5)
highline (= 1.6.20)
json_pure (= 1.8.1)
Expand All @@ -14,15 +14,27 @@ GEM
specs:
commander (4.1.5)
highline (~> 1.6.11)
diff-lcs (1.2.5)
highline (1.6.20)
json_pure (1.8.1)
mime-types (1.19)
rake (10.1.0)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
smart_colored (1.1.1)

PLATFORMS
ruby

DEPENDENCIES
bropages!
rake
rspec
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
THE BRO IDEA
=======

[![Build Status](https://travis-ci.org/ben-biddington/bro.png?branch=examples)](https://travis-ci.org/ben-biddington/bro)

`bro`

It's more focused than `man` pages
Expand Down
5 changes: 5 additions & 0 deletions Rakefile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:all)

task :default => :all
3 changes: 1 addition & 2 deletions bin/bro
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/usr/bin/env ruby

require 'bro'
require 'bro'
20 changes: 19 additions & 1 deletion lib/bro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def require_relative(path)
require 'json'
require 'commander/import'
require 'highline'
require 'smart_colored'
require 'rest-client'

require_relative 'bro/state.rb'
Expand All @@ -32,6 +31,12 @@ def require_relative(path)

state = Bro::BroState.new({:file => FILE})

if state.check_color
ColoredText.apply
else
VanillaText.apply
end

command :thanks do |c|
c.syntax = 'bro thanks [COMMAND]'
c.summary = 'Upvote an entry, bro'
Expand Down Expand Up @@ -201,7 +206,20 @@ def require_relative(path)
c.summary = 'Lookup an entry, bro. Or just call bro [COMMAND]'
c.description = "This looks up entries in the http://bropages.org database."
c.example 'Look up the bro entries for curl', 'bro curl'
c.option '--no-color', 'Switch colored output OFF'
c.option '--with-color', 'Switch colored output ON'
c.action do |args, options|
unless options.no_color.nil?
# set no-color as default
state.write_state({ :color => Bro::BroState::COLOR_OFF })
VanillaText.apply
end
unless options.with_color.nil?
# set color as default
state.write_state({ :color => "" })
ColoredText.apply
end

if args.empty?
say <<-QQQ.unindent
#{"Bro! Specify a command first!".colored.red}
Expand Down
8 changes: 8 additions & 0 deletions lib/bro/bro_state.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module Bro
class BroState < State
COLOR_OFF = "nocolor"

# true/false if color should be used
def check_color
state = read_state
return state[:color] != COLOR_OFF
end

def get_arg_or_last_command args
cmd = args.join(" ")
if args.empty?
Expand Down
72 changes: 55 additions & 17 deletions lib/bro/string_hacks.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,63 @@
class String
def unindent
gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
end
class FakeColor
def initialize(text); @text = text; end

def status
self.colored.yellow
%w{ red green yellow blue underline }.each do |m|
define_method(m){ @text }
end
end

def success
self.colored.green.bold
end
class VanillaText
class << self
def apply
String.class_eval do
def colored
FakeColor.new self
end

def unindent
gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
end
end

def problem
self.colored.yellow_on_red_bold
%w{ status success problem sorry important underline }.each do |m|
String.class_eval do
define_method(m){ self }
end
end
end
end
end

def sorry
self.colored.red.bold
end
class ColoredText
class << self
def apply
String.class_eval do
require 'smart_colored'

def unindent
gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
end

def status
self.colored.yellow
end

def success
self.colored.green.bold
end

def problem
self.colored.yellow_on_red_bold
end

def sorry
self.colored.red.bold
end

def important
self.colored.magenta
def important
self.colored.magenta
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rspec'

dirname = File.expand_path File.dirname(__FILE__)

Dir.glob(File.join(dirname, "support", "**", "*.rb")).each{|f| require f}
9 changes: 9 additions & 0 deletions spec/support/bro_cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class BroCli
class << self
def run(command)
exe = File.join ".", "spec", "system.tests", "bin", "bro"

`#{exe} #{command}`
end
end
end
9 changes: 9 additions & 0 deletions spec/support/bro_system_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module BroSystemTest
def bro(command)
BroCli.run command
end

def bleach(text)
ColourBlind.strip text
end
end
5 changes: 5 additions & 0 deletions spec/support/color_blind.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ColourBlind
def self.strip(text)
text.gsub(/\e\[(\d+)m/, '')
end
end
3 changes: 3 additions & 0 deletions spec/system.tests/bin/bro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path(File.join("..", "bro", "lib"))
require 'bro'
4 changes: 4 additions & 0 deletions spec/system.tests/bin/bro~
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path(File.join("..", "bro", "lib"))
puts $LOAD_PATH
require 'bro'
19 changes: 19 additions & 0 deletions spec/system.tests/color_switch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "spec_helper"

describe "The color switch" do
include BroSystemTest

let(:any_ansi_escape_sequence) { /\e\[(\d+)m/ }

it "you can turn color off" do
result = bro "--no-color"
expect(result).to_not match any_ansi_escape_sequence
end

it "defaults to color on" do
result = bro ""
expect(result).to match any_ansi_escape_sequence
end

it "might be nice to cache the switch on disk in case you never want color"
end
45 changes: 45 additions & 0 deletions spec/system.tests/examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'spec_helper'

module BroSystemTest
def bro(command)
BroCli.run command
end
end

describe "Basic examples" do
include BroSystemTest

it "can ask about curl for example" do
result = bro "curl"

expect(result).to match /[\d+] entries for curl/
end

it "tells me when there is no manual for a command" do
an_unknown_command = "xxx_unknown_command_xxx"

result = bro an_unknown_command

expect(result).to match /The #{an_unknown_command} command isn\'t in our database/
end

it "you can turn color off" do
result = bro "curl --no-color"
expect(result).to match /[\d+] entries for curl/
end
end

class BroCli
class << self
def run(command)
path = File.join ".", "spec", "system.tests", "bin", "bro"
ColourBlind.strip `#{path} #{command}`
end
end
end

class ColourBlind
def self.strip(text)
text.gsub(/\e\[(\d+)m/, '')
end
end
7 changes: 7 additions & 0 deletions spec/system.tests/examples.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

describe "Basic examples" do
it "can be asked about a simple command" do

end
end
19 changes: 19 additions & 0 deletions spec/system.tests/examples_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

describe "Basic examples" do
include BroSystemTest

it "can ask about curl for example" do
result = bro "curl"

expect(result).to match /[\d+] entries for curl/
end

it "tells me when there is no manual for a command" do
an_unknown_command = "xxx_unknown_command_xxx"

result = bleach (bro an_unknown_command)

expect(result).to match /The #{an_unknown_command} command isn\'t in our database/
end
end

0 comments on commit a7180f3

Please sign in to comment.