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):\/\/.*/)

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.



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

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.



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

def app
  @app
end

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#modesObject (readonly)

Returns the value of attribute modes.



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

def modes
  @modes
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

#viewsObject (readonly)

Returns the value of attribute views.



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

def views
  @views
end

Instance Method Details

#clear_subtle_varsObject



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

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

#encode_with(coder) ⇒ Object



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

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

#executeObject



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

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/command.rb', line 106

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



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

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

#subtle_executeObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/command.rb', line 145

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

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



97
98
99
100
101
102
103
104
# File 'lib/command.rb', line 97

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