debugger-ruby_core_source 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/LEGAL +534 -0
  2. data/LICENSE +23 -0
  3. data/LICENSE.txt +22 -0
  4. data/OLD_README +29 -0
  5. data/README.md +27 -0
  6. data/RUBY_LICENSE +56 -0
  7. data/Rakefile +32 -0
  8. data/debugger-ruby_core_source.gemspec +18 -0
  9. data/lib/contrib/progressbar.rb +237 -0
  10. data/lib/contrib/uri_ext.rb +289 -0
  11. data/lib/debugger/ruby_core_source.rb +7 -0
  12. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/debug.h +36 -0
  13. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/dln.h +41 -0
  14. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/encdb.h +163 -0
  15. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/eval_intern.h +232 -0
  16. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/gc.h +77 -0
  17. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/id.h +170 -0
  18. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/insns.inc +179 -0
  19. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/insns_info.inc +695 -0
  20. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/iseq.h +104 -0
  21. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/known_errors.inc +731 -0
  22. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/method.h +103 -0
  23. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/node.h +483 -0
  24. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/node_name.inc +208 -0
  25. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/opt_sc.inc +670 -0
  26. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/optinsn.inc +30 -0
  27. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/optunifs.inc +116 -0
  28. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/parse.h +187 -0
  29. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/regenc.h +211 -0
  30. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/regint.h +841 -0
  31. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/regparse.h +354 -0
  32. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/revision.h +1 -0
  33. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/thread_pthread.h +27 -0
  34. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/thread_win32.h +33 -0
  35. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/timev.h +21 -0
  36. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/transcode_data.h +109 -0
  37. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/transdb.h +179 -0
  38. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/version.h +55 -0
  39. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/vm.inc +3055 -0
  40. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/vm_core.h +707 -0
  41. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/vm_exec.h +184 -0
  42. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/vm_insnhelper.h +208 -0
  43. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/vm_opts.h +51 -0
  44. data/lib/debugger/ruby_core_source/ruby-1.9.2-p290/vmtc.inc +97 -0
  45. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/addr2line.h +21 -0
  46. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/atomic.h +56 -0
  47. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/constant.h +34 -0
  48. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/debug.h +41 -0
  49. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/dln.h +50 -0
  50. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/encdb.h +167 -0
  51. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/eval_intern.h +234 -0
  52. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/gc.h +98 -0
  53. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/id.h +175 -0
  54. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/insns.inc +179 -0
  55. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/insns_info.inc +695 -0
  56. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/internal.h +227 -0
  57. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/iseq.h +125 -0
  58. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/known_errors.inc +731 -0
  59. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/method.h +105 -0
  60. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/node.h +503 -0
  61. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/node_name.inc +208 -0
  62. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/opt_sc.inc +670 -0
  63. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/optinsn.inc +30 -0
  64. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/optunifs.inc +116 -0
  65. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/parse.h +186 -0
  66. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/regenc.h +219 -0
  67. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/regint.h +851 -0
  68. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/regparse.h +362 -0
  69. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/revision.h +1 -0
  70. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/thread_pthread.h +51 -0
  71. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/thread_win32.h +40 -0
  72. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/timev.h +21 -0
  73. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/transcode_data.h +117 -0
  74. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/transdb.h +189 -0
  75. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/version.h +52 -0
  76. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/vm.inc +3067 -0
  77. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/vm_core.h +756 -0
  78. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/vm_exec.h +184 -0
  79. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/vm_insnhelper.h +220 -0
  80. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/vm_opts.h +51 -0
  81. data/lib/debugger/ruby_core_source/ruby-1.9.3-p0/vmtc.inc +97 -0
  82. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/addr2line.h +21 -0
  83. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/atomic.h +56 -0
  84. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/constant.h +34 -0
  85. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/debug.h +41 -0
  86. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/dln.h +50 -0
  87. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/encdb.h +167 -0
  88. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/eval_intern.h +234 -0
  89. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/gc.h +98 -0
  90. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/id.h +175 -0
  91. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/insns.inc +179 -0
  92. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/insns_info.inc +695 -0
  93. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/internal.h +231 -0
  94. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/iseq.h +125 -0
  95. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/known_errors.inc +731 -0
  96. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/method.h +105 -0
  97. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/node.h +503 -0
  98. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/node_name.inc +208 -0
  99. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/opt_sc.inc +670 -0
  100. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/optinsn.inc +30 -0
  101. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/optunifs.inc +116 -0
  102. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/parse.h +186 -0
  103. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/regenc.h +219 -0
  104. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/regint.h +850 -0
  105. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/regparse.h +362 -0
  106. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/revision.h +1 -0
  107. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/thread_pthread.h +51 -0
  108. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/thread_win32.h +40 -0
  109. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/timev.h +21 -0
  110. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/transcode_data.h +117 -0
  111. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/transdb.h +189 -0
  112. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/version.h +52 -0
  113. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/vm.inc +3067 -0
  114. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/vm_core.h +756 -0
  115. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/vm_exec.h +184 -0
  116. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/vm_insnhelper.h +220 -0
  117. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/vm_opts.h +51 -0
  118. data/lib/debugger/ruby_core_source/ruby-1.9.3-p125/vmtc.inc +97 -0
  119. data/lib/debugger/ruby_core_source/version.rb +5 -0
  120. data/lib/ruby_core_source.rb +62 -0
  121. metadata +198 -0
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (C) 2011 Mark Moseley <mark@fast-software.com>
2
+ All rights reserved.
3
+ *
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+ *
13
+ THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
+ ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
+ SUCH DAMAGE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2012 Gabriel Horner
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/OLD_README ADDED
@@ -0,0 +1,29 @@
1
+ Retrieve ruby core source files.
2
+
3
+ This can be used by any C extension gem that needs Ruby core headers (*.h and *.inc)
4
+
5
+ Usage:
6
+
7
+ Ruby_core_source::create_makefile_with_core(hdr_check_proc, gem_name)
8
+ takes the place of
9
+ create_makefile(gem_name)
10
+ in your extconf.rb
11
+
12
+ This will:
13
+ If hdr_check_proc is true, call create_makefile(gem_name)
14
+ Else: if core headers already exist, and hdr_check_proc is true, call create_makefile(gem_name)
15
+ Else: fetch and extract the core headers, check hdr_check_proc, and call create_makefile(gem_name)
16
+ Else: return false
17
+
18
+ Note that for Ruby preview releases, the corresponding RUBY_REVISION needs to be maintained in
19
+ http://cloud.github.com/downloads/mark-moseley/ruby_core_source/preview_revision.yml
20
+
21
+ Example use in extconf.rb:
22
+
23
+ require 'ruby_core_source'
24
+ hdrs = proc { have_header("vm_core.h") and have_header("iseq.h") }
25
+ dir_config("ruby") # allow user to pass in non-standard core include directory
26
+ if !Ruby_core_source::create_makefile_with_core(hdrs, "foo")
27
+ # error
28
+ exit(1)
29
+ end
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ ## Description
2
+ Fork of [ruby\_core\_source](https://github.com/mark-moseley/ruby_core_source)
3
+ that uses included Ruby headers (\*.h and \*.inc) instead of downloading
4
+ them. Used by [debugger](http://github.com/cldwalker/debugger).
5
+
6
+ ##Usage
7
+
8
+ Example use in extconf.rb:
9
+
10
+ ```ruby
11
+ require 'ruby_core_source'
12
+ hdrs = proc { have_header("vm_core.h") and have_header("iseq.h") }
13
+ dir_config("ruby") # allow user to pass in non-standard core include directory
14
+ if !Ruby_core_source::create_makefile_with_core(hdrs, "foo")
15
+ # error
16
+ exit(1)
17
+ end
18
+ ```
19
+
20
+ To add another ruby version's source to this gem's directory:
21
+
22
+ $ rake add_source VERSION=1.9.3-p0
23
+
24
+ ## LICENSE
25
+ Ruby library code is MIT license, see LICENSE.txt. Included ruby headers,
26
+ lib/debugger/ruby\_core\_source/, are mostly Ruby license, see RUBY\_LICENSE. Some headers have
27
+ their own licenses, see LEGAL.
data/RUBY_LICENSE ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require 'tempfile'
2
+ require 'tmpdir'
3
+ require 'uri'
4
+ require 'contrib/uri_ext'
5
+ require 'archive/tar/minitar'
6
+ require 'zlib'
7
+ require 'fileutils'
8
+
9
+ desc 'Add ruby headers under lib for a given VERSION'
10
+ task :add_source do
11
+ version = ENV['VERSION'] or abort "Need a $VERSION"
12
+ ruby_dir = "ruby-#{version}"
13
+ uri_path = "http://ftp.ruby-lang.org/pub/ruby/1.9/#{ruby_dir}.tar.gz"
14
+ dest_dir = File.dirname(__FILE__) + "/lib/debugger/ruby_core_source/#{ruby_dir}"
15
+
16
+ Tempfile.open("ruby-src") do |temp|
17
+ temp.binmode
18
+ uri = URI.parse(uri_path)
19
+ uri.download(temp)
20
+
21
+ tgz = Zlib::GzipReader.new(File.open(temp, "rb"))
22
+
23
+ FileUtils.mkdir_p(dest_dir)
24
+ Dir.mktmpdir do |dir|
25
+ inc_dir = dir + "/" + ruby_dir + "/*.inc"
26
+ hdr_dir = dir + "/" + ruby_dir + "/*.h"
27
+ Archive::Tar::Minitar.unpack(tgz, dir)
28
+ FileUtils.cp(Dir.glob([ inc_dir, hdr_dir ]), dest_dir)
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems' unless defined? Gem
3
+ require File.dirname(__FILE__) + "/lib/debugger/ruby_core_source/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "debugger-ruby_core_source"
7
+ s.version = Debugger::RubyCoreSource::VERSION
8
+ s.authors = ["Mark Moseley", "Gabriel Horner"]
9
+ s.email = "gabriel.horner@gmail.com"
10
+ s.homepage = "http://github.com/cldwalker/debugger-ruby_core_source"
11
+ s.summary = %q{Provide Ruby core source files}
12
+ s.description = %q{Provide Ruby core source files for C extensions that need them.}
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.extra_rdoc_files = [ "README.md"]
15
+ s.files = `git ls-files`.split("\n")
16
+ s.add_development_dependency "archive-tar-minitar", ">= 0.5.2"
17
+ s.add_development_dependency 'rake', '~> 0.9.2'
18
+ end
@@ -0,0 +1,237 @@
1
+ # = progressbar.rb
2
+ #
3
+ # == Copyright (C) 2001 Satoru Takabayashi
4
+ #
5
+ # Ruby License
6
+ #
7
+ # This module is free software. You may use, modify, and/or redistribute this
8
+ # software under the same terms as Ruby.
9
+ #
10
+ # This program is distributed in the hope that it will be useful, but WITHOUT
11
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
+ # FOR A PARTICULAR PURPOSE.
13
+ #
14
+ # == Author(s)
15
+ #
16
+ # * Satoru Takabayashi
17
+
18
+ # Author:: Satoru Takabayashi
19
+ # Copyright:: Copyright (c) 2001 Satoru Takabayashi
20
+ # License:: Ruby License
21
+
22
+ # = Console Progress Bar
23
+ #
24
+ # Console::ProgressBar is a terminal-based progress bar library.
25
+ #
26
+ # == Usage
27
+ #
28
+ # pbar = ConsoleProgressBar.new( "Demo", 100 )
29
+ # 100.times { pbar.inc }
30
+ # pbar.finish
31
+ #
32
+
33
+ module Console; end
34
+
35
+ class Console::ProgressBar
36
+
37
+ def initialize(title, total, out = STDERR)
38
+ @title = title
39
+ @total = total
40
+ @out = out
41
+ @bar_length = 80
42
+ @bar_mark = "o"
43
+ @total_overflow = true
44
+ @current = 0
45
+ @previous = 0
46
+ @is_finished = false
47
+ @start_time = Time.now
48
+ @format = "%-14s %3d%% %s %s"
49
+ @format_arguments = [:title, :percentage, :bar, :stat]
50
+ show_progress
51
+ end
52
+
53
+ private
54
+ def convert_bytes (bytes)
55
+ if bytes < 1024
56
+ sprintf("%6dB", bytes)
57
+ elsif bytes < 1024 * 1000 # 1000kb
58
+ sprintf("%5.1fKB", bytes.to_f / 1024)
59
+ elsif bytes < 1024 * 1024 * 1000 # 1000mb
60
+ sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
61
+ else
62
+ sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
63
+ end
64
+ end
65
+
66
+ def transfer_rate
67
+ bytes_per_second = @current.to_f / (Time.now - @start_time)
68
+ sprintf("%s/s", convert_bytes(bytes_per_second))
69
+ end
70
+
71
+ def bytes
72
+ convert_bytes(@current)
73
+ end
74
+
75
+ def format_time (t)
76
+ t = t.to_i
77
+ sec = t % 60
78
+ min = (t / 60) % 60
79
+ hour = t / 3600
80
+ sprintf("%02d:%02d:%02d", hour, min, sec);
81
+ end
82
+
83
+ # ETA stands for Estimated Time of Arrival.
84
+ def eta
85
+ if @current == 0
86
+ "ETA: --:--:--"
87
+ else
88
+ elapsed = Time.now - @start_time
89
+ eta = elapsed * @total / @current - elapsed;
90
+ sprintf("ETA: %s", format_time(eta))
91
+ end
92
+ end
93
+
94
+ def elapsed
95
+ elapsed = Time.now - @start_time
96
+ sprintf("Time: %s", format_time(elapsed))
97
+ end
98
+
99
+ def stat
100
+ if @is_finished then elapsed else eta end
101
+ end
102
+
103
+ def stat_for_file_transfer
104
+ if @is_finished then
105
+ sprintf("%s %s %s", bytes, transfer_rate, elapsed)
106
+ else
107
+ sprintf("%s %s %s", bytes, transfer_rate, eta)
108
+ end
109
+ end
110
+
111
+ def eol
112
+ if @is_finished then "\n" else "\r" end
113
+ end
114
+
115
+ def bar
116
+ len = percentage * @bar_length / 100
117
+ sprintf("|%s%s|", @bar_mark * len, " " * (@bar_length - len))
118
+ end
119
+
120
+ def percentage
121
+ if @total.zero?
122
+ 100
123
+ else
124
+ @current * 100 / @total
125
+ end
126
+ end
127
+
128
+ def title
129
+ @title[0,13] + ":"
130
+ end
131
+
132
+ def get_width
133
+ # FIXME: I don't know how portable it is.
134
+ default_width = 80
135
+ begin
136
+ tiocgwinsz = 0x5413
137
+ data = [0, 0, 0, 0].pack("SSSS")
138
+ if @out.ioctl(tiocgwinsz, data) >= 0 then
139
+ rows, cols, xpixels, ypixels = data.unpack("SSSS")
140
+ if cols >= 0 then cols else default_width end
141
+ else
142
+ default_width
143
+ end
144
+ rescue Exception
145
+ default_width
146
+ end
147
+ end
148
+
149
+ def show
150
+ arguments = @format_arguments.map {|method| send(method) }
151
+ line = sprintf(@format, *arguments)
152
+
153
+ width = get_width
154
+ if line.length == width - 1
155
+ @out.print(line + eol)
156
+ elsif line.length >= width
157
+ @bar_length = [@bar_length - (line.length - width + 1), 0].max
158
+ if @bar_length == 0 then @out.print(line + eol) else show end
159
+ else #line.length < width - 1
160
+ @bar_length += width - line.length + 1
161
+ show
162
+ end
163
+ end
164
+
165
+ def show_progress
166
+ if @total.zero?
167
+ cur_percentage = 100
168
+ prev_percentage = 0
169
+ else
170
+ cur_percentage = (@current * 100 / @total).to_i
171
+ prev_percentage = (@previous * 100 / @total).to_i
172
+ end
173
+
174
+ if cur_percentage > prev_percentage || @is_finished
175
+ show
176
+ end
177
+ end
178
+
179
+ public
180
+ def file_transfer_mode
181
+ @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
182
+ end
183
+
184
+ def bar_mark= (mark)
185
+ @bar_mark = String(mark)[0..0]
186
+ end
187
+
188
+ def total_overflow= (boolv)
189
+ @total_overflow = boolv ? true : false
190
+ end
191
+
192
+ def format= (format)
193
+ @format = format
194
+ end
195
+
196
+ def format_arguments= (arguments)
197
+ @format_arguments = arguments
198
+ end
199
+
200
+ def finish
201
+ @current = @total
202
+ @is_finished = true
203
+ show_progress
204
+ end
205
+
206
+ def halt
207
+ @is_finished = true
208
+ show_progress
209
+ end
210
+
211
+ def set (count)
212
+ if count < 0
213
+ raise "invalid count less than zero: #{count}"
214
+ elsif count > @total
215
+ if @total_overflow
216
+ @total = count + 1
217
+ else
218
+ raise "invalid count greater than total: #{count}"
219
+ end
220
+ end
221
+ @current = count
222
+ show_progress
223
+ @previous = @current
224
+ end
225
+
226
+ def inc (step = 1)
227
+ @current += step
228
+ @current = @total if @current > @total
229
+ show_progress
230
+ @previous = @current
231
+ end
232
+
233
+ def inspect
234
+ "(ProgressBar: #{@current}/#{@total})"
235
+ end
236
+
237
+ end