-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperform.rb
188 lines (164 loc) · 5.02 KB
/
perform.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require 'pry'
STDOUT.sync = true
def show_abbreviations
`gedit language.json`
end
def escape_double_quotes(string)
string.gsub('"', '\\"')
end
def error(message)
`notify-send --hint=int:transient:1 --hint=string:sound-name:bell "#{escape_double_quotes(message)}"`
end
def capitalize(sentence)
sentence.split(' ').map(&:capitalize).join(' ')
end
APPLICATIONS = [
{
name: 'Chrome',
desktop_file_name: 'google-chrome',
},
{
name: 'TeamViewer',
desktop_file_name: 'com.teamviewer.TeamViewer',
},
{
name: 'Emacs',
desktop_file_name: 'emacs',
},
{
name: 'Telegram',
desktop_file_name: 'telegramdesktop',
},
{
name: 'Nylas Mail',
desktop_file_name: 'nylas-mail',
},
{
name: 'Slack',
desktop_file_name: 'slack',
},
{
name: 'Tilix',
desktop_file_name: 'com.gexperts.Tilix',
},
{
name: 'RubyMine',
desktop_file_name: 'rubymine',
},
].map { |application| OpenStruct.new(application) }
class Performer
def initialize
@desktop_environment = GnomeShell.new
end
def perform(command)
puts command
@desktop_environment.show_notification(capitalize(command))
perform_internal(command)
end
private
def perform_internal(command)
case command
when /\Aclose current window\z/i
@desktop_environment.close_current_window
when /\Ashow abbreviations\z/i
show_abbreviations
when /\Aopen (.+)\z/i
openable = $1
application = APPLICATIONS.find { |application| application.name == openable }
if application
@desktop_environment.open_application(application)
elsif openable.start_with?('~/dev')
@desktop_environment.open_project(openable)
elsif openable.start_with?('http')
perform_internal('open Chrome')
sleep 0.3
perform_internal('press ctrl+t')
sleep 0.3
perform_internal("type #{openable}")
perform_internal('press Return')
elsif openable.end_with?(' model')
perform_internal('press ctrl+n')
sleep 0.5
perform_internal("type #{openable.split(' ').first}")
sleep 0.5
perform_internal('press Return')
end
when /\Atype (.+)\z/i
@desktop_environment.type($1)
when /\Apress (.+)\z/i
@desktop_environment.press($1)
when /\Aexecute (.+)\z/i
@desktop_environment.execute($1)
when /\Aslack (.+)\z/i
perform_internal('open Slack')
perform_internal('press ctrl+2')
perform_internal('press ctrl+k')
perform_internal("type #{$1}")
perform_internal('press Return')
when /\Atelegram (.+)\z/i
perform_internal('open Telegram')
perform_internal('press Escape')
perform_internal("type #{$1}")
perform_internal('press Return')
when /\A([a-z]+) is not defined\z/i
error %Q(Shortcut '#{$1}' is not defined. Ignoring...)
else
error %Q(Don't know how to handle '#{command}'. Ignoring...)
end
end
end
class GnomeShell
def initialize
prepared_shell_code = escape_double_quotes(File.read('osd.js')).gsub("\n", "\\\n")
shell_eval(prepared_shell_code)
end
def show_notification(message)
prepared_command = escape_double_quotes(message).gsub("\n", "\\\n")
shell_eval("new OsdWindow('#{prepared_command}').show();")
end
def open_application(application)
if window_with_class_is_open(application)
shell_eval("Main.activateWindow(Shell.AppSystem.get_default().lookup_desktop_wmclass('#{application.desktop_file_name}').get_windows()[0])")
else
shell_eval("Shell.AppSystem.get_default().lookup_desktop_wmclass('#{application.desktop_file_name}').activate()")
end
end
def open_project(title)
result = shell_eval("global.screen.get_workspace_by_index(0).list_windows().find(w => w.title.contains('[#{title}]'))")
if result == ''
`nohup rubymine #{title} &`
else
shell_eval("Main.activateWindow(global.screen.get_workspace_by_index(0).list_windows().find(w => w.title.contains('[#{title}]')))")
end
end
def close_current_window
shell_eval("global.display.focus_window.delete(0)")
end
def type(string)
old_clipboard_content = `xsel --clipboard --output`
`echo "#{string}" | xsel --clipboard --input` # copy to clipboard.
press('ctrl+v')
sleep 0.1
`echo "#{old_clipboard_content}" | xsel --clipboard --input` # copy to clipboard.
end
def press(key)
`xdotool key #{key}`
end
def execute(command)
`#{command}`
end
private
def shell_eval(js_code)
raw_result = `gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval "#{js_code}"`
_was_successful, quoted_result = raw_result.strip[1..-2].split(',').map(&:strip)
quoted_result[1..-2]
end
def window_with_class_is_open(application)
result = shell_eval("Shell.AppSystem.get_default().lookup_desktop_wmclass('#{application.desktop_file_name}').get_windows().length")
result != '0'
end
end
if __FILE__ == $PROGRAM_NAME
performer = Performer.new
performer.perform($stdin.gets.chomp) while true
end