wasko 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ module Wasko
2
+ # This class will be used to strip the Applescript
3
+ # even further away, encapsulating all the needed
4
+ # methods to set colors and fonts of `Terminal.app`.
5
+ #
6
+ # Added bonus is, this paves the way to add support
7
+ # for [iTerm2](http://code.google.com/p/iterm2/) and
8
+ # other variants.
9
+ class Terminal
10
+
11
+
12
+ # # Getters And Setters
13
+ #
14
+ # This supports the following
15
+ #
16
+ # * `set_background_color "fff"`
17
+ # * `background_color`
18
+ # * `set_normal_text_color "fff"`
19
+ # * `normal_text_color`
20
+ # * `set_font_size 12`
21
+ # * `font_size`
22
+ # * `set_font_name "DejaVu Sans Mono"`
23
+ # * `font_name`
24
+ #
25
+ def self.method_missing(method_sym, *arguments, &block)
26
+ if method_sym.to_s =~ /^set_(.*)$/
27
+ self.set($1.gsub(/_/, " ") => arguments.first)
28
+ elsif method_sym.to_s =~ /^([a-z]+_[a-z]+(.*))$/
29
+ self.get($1.gsub(/_/, " "))
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ # Pretty big todo, shield this off somewhat.
36
+ def self.respond_to?(method_sym, include_private = false)
37
+ if method_sym.to_s =~ /^set_(.*)$/
38
+ true
39
+ elsif method_sym.to_s =~ /^[a-z]+_[a-z]+(.*)$/
40
+ true
41
+ else
42
+ super
43
+ end
44
+ end
45
+
46
+ protected
47
+
48
+ def self.set(conditions = {})
49
+ Wasko::Applescript.run do
50
+ self.set_script(conditions.keys.first, conditions.values.first)
51
+ end
52
+ end
53
+
54
+ def self.get(object)
55
+ Wasko::Applescript.run do
56
+ self.get_script(object)
57
+ end
58
+ end
59
+
60
+ # ## Utility methods
61
+ #
62
+ # Setter
63
+ #
64
+ # Wasko::Terminal.set_script("background color", "red")
65
+ #
66
+ def self.set_script(object, value)
67
+ unless value =~ /^(\{|\[|\()/
68
+ value = "\"#{value}\""
69
+ end
70
+ <<SCRIPT
71
+ tell application "Terminal"
72
+ set #{object} of selected tab of first window to #{value}
73
+ end tell
74
+ SCRIPT
75
+ end
76
+
77
+ # Getter
78
+ #
79
+ # Wasko::Terminal.get_script("background color")
80
+ #
81
+ def self.get_script(object)
82
+ <<SCRIPT
83
+ tell application "Terminal"
84
+ get #{object} of selected tab of first window
85
+ end tell
86
+ SCRIPT
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
3
+ require "rubygems"
4
+ require "wasko"
5
+
6
+ # Quick and simple class to see output of a generated
7
+ # color scheme.
8
+ class WaskoGenerator
9
+ # Initialize with a color (color can be any css-valid color)
10
+ def initialize(color_string = nil)
11
+ @palette = Wasko::Palette.new(color_string) if color_string
12
+ end
13
+
14
+ def to_html!
15
+ File.open("samples.html", 'w') do |f|
16
+ if @palette
17
+ f.write self.class.wrap_in_html{ html_palette}
18
+ else
19
+ f.write self.class.wrap_in_html{ rainbow_palette}
20
+ end
21
+ end
22
+ end
23
+
24
+ def html_palette
25
+ html = ""
26
+ @palette.colors.each do |color_name, color|
27
+ html << "<div class=\"color\" style=\"background-color: #{color.html}\">"
28
+ html << "\t <p>#{color_name} #{color.html}</p>"
29
+ html << "</div>"
30
+ end
31
+ html
32
+ end
33
+
34
+ def rainbow_palette
35
+ html = ""
36
+ %w(black red green yellow blue white).each do |color_string|
37
+ palette = Wasko::Palette.new(color_string)
38
+ html << "<div class=\"colorblock\">"
39
+ html << "<h3>#{color_string}</h3>"
40
+ palette.colors.each do |color_name, color|
41
+ html << "<div class=\"color\" style=\"background-color: #{color.html}\">"
42
+ html << "\t <p>#{color_name} #{color.html}</p>"
43
+ html << "</div>"
44
+ end
45
+ html << "</div>"
46
+ end
47
+ html
48
+ end
49
+
50
+ def self.wrap_in_html
51
+ <<HEADER
52
+ <html>
53
+ <head>
54
+ <style type="text/css" media="screen">
55
+ .colorblock {
56
+ float:left;
57
+ width: 300px;
58
+ padding-left:10px;
59
+ }
60
+ .color {
61
+ display: block;
62
+ width: 300px;
63
+ height: 30px;
64
+ background-color: yellow;
65
+ padding: 10px;
66
+ }
67
+ .color p {
68
+ margin: 10px;
69
+ background: white ; opacity: 0.2;
70
+ }
71
+ </style>
72
+ </head>
73
+ <body>
74
+ #{yield}
75
+ </body>
76
+ </html>
77
+ HEADER
78
+ end
79
+ end
80
+
81
+ WaskoGenerator.new(ARGV.first).to_html!
82
+
83
+
84
+
85
+
86
+
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'mocha'
13
+
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ require 'wasko'
17
+
18
+ class Test::Unit::TestCase
19
+ end
@@ -0,0 +1,8 @@
1
+ require 'helper'
2
+
3
+ class TestWaskoApplescript < Test::Unit::TestCase
4
+ should "run an applescript and catch its return value" do
5
+ value = Wasko::Applescript.run {"set ten_and_ten to 10 + 10"}
6
+ assert_equal 20.to_s, value
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ require 'helper'
2
+
3
+ class TestWaskoColor < Test::Unit::TestCase
4
+
5
+ should "convert from applescript" do
6
+ assert_equal ::Color::RGB.from_html("#fff"), ::Color::RGB.from_applescript("{65535,65535,65535}")
7
+ end
8
+
9
+ should "convert to applescript" do
10
+ assert_equal "{65535, 65535, 65535}", ::Color::RGB.from_html("#fff").to_applescript
11
+ end
12
+
13
+ should "convert #fff to Color" do
14
+ assert_equal ::Color::RGB.from_html("#fff"), Wasko::Color.color_from_string("#fff")
15
+ end
16
+
17
+ should "convert #cccccc to Color" do
18
+ assert_equal ::Color::RGB.from_html("#cccccc"), Wasko::Color.color_from_string("#cccccc")
19
+ end
20
+
21
+ should "convert aliceblue to Color" do
22
+ assert_equal ::Color::RGB.from_html("#f0f8ff"), Wasko::Color.color_from_string("aliceblue")
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ require 'helper'
2
+
3
+ class TestWaskoPalette < Test::Unit::TestCase
4
+
5
+ %w(white black blue green red 001e26 pink).each do |color_string|
6
+ context "creating a palette for #{color_string}" do
7
+
8
+ setup do
9
+ @base_color = Wasko::Color.color_from_string("white")
10
+ @palette = Wasko::Palette::TheOriginal.new("white")
11
+ end
12
+
13
+ should "have a foreground color with a high contrast" do
14
+ assert 0.5 < (@palette.colors[:foreground].brightness - @base_color.brightness).abs
15
+ end
16
+
17
+ should "set its base color as the background color" do
18
+ assert_equal @base_color, @palette.colors[:base]
19
+ end
20
+ end
21
+ end
22
+
23
+ should "generate all colors in its hash" do
24
+ palette = Wasko::Palette::TheOriginal.new("white")
25
+ colors = %w(base foreground bold selection selected cursor red green yellow white black blue magenta cyan)
26
+
27
+ assert_equal [],palette.colors.keys - colors.map(&:to_sym)
28
+ end
29
+
30
+ should "generate a white colorscheme on faulty input" do
31
+ palette = Wasko::Palette::TheOriginal.new("fokdajong")
32
+ palette.colors[:base] = palette.white
33
+ end
34
+
35
+ end
36
+
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ class TestTerminal < Test::Unit::TestCase
4
+ context "defining dynamic methods" do
5
+ setup do
6
+ @terminal = Wasko::Terminal
7
+ end
8
+
9
+ %w(background_color normal_text_color font_size font_name cursor_color).each do |method|
10
+ should "support getter #{method}" do
11
+ assert @terminal.respond_to? method.to_sym
12
+ end
13
+
14
+ should "support setter set_#{method}" do
15
+ assert @terminal.respond_to? "set_#{method}".to_sym
16
+ end
17
+ end
18
+ end
19
+
20
+ # TODO: Mock the `Wasko::Applescript` and see if the
21
+ # actual script gets called.
22
+ end
@@ -0,0 +1,107 @@
1
+ require 'helper'
2
+
3
+ class TestWasko < Test::Unit::TestCase
4
+
5
+ context "setting palette" do
6
+ setup do
7
+ @color = Wasko::Color.color_from_string("white")
8
+ palette = mock()
9
+ palette.expects(:colors).returns({:base => @color, :foreground => @color, :bold => @color, :cursor => @color}).at_least_once
10
+ Wasko::Palette::TheOriginal.expects(:new).with("white").returns(palette)
11
+ end
12
+
13
+ should "draw a palette" do
14
+ Wasko.expects(:set_background_color).with(@color.html)
15
+ Wasko.expects(:set_foreground_color).with(@color.html)
16
+ Wasko.expects(:set_bold_color).with(@color.html)
17
+ Wasko.expects(:set_cursor_color).with(@color.html)
18
+ Wasko.set_palette("white")
19
+ end
20
+
21
+ end
22
+
23
+ should "set palette" do
24
+
25
+ end
26
+
27
+ should "get font size" do
28
+ ata = mock()
29
+ ata.expects(:font_size).returns("14")
30
+ Wasko.expects(:advanced_typing_apparatus).returns(ata)
31
+ assert_equal Wasko.font_size, "14"
32
+ end
33
+
34
+ should "set font size" do
35
+ ata = mock()
36
+ ata.expects(:set_font_size).with("14")
37
+ Wasko.expects(:advanced_typing_apparatus).returns(ata)
38
+ Wasko.set_font_size "14"
39
+ end
40
+
41
+ should "get font name" do
42
+ ata = mock()
43
+ ata.expects(:font_name).returns("SomeFont")
44
+ Wasko.expects(:advanced_typing_apparatus).returns(ata)
45
+ assert_equal Wasko.font_name, "SomeFont"
46
+ end
47
+
48
+ should "set font name" do
49
+ ata = mock()
50
+ ata.expects(:set_font_name).with("SomeFont")
51
+ Wasko.expects(:advanced_typing_apparatus).returns(ata)
52
+ Wasko.set_font_name("SomeFont")
53
+ end
54
+
55
+ should "get font" do
56
+ ata = mock()
57
+ ata.expects(:font_name).returns("SomeFont")
58
+ ata.expects(:font_size).returns("14")
59
+ Wasko.expects(:advanced_typing_apparatus).returns(ata).at_least_once
60
+ assert_equal Wasko.font, "SomeFont, 14"
61
+ end
62
+
63
+ should "set font" do
64
+ ata = mock()
65
+ ata.expects(:set_font_name).with("SomeFont")
66
+ ata.expects(:set_font_size).with(14)
67
+ Wasko.expects(:advanced_typing_apparatus).returns(ata).at_least_once
68
+ Wasko.set_font(14, "SomeFont")
69
+ end
70
+
71
+ # Just so we can dynamically create the color tests.
72
+ def method_to_expect(method)
73
+ case method
74
+ when "foreground_color"
75
+ "normal_text_color"
76
+ when "bold_color"
77
+ "bold_text_color"
78
+ else
79
+ method
80
+ end
81
+ end
82
+
83
+ # Test all getter/setter colors
84
+ context "getting/setting regular colors" do
85
+ %w(background_color cursor_color foreground_color bold_color).each do |method|
86
+
87
+ setup do
88
+ @color = Wasko::Color.color_from_string("white")
89
+ end
90
+
91
+ should "get the #{method} of the advanced typing apparatus" do
92
+ ata = mock()
93
+ ata.expects(method_to_expect(method).to_sym).returns(@color.to_applescript)
94
+ Wasko.expects(:advanced_typing_apparatus).returns(ata)
95
+ assert_equal @color.html, Wasko.send(method)
96
+ end
97
+
98
+ should "set the #{method} of the advanced typing apparatus" do
99
+
100
+ ata = mock()
101
+ ata.expects("set_#{method_to_expect(method)}".to_sym).with(@color.to_applescript)
102
+ Wasko.expects(:advanced_typing_apparatus).returns(ata)
103
+ Wasko.send("set_#{method}", @color.html)
104
+ end
105
+ end
106
+ end
107
+ end
data/wasko.gemspec ADDED
@@ -0,0 +1,111 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{wasko}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["pjaspers"]
12
+ s.date = %q{2011-05-31}
13
+ s.default_executable = %q{wasko}
14
+ s.description = %q{Quick tool to set a color palette to your Terminal}
15
+ s.email = %q{piet@10to1.be}
16
+ s.executables = ["wasko"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.mdown"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.mdown",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/wasko",
30
+ "docs/applescript.html",
31
+ "docs/color.html",
32
+ "docs/configuration.html",
33
+ "docs/docco.css",
34
+ "docs/palette.html",
35
+ "docs/terminal.html",
36
+ "docs/wasko.html",
37
+ "lib/wasko.rb",
38
+ "lib/wasko/applescript.rb",
39
+ "lib/wasko/color.rb",
40
+ "lib/wasko/configuration.rb",
41
+ "lib/wasko/palette.rb",
42
+ "lib/wasko/terminal.rb",
43
+ "sample_generator.rb",
44
+ "test/helper.rb",
45
+ "test/test_applescript.rb",
46
+ "test/test_color.rb",
47
+ "test/test_palette.rb",
48
+ "test/test_terminal.rb",
49
+ "test/test_wasko.rb",
50
+ "wasko.gemspec"
51
+ ]
52
+ s.homepage = %q{http://github.com/pjaspers/wasko}
53
+ s.licenses = ["MIT"]
54
+ s.require_paths = ["lib"]
55
+ s.rubygems_version = %q{1.6.2}
56
+ s.summary = %q{Set your (OS X) terminal background from the command line}
57
+ s.test_files = [
58
+ "test/helper.rb",
59
+ "test/test_applescript.rb",
60
+ "test/test_color.rb",
61
+ "test/test_palette.rb",
62
+ "test/test_terminal.rb",
63
+ "test/test_wasko.rb"
64
+ ]
65
+
66
+ if s.respond_to? :specification_version then
67
+ s.specification_version = 3
68
+
69
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
70
+ s.add_runtime_dependency(%q<thor>, [">= 0"])
71
+ s.add_runtime_dependency(%q<color>, [">= 0"])
72
+ s.add_runtime_dependency(%q<rake>, ["= 0.8.7"])
73
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
74
+ s.add_development_dependency(%q<mocha>, [">= 0"])
75
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
76
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
77
+ s.add_development_dependency(%q<rcov>, [">= 0"])
78
+ s.add_runtime_dependency(%q<color>, [">= 0"])
79
+ s.add_runtime_dependency(%q<thor>, [">= 0"])
80
+ s.add_development_dependency(%q<should>, [">= 0"])
81
+ s.add_development_dependency(%q<mocha>, [">= 0"])
82
+ else
83
+ s.add_dependency(%q<thor>, [">= 0"])
84
+ s.add_dependency(%q<color>, [">= 0"])
85
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
86
+ s.add_dependency(%q<shoulda>, [">= 0"])
87
+ s.add_dependency(%q<mocha>, [">= 0"])
88
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
89
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
90
+ s.add_dependency(%q<rcov>, [">= 0"])
91
+ s.add_dependency(%q<color>, [">= 0"])
92
+ s.add_dependency(%q<thor>, [">= 0"])
93
+ s.add_dependency(%q<should>, [">= 0"])
94
+ s.add_dependency(%q<mocha>, [">= 0"])
95
+ end
96
+ else
97
+ s.add_dependency(%q<thor>, [">= 0"])
98
+ s.add_dependency(%q<color>, [">= 0"])
99
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
100
+ s.add_dependency(%q<shoulda>, [">= 0"])
101
+ s.add_dependency(%q<mocha>, [">= 0"])
102
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
103
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
104
+ s.add_dependency(%q<rcov>, [">= 0"])
105
+ s.add_dependency(%q<color>, [">= 0"])
106
+ s.add_dependency(%q<thor>, [">= 0"])
107
+ s.add_dependency(%q<should>, [">= 0"])
108
+ s.add_dependency(%q<mocha>, [">= 0"])
109
+ end
110
+ end
111
+