Class: Wasko::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/wasko/terminal.rb

Overview

This class will be used to strip the Applescript even further away, encapsulating all the needed methods to set colors and fonts of ‘Terminal.app`.

Added bonus is, this paves the way to add support for [iTerm2](code.google.com/p/iterm2/) and other variants.

Class Method Summary collapse

Class Method Details

.add_theme_script(theme_name) ⇒ Object

This string will add a new theme to the Terminal’s preferences if evaluated by Applescript



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/wasko/terminal.rb', line 158

def self.add_theme_script(theme_name)
  <<SCRIPT
tell application "Terminal"
	activate
	tell application "System Events"
		# Open Preferences
		keystroke "," using command down
		tell process "Terminal"
			# Create the wasko theme placeholder
			click button 2 of tool bar 1 of window 1
			click (every button whose description is "Add") of group 1 of window 1
			keystroke "#{theme_name}"
			key code 36 # hits return
		end tell
	end tell
end tell
SCRIPT
end

.get_selected_themeObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wasko/terminal.rb', line 87

def self.get_selected_theme
  @selected_theme ||=
    Wasko::Applescript.run do
    <<SCRIPT
tell application "Terminal"
  activate
  tell application "System Events"
# Open Inspector
tell process "Terminal"
  if window "Inspector" exists then
  else
    keystroke "I" using command down
  end if
  click radio button 2 of tab group 1 of window "Inspector"
  set selected_theme to value of text field of item 1 of (every row whose selected is true) of table 1 of scroll area 1 of tab group 1 of window "Inspector"
  click button 1 of window "Inspector"
  return selected_theme
end tell

  end tell
end tell
SCRIPT
  end
end

.method_missing(method_sym, *arguments, &block) ⇒ Object

# Getters And Setters

This supports the following

* `set_background_color "fff"`
* `background_color`
* `set_normal_text_color "fff"`
* `normal_text_color`
* `set_font_size 12`
* `font_size`
* `set_font_name "DejaVu Sans Mono"`
* `font_name`


190
191
192
193
194
195
196
197
198
# File 'lib/wasko/terminal.rb', line 190

def self.method_missing(method_sym, *arguments, &block)
  if method_sym.to_s =~ /^set_(.*)$/
    self.set($1.gsub(/_/, " ") => arguments.first)
  elsif method_sym.to_s =~ /^([a-z]+_[a-z]+(.*))$/
    self.get($1.gsub(/_/, " "))
  else
    super
  end
end

.respond_to?(method_sym, include_private = false) ⇒ Boolean

Pretty big todo, shield this off somewhat.

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
209
# File 'lib/wasko/terminal.rb', line 201

def self.respond_to?(method_sym, include_private = false)
  if method_sym.to_s =~ /^set_(.*)$/
    true
  elsif method_sym.to_s =~ /^[a-z]+_[a-z]+(.*)$/
    true
  else
    super
  end
end

.set_ansi_black_color(color) ⇒ Object



19
# File 'lib/wasko/terminal.rb', line 19

def self.set_ansi_black_color(color);end

.set_ansi_blue_color(color) ⇒ Object



23
# File 'lib/wasko/terminal.rb', line 23

def self.set_ansi_blue_color(color);end

.set_ansi_cyan_color(color) ⇒ Object



25
# File 'lib/wasko/terminal.rb', line 25

def self.set_ansi_cyan_color(color);end

.set_ansi_green_color(color) ⇒ Object



21
# File 'lib/wasko/terminal.rb', line 21

def self.set_ansi_green_color(color);end

.set_ansi_magenta_color(color) ⇒ Object



24
# File 'lib/wasko/terminal.rb', line 24

def self.set_ansi_magenta_color(color);end

.set_ansi_red_color(color) ⇒ Object



20
# File 'lib/wasko/terminal.rb', line 20

def self.set_ansi_red_color(color);end

.set_ansi_white_color(color) ⇒ Object



26
# File 'lib/wasko/terminal.rb', line 26

def self.set_ansi_white_color(color);end

.set_ansi_yellow_color(color) ⇒ Object



22
# File 'lib/wasko/terminal.rb', line 22

def self.set_ansi_yellow_color(color);end

.set_color_via_gui(color_index, color) ⇒ Object



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
# File 'lib/wasko/terminal.rb', line 54

def self.set_color_via_gui(color_index, color)
  Wasko::Applescript.run do
    <<SCRIPT
tell application "Terminal"
  activate
  tell application "System Events"
# Open Preferences
keystroke "," using command down
tell process "Terminal"
  click button 2 of tool bar 1 of window 1
  # Make sure the default is selected
  set srows to every row of table 1 of scroll area 1 of group 1 of window 1
  repeat with a_row in srows
    if value of text field 1 of a_row contains "#{get_selected_theme}" then
      set selected of a_row to true
      exit repeat
    end if
  end repeat

  click color well #{color_index} of tab group 1 of group 1 of window "Settings"
  click (every button whose description is "Hex Color Picker") of tool bar 1 of window "Colors"
  set value of text field 1 of group 1 of window "Colors" to "#{color.html}"
  # Close Colors
  click button 1 of window "Colors"
  # Close prefs
  keystroke "w" using command down
end tell
  end tell
end tell
SCRIPT
  end
end

.set_selected_text_color(color) ⇒ Object

If you don’t like that option I’ve sent a pull request to iTerm2 which has ansi applescript support



17
# File 'lib/wasko/terminal.rb', line 17

def self.set_selected_text_color(color);end

.set_selected_theme(theme_name) ⇒ Object



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
# File 'lib/wasko/terminal.rb', line 112

def self.set_selected_theme(theme_name)
  output = Wasko::Applescript.run do
    <<SCRIPT
tell application "Terminal"
  activate
  tell application "System Events"
# Open Inspector
tell process "Terminal"
  if window "Inspector" exists then
  else
    keystroke "I" using command down
  end if
  click radio button 2 of tab group 1 of window "Inspector"
 set srows to every row of table 1 of scroll area 1 of tab group 1 of window "Inspector"
  set theme_found to false
			repeat with a_row in srows
    if value of text field of a_row contains "#{theme_name}" then
      set theme_found to true
      set selected of a_row to true
    end if
  end repeat

  if theme_found then
    # Theme selected
    # Close inspector window
    keystroke "w" using command down
  else
    keystroke "w" using command down
    #{add_theme_script(theme_name)}
    # Cleanup
    keystroke "w" using command down
    return "NOT_FOUND"
  end if
end tell
  end tell
end tell
SCRIPT
  end

  # Doing this in Ruby because Applescript is that much
  # teh suck combined.
  set_selected_theme(theme_name) if output == "NOT_FOUND"
end

.set_selection_color(color) ⇒ Object



18
# File 'lib/wasko/terminal.rb', line 18

def self.set_selection_color(color);end

.startup_background_colorObject

The basic background color, since wasko never changes it, it can always be used as the baseline for creating new color mixes.

Returns an applescript color



43
44
45
46
47
48
49
50
51
# File 'lib/wasko/terminal.rb', line 43

def self.startup_background_color
  Wasko::Applescript.run do
  <<SCRIPT
tell application "Terminal"
	get background color of startup settings
end tell
SCRIPT
  end
end