Class: SleipnirAPI::CLI::Grepnir

Inherits:
Base
  • Object
show all
Defined in:
lib/sleipnir_api/cli/grepnir.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Action

Constant Summary collapse

DEFAULT_ARGUMENTS =
%W(-A -T -S)

Instance Method Summary collapse

Methods inherited from Base

#done!, #execute, #execute0, #prepare, #stderr, #stdin, #stdout

Constructor Details

#initialize(*args) ⇒ Grepnir

Returns a new instance of Grepnir.



75
76
77
78
# File 'lib/sleipnir_api/cli/grepnir.rb', line 75

def initialize(*args)
  super(*args)
  @action = Action.new(self)
end

Instance Method Details

#argument_processor(ctx) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sleipnir_api/cli/grepnir.rb', line 94

def argument_processor(ctx)
  Proc.new do |opt|
    opt.banner = "Usage: grepnir [OPTION]... PATTERN..."

    opt.separator ""
    opt.separator "  Sleipnir control:"
    opt.on("-A", "--[no-]activate", "activate 1st matched tab") {|v|
      ctx.action.activate = v }
    opt.on("-T", "--[no-]title-star", "add star '*' to title") {|v|
      ctx.action.title_star = v }
    opt.on("-L", "--[no-]lock", "navigate lock for each matched tab") {|v|
      ctx.action.lock = v }
    opt.on("-H", "--[no-]hilight", "hilight to distinguish the matched text") {|v|
      ctx.action.hilight = v }
    opt.on("-S", "--[no-]search", "search and select matched text") {|v|
      ctx.action.search = v }

    opt.separator ""
    opt.separator "  Match control:"
    opt.on("-v", "--invert-match", "select non-matching tabs") {
      ctx.invert_match = true }
    opt.on("-l", "--only-locked-tab", "search navigate lock tabs only") {|v|
      ctx.only_locked_tab = true }
    opt.on("--include=PATTERN", "URLs that match PATTERN will be examined") {|v|
      ctx.includes << v }
    opt.on("--exclude=PATTERN", "URLs that match PATTERN will be skipped") {|v|
      ctx.excludes << v }

    opt.separator ""
    opt.separator "  Miscellaneous:"
    opt.on("-V", "--version", "print version information and exit") {
      stdout.puts "grepnir #{SleipnirAPI::VERSION::STRING}"
      done!(0)
    }
    opt.on("-h", "--help", "print this help and exit") {
      stdout.puts opt
      done!(0)
    }

    opt.separator ""
    opt.separator "  Default options:"
    opt.separator "    #{DEFAULT_ARGUMENTS * ' '}"

    opt.parse!
  end
end

#initialize_context(ctx) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sleipnir_api/cli/grepnir.rb', line 80

def initialize_context(ctx)
  ctx.action = OpenStruct.new
  ctx.flag = OpenStruct.new
  ctx.scope = OpenStruct.new.instance_eval {
    self.app = {}
    self.req = {}
    self.tab = {}
    self
  }
  ctx.action.output = true
  ctx.includes = []
  ctx.excludes = []
end

#perform(ctx, pnir) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/sleipnir_api/cli/grepnir.rb', line 170

def perform(ctx, pnir)
  r = false
  ctx.scope.req.clear
  pnir.each do |tab|
    next unless target_tab?(ctx, tab)
    ctx.scope.tab.clear
    tab.document {|doc|
      text = doc.body.innerText rescue next
      matched = ctx.keyword_re.all?{|re| re.match(text) }
      matched = !matched if ctx.invert_match
      @action.run(ctx, pnir, tab, doc, text, matched)
      r = true if matched
    }
  end
  r
end

#process_arguments(ctx, args) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sleipnir_api/cli/grepnir.rb', line 150

def process_arguments(ctx, args)
  proc = argument_processor(ctx)

  defaults = DEFAULT_ARGUMENTS.dup.extend(OptionParser::Arguable)
  defaults.options(&proc)

  args.options(&proc)
  if args.empty?
    stderr.puts "missing PATTERN"
    stderr.puts args.options
    done!(2)
  end

  ctx.keywords = args.dup
  ctx.keyword_re = ctx.keywords.map{|e| /#{Regexp.quote(e)}/i }

  ctx.include_re = Regexp.union(*ctx.includes)
  ctx.exclude_re = Regexp.union(*ctx.excludes)
end

#target_tab?(ctx, tab) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
# File 'lib/sleipnir_api/cli/grepnir.rb', line 141

def target_tab?(ctx, tab)
  return false if ctx.only_locked_tab and not tab.navigate_lock?
  url = tab.document.URL
  return false if ctx.exclude_re.match(url)
  return true if ctx.includes.empty?
  return true if ctx.include_re.match(url)
  false
end