show_code 0.1.4 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4dc7933daf56bb40881fccf3193259dbea82a12
4
- data.tar.gz: 3d0bf6b6681fbe6b2136579425287a7e07e8743c
3
+ metadata.gz: 216188dab49abd9b3c09fcec1b710e151bfdfc42
4
+ data.tar.gz: b5a13deb1dcd082b445544a5ce3541e13abb4f06
5
5
  SHA512:
6
- metadata.gz: e372241a9e9d6e1a360389998b3f9af656e4f5145711bfed78f4fa07a1bf1528c5fde4ca8e6822e54508c2671432d38e076f2e19a7f1ed057bacf4310eb8c336
7
- data.tar.gz: b63de7c35538f346b82520ccdef2f6b0dc980e50ff2db126e98f0e3164df2f454d9647ebc99efd34a6d2ada556737814e49fd0dadb76a46d21badfd3f35ab155
6
+ metadata.gz: 68853a23d8e89bec95e8606521b81283de9d0238a663bfb972bd1564f1c159c39ed78b2a08a3f7d9218037ecc29d3c8c65e35b5f7a470d27117bfd2cf8e6f809
7
+ data.tar.gz: 2c8da73e0559bd8309f967e1c9b0706a2665a914f46761b4bcd966163275aee4be1b94596c9fcba1ab44de2102e5f25d6bbff5381de288099a66ed38e67cec17
data/README.md CHANGED
@@ -2,8 +2,11 @@
2
2
 
3
3
  show_code provides a quick way to show ruby method source codes in terminal.
4
4
 
5
+ ![show_code_example](https://user-images.githubusercontent.com/10070670/27630537-617eb7a0-5c28-11e7-90b8-e76b147dec2e.png)
6
+
5
7
  __NOTE__: show_code current version *require* Ruby v1.9.0 or later.
6
8
 
9
+
7
10
  ### Installation ###
8
11
  # Installing as Ruby gem
9
12
  $ gem install show_code
@@ -12,7 +15,7 @@ __NOTE__: show_code current version *require* Ruby v1.9.0 or later.
12
15
  $ gem show_code
13
16
 
14
17
  ### Usage ###
15
- You can use show_code in `console c` or `irb`
18
+ You will be able use show_code in `rails c` or `irb`.
16
19
 
17
20
  ```ruby
18
21
  require "show_code" # just when use irb
@@ -45,7 +48,6 @@ ShowCode.open 'ShowCode::Code.new.greet'
45
48
  ```
46
49
 
47
50
  ### TODO ###
48
- - Colorize output
49
51
  - Add more statistic analysis in output
50
52
 
51
53
  ### License ###
@@ -1,13 +1,15 @@
1
1
  module ShowCode
2
2
  class Code
3
+ require 'coderay'
3
4
 
4
- attr :content, :owner, :file, :lines
5
+ attr :content, :owner, :file, :line, :lines
5
6
 
6
7
  def initialize(location)
7
8
  @file = location.file
9
+ @line = location.line
8
10
  file_lines = File.readlines(@file) if @file
9
11
 
10
- related_lines = file_lines[(location.line - 1)..-1] || []
12
+ related_lines = file_lines[(@line - 1)..-1] || []
11
13
  codes = extract_method_code related_lines
12
14
  @lines = codes.count
13
15
  @content = codes.join
@@ -15,8 +17,8 @@ module ShowCode
15
17
 
16
18
  def extract_method_code(related_lines)
17
19
  codes = []
18
- related_lines.each do |value|
19
- codes << value
20
+ related_lines.each do |loc|
21
+ codes << loc
20
22
  return codes if expression_end?(codes)
21
23
  end
22
24
  raise SyntaxError, "unexpected $end"
@@ -36,6 +38,21 @@ module ShowCode
36
38
  false
37
39
  end
38
40
 
41
+ def highlighted(opts = {})
42
+ options = {:line_numbers => true}.merge(opts)
43
+ colorized = CodeRay.scan(content, :ruby).tty
44
+ output = options[:line_numbers] ? add_line_numbers(colorized) : colorized
45
+ "\n%s\n\n" % output
46
+ end
47
+
48
+ def add_line_numbers(output)
49
+ line_number = line - 1
50
+ output.split("\n").map do |loc|
51
+ line_number += 1
52
+ "\e[33m%i\e[0m #{loc}" % line_number
53
+ end.join("\n")
54
+ end
55
+
39
56
  # Example: ShowCode 'ShowCode::Code.new.greet'
40
57
  def greet
41
58
  puts 'Hello ShowCode!'
@@ -6,7 +6,7 @@ module ShowCode
6
6
 
7
7
  def initialize(target)
8
8
  if target.is_a?(String)
9
- arr = target.split('.').map{|t| t == 'new' ? 'allocate' : t}
9
+ arr = target.gsub('.new.', '.allocate.').split('.')
10
10
  klass = arr[0..-2].join('.')
11
11
  method = arr[-1]
12
12
 
@@ -1,3 +1,3 @@
1
1
  module ShowCode
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/show_code.rb CHANGED
@@ -5,16 +5,17 @@ require_relative 'show_code/code'
5
5
 
6
6
  module ShowCode
7
7
 
8
- def self.parse(obj)
8
+ def self.parse(obj, opts = {})
9
9
  sol = SourceLocation.new obj
10
10
  code = Code.new sol
11
- puts code.content
11
+ puts code.highlighted opts
12
12
  sol.method
13
13
  end
14
14
 
15
15
  def self.open(obj)
16
16
  sol = SourceLocation.new obj
17
17
  file = sol.file
18
+
18
19
  %x[gedit #{file}]
19
20
  if $?.success?
20
21
  'File has opened via gedit!'
@@ -28,8 +29,8 @@ end
28
29
  # ShowCode() runs like ShowCode.parse()
29
30
  module Kernel
30
31
 
31
- def ShowCode(target)
32
- ShowCode.parse target
32
+ def ShowCode(target, opts = {})
33
+ ShowCode.parse target, opts
33
34
  end
34
35
  module_function :ShowCode
35
36
 
data/show_code.gemspec CHANGED
@@ -6,6 +6,9 @@ require 'show_code/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "show_code"
8
8
  spec.version = ShowCode::VERSION
9
+
10
+ spec.required_ruby_version = '>= 1.9.0'
11
+
9
12
  spec.authors = ["cenxky"]
10
13
  spec.email = ["cenxky@gmail.com"]
11
14
 
@@ -14,18 +17,14 @@ Gem::Specification.new do |spec|
14
17
  spec.homepage = "https://github.com/cenxky/show_code"
15
18
  spec.license = "MIT"
16
19
 
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
-
20
20
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
21
  f.match(%r{^(test|spec|features)/})
22
22
  end
23
23
  spec.bindir = "exe"
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ["lib"]
26
- spec.required_ruby_version = '>= 1.9.0'
27
26
 
27
+ spec.add_dependency 'coderay', '~> 1.1.1'
28
28
  spec.add_development_dependency "bundler", "~> 1.14"
29
29
  spec.add_development_dependency "rake", "~> 10.0"
30
- spec.add_development_dependency "rspec", "~> 3.0"
31
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: show_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - cenxky
@@ -11,47 +11,47 @@ cert_chain: []
11
11
  date: 2017-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: coderay
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.14'
20
- type: :development
19
+ version: 1.1.1
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.14'
26
+ version: 1.1.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '1.14'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '1.14'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '10.0'
55
55
  description: show_code provides a quick way to show ruby method source codes in terminal.
56
56
  email:
57
57
  - cenxky@gmail.com