ripl-rocket 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems' unless defined? Gem
3
+ require File.dirname(__FILE__) + "/lib/ripl/rocket"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ripl-rocket"
7
+ s.version = Ripl::Rocket::VERSION
8
+ s.authors = ["Jan Lelis"]
9
+ s.email = "mail@janlelis.de"
10
+ s.homepage = "http://github.com/janlelis/ripl-rocket"
11
+ s.summary = "Rocketize your ripl output."
12
+ s.description = "Lets you display the ripl result as a comment on the same line."
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.add_dependency 'ripl', '>= 0.3.0'
15
+ s.add_dependency 'unicode-display_width', '>= 0.1.1'
16
+ s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c} **/deps.rip]) + %w{Rakefile .gemspec}
17
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
18
+ s.license = 'MIT'
19
+ end
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ == 0.1.0
2
+ * Initial release.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2011 Jan Lelis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,35 @@
1
+ == Description
2
+ This {ripl}[http://github.com/cldwalker/ripl] plugin lets you output your result as a comment (using the #=> rocket).
3
+
4
+ == Install
5
+ Install the gem with:
6
+
7
+ gem install ripl-colorize_output
8
+
9
+ == Usage
10
+
11
+ Add to your <tt>~/.riplrc</tt>
12
+
13
+ require 'ripl/rocket'
14
+
15
+ == Configuration
16
+
17
+ There is a <tt>Ripl.config[:rocket_mode]</tt> setting. If it is set to </tt>false</tt>, the rocket is not used.
18
+
19
+ The default option is <tt>:auto</tt>, which means the rocket will be used, if there is enough space on the last line and there has not been too much standard output.
20
+
21
+ Set it to any other true value and the plugin will always use the rocket.
22
+
23
+ You can change the rocket prompt with <tt>Ripl.config[:rocket_prompt]</tt> and set a color with <tt>Ripl.config[:rocket_color]</tt>
24
+
25
+ == Bugs / TODO
26
+
27
+ Input methods are currently not supported.
28
+
29
+ == Copyright
30
+
31
+ Thanks to genki for the original idea :).
32
+
33
+ Copyright (c) 2011 Jan Lelis <http://rbjl.net> released under the MIT license.
34
+
35
+ J-_-L
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'fileutils'
3
+
4
+ def gemspec
5
+ @gemspec ||= eval(File.read('.gemspec'), binding, '.gemspec')
6
+ end
7
+
8
+ desc "Build the gem"
9
+ task :gem => :gemspec do
10
+ sh "gem build .gemspec"
11
+ FileUtils.mkdir_p 'pkg'
12
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
13
+ end
14
+
15
+ desc "Install the gem locally"
16
+ task :install => :gem do
17
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version} --no-rdoc --no-ri}
18
+ end
19
+
20
+ desc "Generate the gemspec"
21
+ task :generate do
22
+ puts gemspec.to_ruby
23
+ end
24
+
25
+ desc "Validate the gemspec"
26
+ task :gemspec do
27
+ gemspec.validate
28
+ end
data/deps.rip ADDED
@@ -0,0 +1,2 @@
1
+ ripl >=0.3.0
2
+ unicode-display_width >=0.1.1
@@ -0,0 +1,117 @@
1
+ require 'ripl'
2
+ require 'unicode/display_width'
3
+
4
+ module Ripl
5
+ module Rocket
6
+ VERSION = '0.1.0'
7
+
8
+ TPUT = {
9
+ :sc => `tput sc`,
10
+ :rc => `tput rc`,
11
+ :cuu1 => `tput cuu1`,
12
+ :cuf1 => `tput cuf1`,
13
+ }
14
+
15
+ COLORS = {
16
+ :nothing => '0;0',
17
+ :black => '0;30',
18
+ :red => '0;31',
19
+ :green => '0;32',
20
+ :brown => '0;33',
21
+ :blue => '0;34',
22
+ :purple => '0;35',
23
+ :cyan => '0;36',
24
+ :light_gray => '0;37',
25
+ :dark_gray => '1;30',
26
+ :light_red => '1;31',
27
+ :light_green => '1;32',
28
+ :yellow => '1;33',
29
+ :light_blue => '1;34',
30
+ :light_purple => '1;35',
31
+ :light_cyan => '1;36',
32
+ :white => '1;37',
33
+ }
34
+
35
+ class << self
36
+ def reset_output_height
37
+ @height_counter = []
38
+ end
39
+
40
+ def get_height(data)
41
+ lines = data.to_s.count("\n")
42
+ long_lines = data.to_s.split("\n").inject(0){ |sum, line|
43
+ sum + (line.display_size / `tput cols`.to_i)
44
+ }
45
+ lines + long_lines
46
+ end
47
+
48
+ def track_output_height(data)
49
+ @height_counter ||= []
50
+ @height_counter << get_height(data)
51
+ end
52
+
53
+ def output_height
54
+ 1 + ( @height_counter == [0] ? 0 : @height_counter.reduce(:+) || 0 )
55
+ end
56
+ end
57
+
58
+ module Shell
59
+ def loop_eval(input)
60
+ Ripl::Rocket.reset_output_height
61
+ super
62
+ end
63
+
64
+ def print_result(result)
65
+ return if @error_raised
66
+ if config[:rocket_mode]
67
+ # get important data
68
+ screen_length = `tput cols`.to_i
69
+ screen_height = `tput lines`.to_i
70
+ last_line_without_prompt = @input.split("\n").last || ''
71
+ offset = (last_line_without_prompt.display_size + prompt.display_size ) % screen_length.to_i
72
+ output_length = result.inspect.display_size
73
+ rocket_length = config[:rocket_prompt].display_size
74
+ stdout_height = Ripl::Rocket.output_height
75
+ color_config = config[:rocket_color]
76
+ color_code = !color_config || color_config.to_s[/^[\d;]+$/] ?
77
+ color_config : Ripl::Rocket::COLORS[color_config.to_sym]
78
+ colored_rocket = color_code ? "\e[#{ color_code }m#{ config[:rocket_prompt] }\e[0;0m"
79
+ : config[:rocket_prompt]
80
+
81
+ # auto rocket mode: only display rocket if line has enough space left
82
+ line_too_long = screen_length <= offset + rocket_length + output_length
83
+ height_too_long = stdout_height >= screen_height
84
+ if !(config[:rocket_mode] == :auto && ( line_too_long || height_too_long ) )
85
+ if !height_too_long
86
+ print TPUT[:sc] + # save cursor position
87
+ TPUT[:cuu1]*stdout_height + # move cursor upwards to the original input line
88
+ TPUT[:cuf1]*offset + # move cursor rightwards to the original input offset
89
+ colored_rocket + # draw rocket prompt
90
+ format_result(result).sub( /^#{result_prompt}/, '' ) + # draw result (without prompt)
91
+ TPUT[:rc] + # restore cursor position
92
+ ( config[:rocket_mode] != :auto && line_too_long ? "\n" : '') # add a line for always-rocket mode
93
+
94
+ else # too much stdout, but in "always rocket mode"
95
+ puts colored_rocket + # draw rocket prompt
96
+ format_result(result).sub( /^#{result_prompt}/, '' ) # draw result (without prompt)
97
+ end
98
+ return
99
+ end
100
+ end
101
+ # else: no rocket ;)
102
+ super
103
+ end
104
+ end
105
+
106
+ # load stream extensions
107
+ require File.dirname(__FILE__) + "/rocket/stream_ext"
108
+ end
109
+ end
110
+
111
+ Ripl::Shell.include Ripl::Rocket::Shell
112
+
113
+ Ripl.config[:rocket_mode] = :auto if Ripl.config[:rocket_mode].nil?
114
+ Ripl.config[:rocket_prompt] ||= " #=> "
115
+ Ripl.config[:rocket_color] ||= :blue
116
+
117
+ # J-_-L
@@ -0,0 +1,18 @@
1
+ # hook into streams to count lines used in stdout
2
+ class << $stdout
3
+ alias write_without_rocket write
4
+ def write(data)
5
+ Ripl::Rocket.track_output_height data
6
+ write_without_rocket data
7
+ end
8
+ end
9
+
10
+ class << $stderr
11
+ alias write_without_rocket write
12
+ def write(data)
13
+ Ripl::Rocket.track_output_height data
14
+ write_without_rocket data
15
+ end
16
+ end
17
+
18
+ # J-_-L
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ripl-rocket
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Jan Lelis
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-07 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ripl
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 3
31
+ - 0
32
+ version: 0.3.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: unicode-display_width
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 1
46
+ - 1
47
+ version: 0.1.1
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: Lets you display the ripl result as a comment on the same line.
51
+ email: mail@janlelis.de
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - README.rdoc
58
+ - LICENSE.txt
59
+ files:
60
+ - lib/ripl/rocket/stream_ext.rb
61
+ - lib/ripl/rocket.rb
62
+ - LICENSE.txt
63
+ - README.rdoc
64
+ - CHANGELOG.rdoc
65
+ - deps.rip
66
+ - Rakefile
67
+ - .gemspec
68
+ has_rdoc: true
69
+ homepage: http://github.com/janlelis/ripl-rocket
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 1
92
+ - 3
93
+ - 6
94
+ version: 1.3.6
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Rocketize your ripl output.
102
+ test_files: []
103
+