#!/usr/bin/env ruby #------------------------------------------ def print_usage print < #{$0} -h where: igrep stands for "indented grep" -i gives the number of spaces to indent the text or pattern to search for the file to search in USAGE exit end print_usage if ARGV[0] == nil #------------------------------------------ require 'getoptlong' @indent = "" opt = GetoptLong.new( ['--indent', '-i', GetoptLong::REQUIRED_ARGUMENT], ['--help', '-h', GetoptLong::NO_ARGUMENT] ) opt.each_option do |name,arg| case name when '--indent' indentSize = arg.to_i @indent = " " * indentSize # Repeating string! when '--help' print_usage exit end end @from = ARGV[0] @file = ARGV[1] #------------------------------------------ File.readlines(@file).each do |line| if line.include? @from then start = line.index @from if (start > 20) then newStart = start - 17 line[0..newStart] = "..." end if (line.length > 60) then line = line[0..59]+"..." end printf "%s%s", @indent, line end end puts