act 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 684ed5ccb021e0950d357fa0157cdb631d1852ad
4
- data.tar.gz: d1fc364445937bb80789188bb88b6119af63da22
3
+ metadata.gz: 2faf8a1d513d94638d664661519f8b40df95c6ca
4
+ data.tar.gz: 7e4c4df4a3971d1b64c138793df53526adfcbb4e
5
5
  SHA512:
6
- metadata.gz: 2077bc99056dcbbc185fa82aee2eb87f5bcd744fd9fbe0e5de4ba4653f36bd85b9fca8d92ad471b23ee6c722823e5d0595a045724269fb5de2a122895f938be9
7
- data.tar.gz: 86b85a404e763e410fb3a3a8c333b150a008fc1b7e822264d18e80c91dca2b885986ee3c715ac5a1d71f423f59547ad3164dd5c7a125c35c28bb1403167634c5
6
+ metadata.gz: 5d9ab8baa30dd2bc07755fab8a4a456c866a2b32b1cf4ed9a8f233500475348c44af6c9919cf40f30d381d4b644969b16982ad472296a68b1a1e401191ef7271
7
+ data.tar.gz: 2cb5dbe4bd494e7a037e294e90e7e79ae7435d830d71bfa4b90ad6b6f320f93e5fe72b7d1ee2ae650f21b6b5706275e6ebe4bf29a4a262bb84d3f082480f48eb
data/Gemfile CHANGED
@@ -3,13 +3,13 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :development do
6
- gem "bacon"
7
- gem "mocha-on-bacon"
6
+ gem 'bacon'
7
+ gem 'mocha-on-bacon'
8
8
  gem 'prettybacon'
9
9
  gem 'rubocop'
10
10
  end
11
11
 
12
12
  group :debugging do
13
- gem "kicker"
13
+ gem 'kicker'
14
14
  end
