Skip to content

Commit

Permalink
Add basic search for string
Browse files Browse the repository at this point in the history
The search is case insensitive. The terminal display is colorized
thanks to the rainbow gem.
  • Loading branch information
lkdjiin committed Apr 23, 2013
1 parent ec892e3 commit 948f4f6
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 4 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
9 changes: 8 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
Ackr
================

Ackr is a very light grep/ack/rak replacement for lazy developers.

Description
-----------
TODO

TODO. Work in progress.

Run on linux. Maybe run on mac os. I guess it doesn't run on windows.

Install
-------------------------

gem install ackr

Usage
--------------------------

TODO. Work in progress.


Dependencies
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ task :default => :spec

desc 'Test GEM with rspec'
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = ['--color --format documentation']
t.rspec_opts = ['--color']
end

desc 'Check for code smells'
Expand Down
5 changes: 3 additions & 2 deletions ackr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Gem::Specification.new do |s|
s.version = File.read('VERSION').strip
s.authors = ['Xavier Nayrac']
s.email = 'xavier.nayrac@gmail.com'
s.summary = ''
s.summary = 'Ackr is a very light grep/ack/rak replacement for lazy developers.'
s.homepage = ''
s.description = %q{}
s.description = %q{Ackr is a very light grep/ack/rak replacement for lazy developers.}

readmes = FileList.new('*') do |list|
list.exclude(/(^|[^.a-z])[a-z]+/)
Expand All @@ -19,4 +19,5 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.required_ruby_version = '>= 1.9.2'
s.executables = ['ackr']
s.add_dependency 'rainbow', '>= 1.1.4'
end
13 changes: 13 additions & 0 deletions bin/ackr
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
$ACKR_PATH = File.expand_path(File.dirname(__FILE__)) + '/..'

require 'ackr'

if ARGV[0].nil?
puts "usage:
ackr search_string
...OR...
ackr /regex/
"
else
Ackr::Finder.new(ARGV[0].dup).run
end
22 changes: 22 additions & 0 deletions lib/ackr.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# -*- encoding: utf-8 -*-

require 'ackr/finder'
require 'ackr/colorizer'

module Ackr
# Method taken from: https://github.com/djberg96/ptools
# --
#
# Returns whether or not +file+ is a binary file. Note that this is
# not guaranteed to be 100% accurate. It performs a "best guess" based
# on a simple test of the first +File.blksize+ characters.
#
# Example:
#
# File.binary?('somefile.exe') # => true
# File.binary?('somefile.txt') # => false
#--
# Based on code originally provided by Ryan Davis (which, in turn, is
# based on Perl's -B switch).
#
def self.binary?(file)
s = (File.read(file, File.stat(file).blksize) || "").split(//)
((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
end
end
32 changes: 32 additions & 0 deletions lib/ackr/colorizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- encoding: utf-8 -*-

require 'rainbow'

module Ackr

# Methods to add terminal color commands into strings.
module Colorizer

# Public: Highlight a filename.
#
# string - The filename.
#
# Returns the String filename highlighted.
def self.for_file string
string.foreground(:blue).bright.underline
end

# Public: Highlight a matched file line.
#
# string - The line.
# search - The String search term.
#
# Returns the String line highlighted.
def self.for_line string, search
reg = Regexp.new(search, Regexp::IGNORECASE)
string.gsub(reg) do |exp|
exp.bright
end
end
end
end
41 changes: 41 additions & 0 deletions lib/ackr/finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- encoding: utf-8 -*-

module Ackr
class Finder

def initialize search_term
@search_term = search_term.downcase
end

def run
Dir.glob('**/*').each do |f|
next if File.directory?(f)
next if Ackr::binary?(f)
@file = f
search
end
end

private

def search
results = search_into_file
unless results.empty?
puts Colorizer::for_file(@file)
results.each {|r| puts r}
puts ""
end
end

def search_into_file
result = []
File.readlines(@file).each_with_index do |line, i|
if line.downcase.include?(@search_term)
result << "#{'%4i' % i}| #{Colorizer::for_line(line, @search_term)}"
end
end
result
end

end
end
35 changes: 35 additions & 0 deletions spec/colorizer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- encoding: utf-8 -*-

require './spec/helper'

describe Colorizer do

describe "#for_file" do
it "should colorize filename" do
string = "path/to/file"
expect = string.foreground(:blue).bright.underline
Colorizer::for_file(string).should == expect
end
end

describe "#for_line" do
it "should colorize matched term" do
string = "123 456 789"
expect = "123 " + "456".bright + " 789"
Colorizer::for_line(string, "456").should == expect
end

it "should colorize matched terms" do
string = "123 456 789 456"
expect = "123 " + "456".bright + " 789 " + "456".bright
Colorizer::for_line(string, "456").should == expect
end

it "should be case insensitive" do
string = "AAA BBB CCC"
expect = "AAA " + "BBB".bright + " CCC"
Colorizer::for_line(string, "bbb").should == expect
end
end
end

0 comments on commit 948f4f6

Please sign in to comment.