-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The search is case insensitive. The terminal display is colorized thanks to the rainbow gem.
- Loading branch information
Showing
9 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|