Class: Command

Inherits:
Object
  • Object
show all
Includes:
Item
Defined in:
lib/command.rb

Constant Summary collapse

RE_COMMAND =
Regexp.new(/^[+\^\*]*.+(\s[+\^\*])*(\s[@#][A-Za-z0-9_-]+)*$/)
RE_MODES =
Regexp.new(/^[+\^\*=]+$/)
RE_METHOD =
Regexp.new(/^:\s*(.*)/)
RE_BROWSER =
Regexp.new(/(chrom[e|ium]|iron|navigator|firefox|opera)/i)
RE_SEARCH =
Regexp.new(/^[gs]\s+(.*)/)
RE_URI =
Regexp.new(/^((w\s+((https?|ftp):\/\/)?)|(https?|ftp):\/\/)(?:[A-Z0-9-]+.)+[A-Z]{2,6}([\/?].+)?$/i)
RE_PROTO =
Regexp.new(/^(http|https):\/\/.*/)
MODES =
{ '+' => :full, '^' => :float, '*' => :stick, '=' => :zaphod }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Item

#name, #name=

Constructor Details

#initialize(name, command = nil) ⇒ Command

Returns a new instance of Command.



19
20
21
22
23
# File 'lib/command.rb', line 19

def initialize name, command = nil
  @name = name
  clear_subtle_vars
  self.command = command
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



17
18
19
# File 'lib/command.rb', line 17

def app
  @app
end

#commandObject

Returns the value of attribute command.



17
18
19
# File 'lib/command.rb', line 17

def command
  @command
end

#modesObject (readonly)

Returns the value of attribute modes.



17
18
19
# File 'lib/command.rb', line 17

def modes
  @modes
end

#tagsObject (readonly)

Returns the value of attribute tags.



17
18
19
# File 'lib/command.rb', line 17

def tags
  @tags
end

#viewsObject (readonly)

Returns the value of attribute views.



17
18
19
# File 'lib/command.rb', line 17

def views
  @views
end

Instance Method Details

#add_mode(arg) ⇒ Object



110
111
112
113
114
115
# File 'lib/command.rb', line 110

def add_mode arg
  modes = RE_MODES.match(arg).to_s.split('')
  @modes += modes.split.map do |mode|
    MODES[mode]
  end
end

#add_tag(arg) ⇒ Object Also known as: add_view



104
105
106
# File 'lib/command.rb', line 104

def add_tag arg
  @tags << arg[1..-1]      
end

#clear_subtle_varsObject



25
26
27
28
29
30
# File 'lib/command.rb', line 25

def clear_subtle_vars
  @tags = []
  @views = []
  @app = ""
  @modes = []
end

#encode_with(coder) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/command.rb', line 32

def encode_with coder
  coder['name'] = @name
  coder['command'] = @command
  coder['tags'] = @tags
  coder['views'] = @views
  coder['app'] = @app
  coder['modes'] = @modes
end

#executeObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/command.rb', line 142

def execute
  case @command
  when String
    if $subtle
      subtle_execute
    else
      command = "#{@command} &>/dev/null"
      puts command if $debug
      system command 
    end
  when URI
    command = "xdg-open '#{@command.to_s}' &>/dev/null"
    puts command if $debug
    system command
    if $subtle
      browser = find_browser
      browser.focus unless browser.nil?
    end 
  end
  true
end

#find_browserObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/command.rb', line 125

def find_browser
  begin
    if @browser.nil?
      Subtlext::Client.all.each do |c|
        if c.klass.match(RE_BROWSER)
          @browser = c
          @view = c.views.first
          return
        end
      end
    end
  rescue
    @browser = nil
    @view = nil
  end
end

#init_with(coder) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/command.rb', line 41

def init_with coder
  @name = coder['name']
  @command = coder['command']
  @tags = coder['tags']
  @views = coder['views']
  @app = coder['app']
  @modes = coder['modes']
end

#parse_subtle_args(args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/command.rb', line 89

def parse_subtle_args args
  args.each do |arg|
    case arg[0]
    when '#' then add_tag arg
    when '@' then add_view arg
    when '+', '^', '*', '=' then add_mode arg
    else
      set_app arg
    end
  end
  if @views.any? and not @app.empty? and @tags.empty?
    @tags << "tag_#{rand(1337)}"
  end
end

#set_app(arg) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/command.rb', line 117

def set_app arg
  if @app.nil? || @app.empty?
    @app = arg
  else
    @app += ' ' + arg
  end
end

#subtle_command(command) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/command.rb', line 78

def subtle_command command
  if RE_METHOD.match(command)
    command = Regexp.last_match(0).to_sym      
  elsif RE_COMMAND.match command
    clear_subtle_vars
    parse_subtle_args command.split
  else
    @app = command
  end
end

#subtle_executeObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/command.rb', line 164

def subtle_execute
  if $debug
    puts "App: #{@app}"
    puts "Tags: #{@tags.to_s}"
    puts "Views: #{@views.to_s}"
    puts "Modes: #{@modes.to_s}"
  end
  tags = @tags.map do |t|
    tag = Subtlext::Tag.first(t) || Subtlext::Tag.new(t)
    tag.save
    tag
  end
  @views.each do |v|
    view = Subtlext::View.first(v) || Subtlext::View.new(v)
    view.save
    view.tag(tags) unless view.nil? or tags.empty?
  end
  unless (client = Subtlext::Client.spawn(@app)).nil?
    client.tags = tags unless tags.empty?
    client.flags = @modes unless @modes.empty?
  end
  true
end

#uri_command(uri) ⇒ Object



63
64
65
66
67
# File 'lib/command.rb', line 63

def uri_command uri
  command = uri.split(/\s+/)[-1]
  command.prepend("http://") unless RE_PROTO.match command
  command = URI.parse command
end

#web_search(search_string, engine = :google) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/command.rb', line 69

def web_search search_string, engine = :google
  escaped_string = URI.escape search_string
  case engine
  when :duckduckgo then escaped_string.prepend "https://duckduckgo.com/?q="
  else escaped_string.prepend "https://www.google.com/#q="
  end
  URI.parse escaped_string
end