simple-ui 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ # simple-ui
2
+
3
+ A really simple UI helper, supporting
4
+
5
+ - logging severities (:debug, :info, :warn, :error, :success)
6
+
7
+ UI.warn "This is a warning"
8
+
9
+ - ...with different colors...
10
+
11
+ UI.success "success is green"
12
+ UI.error "error is red"
13
+
14
+ - ...with inspect'ing arguments after the first
15
+
16
+ UI.warn "Some things", 1, "foo\nline", :bar
17
+ # =>
18
+ # Some things 1, "foo\nline", :bar
19
+
20
+ - ...with support for source names
21
+
22
+ url = "http://radiospiel.org"
23
+ url.warn "Found a new blog post"
24
+ # =>
25
+ # [http://radiospiel.org] Found a new blog post
26
+
27
+ Well, nothing too exciting; yet easy enough to throw into your projects:
28
+
29
+ require "simple/ui"
@@ -0,0 +1,122 @@
1
+ module UI
2
+ VERSION = "0.1.2"
3
+
4
+ def self.verbosity
5
+ @verbosity ||= 1
6
+ end
7
+
8
+ def self.verbosity=(verbosity)
9
+ @verbosity = verbosity
10
+ end
11
+
12
+ COLORS = {
13
+ clear: "\e[0m", # Embed in a String to clear all previous ANSI sequences.
14
+ bold: "\e[1m", # The start of an ANSI bold sequence.
15
+ black: "\e[30m", # Set the terminal's foreground ANSI color to black.
16
+ red: "\e[31m", # Set the terminal's foreground ANSI color to red.
17
+ green: "\e[32m", # Set the terminal's foreground ANSI color to green.
18
+ yellow: "\e[33m", # Set the terminal's foreground ANSI color to yellow.
19
+ blue: "\e[34m", # Set the terminal's foreground ANSI color to blue.
20
+ magenta: "\e[35m", # Set the terminal's foreground ANSI color to magenta.
21
+ cyan: "\e[36m", # Set the terminal's foreground ANSI color to cyan.
22
+ white: "\e[37m", # Set the terminal's foreground ANSI color to white.
23
+
24
+ on_black: "\e[40m", # Set the terminal's background ANSI color to black.
25
+ on_red: "\e[41m", # Set the terminal's background ANSI color to red.
26
+ on_green: "\e[42m", # Set the terminal's background ANSI color to green.
27
+ on_yellow: "\e[43m", # Set the terminal's background ANSI color to yellow.
28
+ on_blue: "\e[44m", # Set the terminal's background ANSI color to blue.
29
+ on_magenta: "\e[45m", # Set the terminal's background ANSI color to magenta.
30
+ on_cyan: "\e[46m", # Set the terminal's background ANSI color to cyan.
31
+ on_white: "\e[47m" # Set the terminal's background ANSI color to white.
32
+ }
33
+
34
+ @@started_at = Time.now
35
+
36
+ MESSAGE_COLOR = {
37
+ :info => :cyan,
38
+ :warn => :yellow,
39
+ :error => :red,
40
+ :success => :green,
41
+ }
42
+
43
+ MIN_VERBOSITY = {
44
+ :debug => 3,
45
+ :info => 2,
46
+ :warn => 1,
47
+ :error => 0,
48
+ :success => 0
49
+ }
50
+
51
+ def debug(msg, *args)
52
+ UI.log self, :debug, msg, *args
53
+ end
54
+
55
+ def info(msg, *args)
56
+ UI.log self, :info, msg, *args
57
+ end
58
+
59
+ def warn(msg, *args)
60
+ UI.log self, :warn, msg, *args
61
+ end
62
+
63
+ def error(msg, *args)
64
+ UI.log self, :error, msg, *args
65
+ end
66
+
67
+ def success(msg, *args)
68
+ UI.log self, :success, msg, *args
69
+ end
70
+
71
+ def benchmark(msg, *args, &block)
72
+ severity = :warn
73
+ if msg.is_a?(Symbol)
74
+ severity, msg = msg, args.shift
75
+ end
76
+
77
+ start = Time.now
78
+ yield.tap do
79
+ msg += ": #{(1000 * (Time.now - start)).to_i} msecs."
80
+ UI.log self, severity, msg, *args
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def self.log(sender, sym, msg, *args)
87
+ rv = args.empty? ? msg : args.first
88
+
89
+ return rv unless verbosity >= MIN_VERBOSITY[sym]
90
+
91
+ unless args.empty?
92
+ msg += ": " + args.map(&:inspect).join(", ")
93
+ end
94
+
95
+ timestamp = "[%3d msecs]" % (1000 * (Time.now - @@started_at))
96
+
97
+ if sender_name = sender.send(:log_inspect)
98
+ sender_name = "[#{sender_name}] "
99
+ end
100
+
101
+ msg = "#{timestamp} #{sender_name}#{msg}"
102
+
103
+ if color = COLORS[MESSAGE_COLOR[sym]]
104
+ msg = "#{color}#{msg}#{COLORS[:clear]}"
105
+ end
106
+
107
+ STDERR.puts msg
108
+
109
+ rv
110
+ end
111
+
112
+ def log_inspect
113
+ end
114
+ end
115
+
116
+ class Object
117
+ include UI
118
+ end
119
+
120
+ class String
121
+ alias :log_inspect :to_s
122
+ end
@@ -0,0 +1,10 @@
1
+ # Author:: radiospiel (mailto:eno@radiospiel.org)
2
+ # Copyright:: Copyright (c) 2011, 2012 radiospiel
3
+ # License:: Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
4
+ require_relative 'test_helper'
5
+
6
+ class SimpleUITest < Test::Unit::TestCase
7
+ def test_loaded
8
+ "string".warn "hello world!"
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'simplecov'
5
+ require 'test/unit'
6
+ require 'test/unit/ui/console/testrunner'
7
+
8
+ class Test::Unit::UI::Console::TestRunner
9
+ def guess_color_availability; true; end
10
+ end
11
+
12
+ require 'mocha'
13
+
14
+ SimpleCov.start do
15
+ add_filter "test/*.rb"
16
+ end
17
+
18
+ require "simple/ui"
19
+
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-ui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - radiospiel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple code for simple things in the console
15
+ email: eno@radiospiel.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/simple/ui.rb
21
+ - README.md
22
+ - test/simple_ui_test.rb
23
+ - test/test_helper.rb
24
+ homepage: http://github.com/radiospiel/simple-ui
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ segments:
37
+ - 0
38
+ hash: 4546853688719557902
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ segments:
46
+ - 0
47
+ hash: 4546853688719557902
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Simple code for simple things in the console
54
+ test_files: []