wasko 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wasko.rb ADDED
@@ -0,0 +1,111 @@
1
+ require "color/palette/monocontrast"
2
+ require "yaml"
3
+ # [Applescript: Small wrapper for running applescript](applescript.html)
4
+ require "wasko/applescript"
5
+ # [Terminal: Support for Terminal.app](terminal.html)
6
+ require "wasko/terminal"
7
+ # [Color: Small color utilities](color.html)
8
+ require "wasko/color"
9
+ # [Palette: Generates a color scheme](palette.html)
10
+ require "wasko/palette"
11
+ # [Configuration: Loading and saving themes](configuration.html)
12
+ require "wasko/configuration"
13
+
14
+ module Wasko
15
+ class << self
16
+
17
+ # Currently this only returns Apple's `Terminal.app`,
18
+ # in the future this could be used to support other
19
+ # Terminals as well.
20
+ def advanced_typing_apparatus
21
+ Wasko::Terminal
22
+ end
23
+
24
+ # ## Set/Get fonts and colors
25
+ #
26
+ # These all call the `advanced_typing_apparatus` and
27
+ # do exactly what they say on the tin.
28
+ #
29
+ def background_color
30
+ ::Color::RGB.from_applescript(advanced_typing_apparatus.background_color).html
31
+ end
32
+
33
+ def set_background_color(color)
34
+ advanced_typing_apparatus.set_background_color(Wasko::Color.color_from_string(color).to_applescript)
35
+ end
36
+
37
+ def foreground_color
38
+ ::Color::RGB.from_applescript(advanced_typing_apparatus.normal_text_color).html
39
+ end
40
+
41
+ def set_foreground_color(color)
42
+ advanced_typing_apparatus.set_normal_text_color(Wasko::Color.color_from_string(color).to_applescript)
43
+ end
44
+
45
+ def bold_color
46
+ ::Color::RGB.from_applescript(advanced_typing_apparatus.bold_text_color).html
47
+ end
48
+
49
+ def set_bold_color(color)
50
+ advanced_typing_apparatus.set_bold_text_color(Wasko::Color.color_from_string(color).to_applescript)
51
+ end
52
+
53
+ def cursor_color
54
+ ::Color::RGB.from_applescript(advanced_typing_apparatus.cursor_color).html
55
+ end
56
+
57
+ def set_cursor_color(color)
58
+ advanced_typing_apparatus.set_cursor_color(Wasko::Color.color_from_string(color).to_applescript)
59
+ end
60
+
61
+ def font_name
62
+ advanced_typing_apparatus.font_name
63
+ end
64
+
65
+ def set_font_name(name)
66
+ advanced_typing_apparatus.set_font_name name
67
+ end
68
+
69
+ def font_size
70
+ advanced_typing_apparatus.font_size
71
+ end
72
+
73
+ def set_font_size(size)
74
+ advanced_typing_apparatus.set_font_size size
75
+ end
76
+
77
+ def font
78
+ "#{advanced_typing_apparatus.font_name}, #{advanced_typing_apparatus.font_size}"
79
+ end
80
+
81
+ def set_font(font_size = 14, name = nil)
82
+ name = font_name if name.empty?
83
+ set_font_size font_size
84
+ set_font_name name
85
+ end
86
+
87
+ # ## Palette
88
+ #
89
+ # Returns a string representation of the current settings
90
+ def palette
91
+ [
92
+ "Background: #{background_color}",
93
+ "Foreground: #{foreground_color}",
94
+ "Bold Text : #{bold_color}",
95
+ "Cursor : #{cursor_color}",
96
+ "Font : #{font}"
97
+ ].join("\n")
98
+ end
99
+
100
+ # Try to set a sensible palette from a base color
101
+ def set_palette(color_string)
102
+ p = Wasko::Palette::TheOriginal.new(color_string)
103
+
104
+ set_background_color p.colors[:base].html
105
+ set_foreground_color p.colors[:foreground].html
106
+ set_bold_color p.colors[:bold].html
107
+ set_cursor_color p.colors[:cursor].html
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,17 @@
1
+ module Wasko
2
+ class Applescript
3
+ # Runs a piece of Applescript,
4
+ #
5
+ # Wasko::Applescript.run do
6
+ # "set ten_and_ten to 10 + 10"
7
+ # end
8
+ # => "20"
9
+ #
10
+ # Since Applescript has a nasy bit of littering its
11
+ # return values with `\n`, already escaping those.
12
+ def self.run
13
+ value = `/usr/bin/osascript -e "#{yield.gsub('"', '\"')}"`
14
+ value.gsub("\n", '')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,213 @@
1
+ require "color"
2
+
3
+ class Color::RGB
4
+
5
+ class << self
6
+ # Creates a `color` from an `Applescript` string,
7
+ # `Applescript` uses `short int` to make the RGB,
8
+ # `65535` is the maximum.
9
+ #
10
+ # Example:
11
+ #
12
+ # Color::RGB.from_applescript "65535,65535,65535"
13
+ # => <RGB [#ffffff]>
14
+ #
15
+ def from_applescript(applescript_string)
16
+ applescript_string.gsub!(/\{|\}/, "")
17
+ rgb = applescript_string.strip.split(",")
18
+ colors = rgb.map do |value|
19
+ value.to_i / 257
20
+ end
21
+ Color::RGB.new(*colors)
22
+ end
23
+ end
24
+
25
+ # Converts an instance of `Color` to an `Applescript`
26
+ # string color format.
27
+ def to_applescript
28
+ rgb = [self.red.to_i * 257, self.green.to_i * 257, self.blue.to_i * 257].join(", ")
29
+ "{#{rgb}}"
30
+ end
31
+ end
32
+
33
+ module Wasko
34
+ # This class will be used to handle (aptly named, it is)
35
+ # all things concerning Color. Internally we'll be mostly
36
+ # using the [color gem](http://rubydoc.info/gems/color/1.4.1/frames)
37
+ class Color
38
+
39
+ # Tries to get a `Color` from a string, will return `false`
40
+ # if it fails to do so, if it does recognize a color converts it
41
+ # to a format that `Applescript` will support.
42
+ #
43
+ # At the moment support all named css-colors (like `red`, `aliceblue`, etc.)
44
+ # and all hex-colors.
45
+ def self.color_from_string(color_string)
46
+ return ::Color::RGB.from_html(color_string) if hex_color?(color_string)
47
+ return ::Color::RGB.from_html(names_hash[color_string]) if named_color?(color_string)
48
+ false
49
+ end
50
+
51
+ # Looks through all the named css colors
52
+ def self.named_color?(color_string)
53
+ names_hash.keys.include?(color_string)
54
+ end
55
+
56
+ def self.hex_color?(color_string)
57
+ # Blatantly stolen from [here](http://www.geekzilla.co.uk/View05C802F8-F1DF-4097-A969-0EACB51C7834.htm)
58
+ color_string =~ /^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/
59
+ end
60
+
61
+ def self.names_hash
62
+ {
63
+ "aliceblue" => "#f0f8ff",
64
+ "antiquewhite" => "#faebd7",
65
+ "aqua" => "#00ffff",
66
+ "aquamarine" => "#7fffd4",
67
+ "azure" => "#f0ffff",
68
+ "beige" => "#f5f5dc",
69
+ "bisque" => "#ffe4c4",
70
+ "black" => "#000000",
71
+ "blanchedalmond" => "#ffebcd",
72
+ "blue" => "#0000ff",
73
+ "blueviolet" => "#8a2be2",
74
+ "brown" => "#a52a2a",
75
+ "burlywood" => "#deb887",
76
+ "cadetblue" => "#5f9ea0",
77
+ "chartreuse" => "#7fff00",
78
+ "chocolate" => "#d2691e",
79
+ "coral" => "#ff7f50",
80
+ "cornflowerblue" => "#6495ed",
81
+ "cornsilk" => "#fff8dc",
82
+ "crimson" => "#dc143c",
83
+ "cyan" => "#00ffff",
84
+ "darkblue" => "#00008b",
85
+ "darkcyan" => "#008b8b",
86
+ "darkgoldenrod" => "#b8860b",
87
+ "darkgray" => "#a9a9a9",
88
+ "darkgrey" => "#a9a9a9",
89
+ "darkgreen" => "#006400",
90
+ "darkkhaki" => "#bdb76b",
91
+ "darkmagenta" => "#8b008b",
92
+ "darkolivegreen" => "#556b2f",
93
+ "darkorange" => "#ff8c00",
94
+ "darkorchid" => "#9932cc",
95
+ "darkred" => "#8b0000",
96
+ "darksalmon" => "#e9967a",
97
+ "darkseagreen" => "#8fbc8f",
98
+ "darkslateblue" => "#483d8b",
99
+ "darkslategray" => "#2f4f4f",
100
+ "darkslategrey" => "#2f4f4f",
101
+ "darkturquoise" => "#00ced1",
102
+ "darkviolet" => "#9400d3",
103
+ "deeppink" => "#ff1493",
104
+ "deepskyblue" => "#00bfff",
105
+ "dimgray" => "#696969",
106
+ "dimgrey" => "#696969",
107
+ "dodgerblue" => "#1e90ff",
108
+ "firebrick" => "#b22222",
109
+ "floralwhite" => "#fffaf0",
110
+ "forestgreen" => "#228b22",
111
+ "fuchsia" => "#ff00ff",
112
+ "gainsboro" => "#dcdcdc",
113
+ "ghostwhite" => "#f8f8ff",
114
+ "gold" => "#ffd700",
115
+ "goldenrod" => "#daa520",
116
+ "gray" => "#808080",
117
+ "grey" => "#808080",
118
+ "green" => "#008000",
119
+ "greenyellow" => "#adff2f",
120
+ "honeydew" => "#f0fff0",
121
+ "hotpink" => "#ff69b4",
122
+ "indianred" => "#cd5c5c",
123
+ "indigo" => "#4b0082",
124
+ "ivory" => "#fffff0",
125
+ "khaki" => "#f0e68c",
126
+ "lavender" => "#e6e6fa",
127
+ "lavenderblush" => "#fff0f5",
128
+ "lawngreen" => "#7cfc00",
129
+ "lemonchiffon" => "#fffacd",
130
+ "lightblue" => "#add8e6",
131
+ "lightcoral" => "#f08080",
132
+ "lightcyan" => "#e0ffff",
133
+ "lightgoldenrodyellow" => "#fafad2",
134
+ "lightgray" => "#d3d3d3",
135
+ "lightgrey" => "#d3d3d3",
136
+ "lightgreen" => "#90ee90",
137
+ "lightpink" => "#ffb6c1",
138
+ "lightsalmon" => "#ffa07a",
139
+ "lightseagreen" => "#20b2aa",
140
+ "lightskyblue" => "#87cefa",
141
+ "lightslategray" => "#778899",
142
+ "lightslategrey" => "#778899",
143
+ "lightsteelblue" => "#b0c4de",
144
+ "lightyellow" => "#ffffe0",
145
+ "lime" => "#00ff00",
146
+ "limegreen" => "#32cd32",
147
+ "linen" => "#faf0e6",
148
+ "magenta" => "#ff00ff",
149
+ "maroon" => "#800000",
150
+ "mediumaquamarine" => "#66cdaa",
151
+ "mediumblue" => "#0000cd",
152
+ "mediumorchid" => "#ba55d3",
153
+ "mediumpurple" => "#9370d8",
154
+ "mediumseagreen" => "#3cb371",
155
+ "mediumslateblue" => "#7b68ee",
156
+ "mediumspringgreen" => "#00fa9a",
157
+ "mediumturquoise" => "#48d1cc",
158
+ "mediumvioletred" => "#c71585",
159
+ "midnightblue" => "#191970",
160
+ "mintcream" => "#f5fffa",
161
+ "mistyrose" => "#ffe4e1",
162
+ "moccasin" => "#ffe4b5",
163
+ "navajowhite" => "#ffdead",
164
+ "navy" => "#000080",
165
+ "oldlace" => "#fdf5e6",
166
+ "olive" => "#808000",
167
+ "olivedrab" => "#6b8e23",
168
+ "orange" => "#ffa500",
169
+ "orangered" => "#ff4500",
170
+ "orchid" => "#da70d6",
171
+ "palegoldenrod" => "#eee8aa",
172
+ "palegreen" => "#98fb98",
173
+ "paleturquoise" => "#afeeee",
174
+ "palevioletred" => "#d87093",
175
+ "papayawhip" => "#ffefd5",
176
+ "peachpuff" => "#ffdab9",
177
+ "peru" => "#cd853f",
178
+ "pink" => "#ffc0cb",
179
+ "plum" => "#dda0dd",
180
+ "powderblue" => "#b0e0e6",
181
+ "purple" => "#800080",
182
+ "red" => "#ff0000",
183
+ "rosybrown" => "#bc8f8f",
184
+ "royalblue" => "#4169e1",
185
+ "saddlebrown" => "#8b4513",
186
+ "salmon" => "#fa8072",
187
+ "sandybrown" => "#f4a460",
188
+ "seagreen" => "#2e8b57",
189
+ "seashell" => "#fff5ee",
190
+ "sienna" => "#a0522d",
191
+ "silver" => "#c0c0c0",
192
+ "skyblue" => "#87ceeb",
193
+ "slateblue" => "#6a5acd",
194
+ "slategray" => "#708090",
195
+ "slategrey" => "#708090",
196
+ "snow" => "#fffafa",
197
+ "springgreen" => "#00ff7f",
198
+ "steelblue" => "#4682b4",
199
+ "tan" => "#d2b48c",
200
+ "teal" => "#008080",
201
+ "thistle" => "#d8bfd8",
202
+ "tomato" => "#ff6347",
203
+ "turquoise" => "#40e0d0",
204
+ "violet" => "#ee82ee",
205
+ "wheat" => "#f5deb3",
206
+ "white" => "#ffffff",
207
+ "whitesmoke" => "#f5f5f5",
208
+ "yellow" => "#ffff00",
209
+ "yellowgreen" => "#9acd32"
210
+ }
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,87 @@
1
+ require "fileutils"
2
+
3
+ module Wasko
4
+ # This class will handle all things considering
5
+ # loading and saving configuration. It should work
6
+ # like this.
7
+ #
8
+ # * User sets all things to his liking
9
+ # * User writes to a `.color` file in `~/.wasko`
10
+ # * User can share/edit this `.color` file
11
+ # * User can load `.color` file to restore settings
12
+ #
13
+ class Configuration
14
+ class << self
15
+
16
+ # All config files are stored in `~/.wasko/`
17
+ def wasko_directory
18
+ wasko_path = File.join(ENV['HOME'], ".wasko")
19
+ unless File.exists?(wasko_path) && File.directory?(wasko_path)
20
+ FileUtils.mkdir_p File.join(ENV['HOME'], ".wasko")
21
+ end
22
+ wasko_path
23
+ end
24
+
25
+ # All possible `.color` themes
26
+ def all_themes
27
+ Dir.chdir(wasko_directory) do |path|
28
+ Dir["*.color"].map do |filename|
29
+ filename.gsub(/\.color$/, "")
30
+ end
31
+ end
32
+ end
33
+
34
+ # Blatantly stolen from [here](http://stackoverflow.com/questions/1032104/regex-for-finding-valid-filename)
35
+ # Spots all obvious bad filenames
36
+ def valid_name?(name)
37
+ name =~ /^[^\/?*:;{}\\]+$/
38
+ end
39
+
40
+ # File path of the color theme file
41
+ def config_file_with_name(name)
42
+ File.join(wasko_directory, "#{name}.color")
43
+ end
44
+
45
+ # Setup the color theme with the hash.
46
+ def color_theme_from_hash(name)
47
+ return {} unless all_themes.include?(name)
48
+ return {} unless Hash === YAML::load(File.open(config_file_with_name(name)))
49
+ YAML::load(File.open(config_file_with_name(name)))
50
+ end
51
+
52
+ # Transform a color theme to a hash
53
+ def color_theme_to_hash
54
+ %w(background_color foreground_color bold_color cursor_color font_size font_name).inject({}) do |hash, value|
55
+ hash[value] = Wasko.send(value)
56
+ hash
57
+ end
58
+ end
59
+
60
+ # ## Loading and saving colors
61
+
62
+ # Draw the saved colors
63
+ def load_colors!(name)
64
+ color_theme_from_hash(name).each do |object, color|
65
+ Wasko.send("set_#{object}", color)
66
+ end
67
+ end
68
+
69
+ # Write out the colors
70
+ def save_colors!(name)
71
+ File.open(config_file_with_name(name), 'w') do |out|
72
+ out.write(configuration_help)
73
+ out.write(color_theme_to_hash.to_yaml)
74
+ end
75
+ end
76
+
77
+ def configuration_help
78
+ <<HELP
79
+ # This is a theme used by the wasko gem, it's nothing
80
+ # more than regular yaml. So (ab)use as you would normally
81
+ # do. The only thing to note is, colors take only valid
82
+ # css colors and this comment can be deleted.
83
+ HELP
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,90 @@
1
+ module Wasko
2
+ # A module aimed at creating color schemes for terminals
3
+ # Pretty straightforward, no?
4
+ module Palette
5
+ # Since I change my mind pretty frequently on
6
+ # color schemes etc. Putting the actual logic in
7
+ # a class so it's easier to extend.
8
+ class TheOriginal
9
+ # Takes a valid css color string and an optional
10
+ # contrast argument. The contrast is useful to tweak
11
+ # the output of things.
12
+ #
13
+ # It sets a base color `@base` and creates a color
14
+ # palette derived from the base color using the
15
+ # `colors!` method.
16
+ def initialize(color_string, contrast = 0)
17
+ @colors = {}
18
+ @base = Wasko::Color.color_from_string(color_string)
19
+ @base = white unless @base
20
+ @contrast = 30 + contrast
21
+ colors!
22
+ end
23
+
24
+ # Returns a `color`-instance of white
25
+ def white
26
+ Wasko::Color.color_from_string("white")
27
+ end
28
+
29
+ # Returns a `color`-instance of black
30
+ def black
31
+ Wasko::Color.color_from_string("black")
32
+ end
33
+
34
+ # Checks the brightness of the base color and
35
+ # returns the appropriate opposite color.
36
+ #
37
+ # For example black will return white, white will
38
+ # return black.
39
+ def opposite_color
40
+ @base.brightness > 0.5 ? black : white
41
+ end
42
+
43
+ # To calculate colors that will fit our base color
44
+ # we need to find out to brighten them, or darken
45
+ # them
46
+ def inverse_brightness
47
+ @base.brightness > 0.5 ? -1 : 1
48
+ end
49
+
50
+ # Hash of the color palette
51
+ # TODO: attr_accessible
52
+ def colors
53
+ @colors
54
+ end
55
+
56
+ # Creates a palette based on the `@base`-color. This
57
+ # generates a color palette which has taken a good
58
+ # look at [Solarized](http://ethanschoonover.com/solarized)
59
+ # The plus side is you can use any base color, the
60
+ # downside is, the colors won't be picked as well as
61
+ # when using [Solarized](http://ethanschoonover.com/solarized) so if that's what you need, check it out.
62
+ def colors!
63
+ p = {}
64
+ p[:base] = @base
65
+ p[:foreground] = @base.mix_with(opposite_color, @contrast + 18)
66
+ p[:bold] = @base.mix_with(opposite_color, @contrast + 19.5)
67
+ p[:selection] = @base.adjust_brightness inverse_brightness * @contrast
68
+ p[:selected] = p[:bold]
69
+ p[:cursor] = p[:foreground]
70
+
71
+ # ANSI Colors
72
+ p[:red] = mix_base_with("red", 50, inverse_brightness * @contrast)
73
+ p[:green] = mix_base_with("green", 50, inverse_brightness * @contrast)
74
+ p[:yellow] = mix_base_with("yellow", 50, inverse_brightness * @contrast)
75
+ p[:white] = mix_base_with("white", 35, inverse_brightness * @contrast)
76
+ p[:black] = mix_base_with("black", 35, inverse_brightness * @contrast)
77
+ p[:blue] = mix_base_with("blue", 50, inverse_brightness * (@contrast))
78
+ p[:magenta] = mix_base_with("#CA1F7B", 35, inverse_brightness * @contrast)
79
+ p[:cyan] = mix_base_with("#00FFFF", 50, inverse_brightness * (@contrast - 20))
80
+ @colors = p
81
+ end
82
+
83
+ # Just a utility method that mixes colors, for more
84
+ # info on this check the docs of the `color`-gem.
85
+ def mix_base_with(color_name, mix_value = 50, brightness = 30)
86
+ @base.mix_with(Wasko::Color.color_from_string(color_name), mix_value).adjust_brightness(brightness)
87
+ end
88
+ end
89
+ end
90
+ end