15
15
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Act
2
2
  [![Build Status](https://travis-ci.org/irrationalfab/act.svg?branch=master)](https://travis-ci.org/irrationalfab/act)
3
3
 
4
- Allows to act on files from the command line efficiently... `cat` for the twenty first century!
4
+ Preview files from the command line efficiently. `cat` for the twenty first century!
5
5
 
6
6
  <img src="http://cl.ly/image/0A2p320r442D/Image%202014-04-04%20at%202.59.18%20pm.png" height="50%" width="50%">
7
7
 
@@ -11,7 +11,7 @@ Allows to act on files from the command line efficiently... `cat` for the twenty
11
11
  - Support for colon syntax
12
12
  - Support for GitHub like URLs
13
13
  - Automatic syntax highlighting based on the file extension (via [Pygments](http://pygments.org))
14
- - Fast enoguht for the task
14
+ - Fast enough for the task
15
15
 
16
16
  ## Installation
17
17
 
@@ -5,58 +5,81 @@ require 'active_support/core_ext/string/strip'
5
5
  module Act
6
6
  class Command < CLAide::Command
7
7
  self.command = 'act'
8
- self.description = 'Act the command line tool to act on files'
8
+
9
+ self.description = <<-DOC
10
+ Act the command line tool to act on files
11
+
12
+ Prints the contents of the file at `PATH`.
13
+
14
+ DOC
15
+
16
+ def self.arguments
17
+ [
18
+ ['PATH', :optional],
19
+ ]
20
+ end
9
21
 
10
22
  def self.options
11
23
  [
12
24
  ['--open', 'Open the file in $EDITOR instead of printing it'],
13
- ['--no-line-numbers', 'Show output without line numbers'],
14
- ['--version', 'Show the version of Act'],
25
+ ['--prettify', "Don't prettify output"],
26
+ ['--line-numbers', 'Show output without line numbers'],
27
+ ['--lexer=NAME', 'Use the given lexer'],
15
28
  ].concat(super)
16
29
  end
17
30
 
18
- def self.run(argv)
19
- argv = CLAide::ARGV.new(argv)
20
- if argv.flag?('version')
21
- UI.puts VERSION
22
- exit 0
23
- end
24
- super(argv)
31
+ def self.completion_description
32
+ description = super
33
+ # _path_files function
34
+ description[:paths] = :all_files
35
+ description
25
36
  end
26
37
 
27
38
  def initialize(argv)
39
+ @stdin = STDIN.read unless STDIN.tty?
28
40
  @open = argv.flag?('open')
29
- @number_lines = argv.flag?('line-numbers', true)
41
+ @prettify = argv.flag?('prettify', false)
42
+ @number_lines = argv.flag?('line-numbers', false)
43
+ @lexer = argv.option('lexer', false)
30
44
  @file_string = argv.shift_argument
31
45
  super
32
46
  end
33
47
 
34
48
  def validate!
35
49
  super
36
- help! 'A file is required.' unless @file_string
50
+ help! 'A file is required.' unless @file_string || @open || @stdin
37
51
  end
38
52
 
39
53
  CONTEXT_LINES = 5
40
54
 
41
55
  def run
56
+ if @stdin && !@open
57
+ cat_string(@stdin)
58
+ return
59
+ end
60
+
61
+
62
+ @file_string ||= '.'
42
63
  clean_file_string = pre_process_file_string(@file_string)
43
64
  file = ArgumentParser.parse_file_information(clean_file_string, CONTEXT_LINES)
44
65
 
45
66
  path_exists = File.exist?(file.path)
46
67
  unless path_exists
47
68
  inferred = infer_local_path(file.path)
48
- file.path = inferred
49
- path_exists = true if inferred
69
+ if inferred
70
+ file.path = inferred
71
+ path_exists = true
72
+ end
50
73
  end
51
74
 
52
- if path_exists
53
- if @open
54
- open_file(file)
55
- else
75
+ if @open
76
+ open_file(file)
77
+ else
78
+ if path_exists
56
79
  cat_file(file)
80
+ else
81
+ UI.warn '[!] File not found'
57
82
  end
58
- else
59
- UI.warn '[!] File not found'
60
83
  end
61
84
  end
62
85
 
@@ -85,7 +108,13 @@ module Act
85
108
  line = file.highlight_line || file.from_line
86
109
  command = Helper.open_in_editor_command(file.path, line)
87
110
  UI.puts command if self.verbose?
88
- system(command)
111
+ if defined? Bundler
112
+ Bundler.with_clean_env do
113
+ system(command)
114
+ end
115
+ else
116
+ system(command)
117
+ end
89
118
  end
90
119
 
91
120
  # @return [void]
@@ -95,11 +124,17 @@ module Act
95
124
  if file.from_line && file.to_line
96
125
  string = Helper.select_lines(string, file.from_line, file.to_line)
97
126
  end
127
+ cat_string(string, file)
128
+ end
98
129
 
130
+ def cat_string(string, file = nil)
99
131
  if string
132
+ path = file.path if file
133
+ @lexer ||= Helper.lexer(path)
100
134
  string = Helper.strip_indentation(string)
101
- string = Helper.syntax_highlith(string, file.path) if self.ansi_output?
102
- string = Helper.add_line_numbers(string, file.from_line, file.highlight_line) if @number_lines
135
+ string = Helper.prettify(string, @lexer) if @prettify
136
+ string = Helper.syntax_highlith(string, @lexer) if self.ansi_output?
137
+ string = Helper.add_line_numbers(string, file.from_line, file.highlight_line) if @number_lines && file
103
138
  UI.puts "\n#{string}"
104
139
  else
105
140
  UI.warn '[!] Nothing to show'
@@ -50,7 +50,7 @@ module Act
50
50
  # @return [String]
51
51
  #
52
52
  def self.add_line_numbers(string, start_line, highlight_line = nil)
53
- start_line ||= 0
53
+ start_line ||= 1
54
54
  line_count = start_line
55
55
  numbered_lines = string.lines.map do |line|
56
56
  number = line_count.to_s.ljust(3)
@@ -63,12 +63,40 @@ module Act
63
63
  numbered_lines.join
64
64
  end
65
65
 
66
+ def self.lexer(file_name)
67
+ case file_name
68
+ when 'Gemfile', 'Rakefile', 'Podfile'
69
+ 'rb'
70
+ when 'Podfile.lock', 'Gemfile.lock'
71
+ 'yaml'
72
+ else
73
+ if file_name
74
+ `pygmentize -N #{file_name}`.chomp
75
+ else
76
+ 'text'
77
+ end
78
+ end
79
+ end
80
+
81
+ def self.prettify(string, lexer)
82
+ raise ArgumentError unless string
83
+ raise ArgumentError unless lexer
84
+ case lexer
85
+ when 'json'
86
+ require 'json'
87
+ JSON.pretty_generate(JSON.parse(string))
88
+ else
89
+ string
90
+ end
91
+ end
92
+
66
93
  # @return [String]
67
94
  #
68
- def self.syntax_highlith(string, file_name)
69
- return string if `which gen_bridge_metadata`.strip.empty?
95
+ def self.syntax_highlith(string, lexer)
96
+ raise ArgumentError unless string
97
+ raise ArgumentError unless lexer
98
+ return string if `which pygmentize`.strip.empty?
70
99
  result = nil
71
- lexer = `pygmentize -N #{file_name}`.chomp
72
100
  Open3.popen3("pygmentize -l #{lexer}") do |stdin, stdout, stderr|
73
101
  stdin.write(string)
74
102
  stdin.close_write
@@ -76,5 +104,6 @@ module Act
76
104
  end
77
105
  result
78
106
  end
107
+
79
108
  end
80
109
  end
@@ -1,3 +1,3 @@
1
1
  module Act
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -51,5 +51,21 @@ module Act
51
51
 
52
52
  #-------------------------------------------------------------------------#
53
53
 
54
+ describe 'lexer' do
55
+ it 'infers the lexer according to the extension using pygmentize' do
56
+ file_name = 'test.rb'
57
+ result = @subject.lexer(file_name)
58
+ result.should == 'rb'
59
+ end
60
+
61
+ it 'infers the lexer according to the file name' do
62
+ file_name = 'Gemfile'
63
+ result = @subject.lexer(file_name)
64
+ result.should == 'rb'
65
+ end
66
+ end
67
+
68
+ #-------------------------------------------------------------------------#
69
+
54
70
  end
55
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: act
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Pelosin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-04 00:00:00.000000000 Z
11
+ date: 2014-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide