bcat 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'bcat'
3
- s.version = '0.0.1'
4
- s.date = '2010-06-19'
3
+ s.version = '0.2.0'
4
+ s.date = '2010-06-20'
5
5
 
6
6
  s.summary = "browser cat"
7
7
  s.description =
@@ -17,9 +17,14 @@ Gem::Specification.new do |s|
17
17
  Rakefile
18
18
  bcat.gemspec
19
19
  bin/bcat
20
+ bin/bcat-encodehtml
21
+ bin/btee
20
22
  lib/bcat.rb
23
+ lib/bcat/browser.rb
21
24
  lib/bcat/kidgloves.rb
25
+ lib/bcat/reader.rb
22
26
  man/bcat.1.ronn
27
+ man/btee.1.ronn
23
28
  ]
24
29
  # = MANIFEST =
25
30
 
data/bin/bcat CHANGED
@@ -1,53 +1,63 @@
1
1
  #!/usr/bin/env ruby
2
- #/ Usage: bcat [-h] [<file>]...
3
- #/ Read standard input, or one or more <file>s, and write to browser.
2
+ #/ Usage: bcat [-h] [-t <title>] [<file>]...
3
+ #/ btee <options> [<file>]...
4
+ #/ Read standard input, or one or more <file>s, and write to browser. When
5
+ #/ invoked as btee, also write input to standard output.
4
6
  #/
5
7
  #/ Options
6
8
  #/ -h, --html input is html encoded
7
9
  #/ -t, --title=<text> use <text> as the page title
10
+ #/ -d, --debug enable verbose debug logging on stderr
8
11
  require 'optparse'
9
- require 'bcat'
10
12
 
11
13
  options = {
12
14
  :html => false,
13
15
  :title => Dir.pwd,
14
- :Host => 'localhost',
15
- :Port => 8091
16
+ :Host => '127.0.0.1',
17
+ :Port => 8091,
18
+ :debug => false
16
19
  }
17
20
 
21
+ (class <<self;self;end).send(:define_method, :notice) { |message|
22
+ warn "#{File.basename($0)}: #{message}" if options[:debug] }
23
+
18
24
  ARGV.options do |argv|
19
25
  argv.on('-h', '--html') { options[:html] = true }
26
+ argv.on('-a', '--app=v') { |app| ENV['BCAT_APPLICATION'] = app }
20
27
  argv.on('-t', '--title=v') { |text| options[:title] = text }
28
+ argv.on('-d', '--debug') { options[:debug] = true }
21
29
  argv.on_tail('--help') { exec "grep ^#/ <#{__FILE__} | cut -c4-" }
22
30
  argv.parse!
23
31
  end
32
+ ARGV.push '-' if ARGV.empty?
24
33
 
25
- fds =
26
- ARGV.map do |file|
27
- if file == '-'
28
- $stdin
29
- else
30
- File.open(file, 'rb')
31
- end
32
- end
33
- fds = [$stdin] if fds.empty?
34
+ require 'bcat'
35
+ notice "loaded bcat v#{Bcat::VERSION}"
36
+
37
+ include Bcat::Browser
38
+ notice "env BCAT_APPLICATION=#{ENV['BCAT_APPLICATION'].inspect}"
39
+ notice "env BCAT_COMMAND=#{browser_command.inspect}"
34
40
 
35
- command = ENV['BCAT_COMMAND'] || 'open $BCAT_ARGS "$BCAT_URL"'
41
+ reader =
42
+ if File.basename($0) =~ /tee$/
43
+ Bcat::TeeReader.new(ARGV, $stdout)
44
+ else
45
+ Bcat::Reader.new(ARGV)
46
+ end
36
47
 
48
+ notice "starting server"
37
49
  pid = nil
38
50
  begin
39
- bcat = Bcat.new(fds, options)
51
+ bcat = Bcat.new(reader, options)
40
52
  bcat.serve! do |sock|
41
- pid =
42
- fork do
43
- (fds + [sock, $stdin, $stdout]).uniq.each { |fd| fd.close }
44
- ENV['BCAT_URL'] = "http://#{bcat[:Host]}:#{bcat[:Port]}/#{File.basename(Dir.pwd)}"
45
- ENV['BCAT_ARGS'] = "-a '#{ENV['BCAT_APPLICATION']}'" if !ENV['BCAT_APPLICATION'].to_s.empty?
46
- exec "/bin/sh -c \"#{command}\""
47
- end
53
+ url = "http://#{bcat[:Host]}:#{bcat[:Port]}/#{File.basename(Dir.pwd)}"
54
+ pid = browser(url)
48
55
  end
49
56
  rescue Interrupt
57
+ notice "interrupt"
50
58
  end
51
59
 
52
60
  Process.wait(pid) if pid
53
- exit $?
61
+ status = $?
62
+ notice "open exited with #{status}"
63
+ exit status
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # Usage: bcat-encodehtml
3
+ # Read text from stdin and write HTML encoded result to stdout.
4
+
5
+ while char = $stdin.getc
6
+ case char
7
+ when ?<
8
+ $stdout.write('&lt;')
9
+ when ?&
10
+ $stdout.write('&amp;')
11
+ else
12
+ $stdout.putc(char)
13
+ end
14
+ end
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ load File.expand_path('../bcat', __FILE__)
@@ -1,12 +1,14 @@
1
1
  require 'rack'
2
+ require 'bcat/reader'
2
3
  require 'bcat/kidgloves'
4
+ require 'bcat/browser'
3
5
 
4
6
  class Bcat
5
- VERSION = '0.0.1'
7
+ VERSION = '0.2.0'
6
8
  include Rack::Utils
7
9
 
8
- def initialize(fds=[$stdin], config={})
9
- @fds = fds
10
+ def initialize(reader, config={})
11
+ @reader = reader
10
12
  @config = {:Host => '127.0.0.1', :Port => 8091}.merge(config)
11
13
  end
12
14
 
@@ -15,9 +17,29 @@ class Bcat
15
17
  end
16
18
 
17
19
  def call(env)
20
+ notice "#{env['REQUEST_METHOD']} #{env['PATH_INFO'].inspect}"
18
21
  [200, {"Content-Type" => "text/html;charset=utf-8"}, self]
19
22
  end
20
23
 
24
+ def each
25
+ yield "\n" * 1000
26
+ yield "<!DOCTYPE html>\n"
27
+ yield head
28
+ yield "<pre>" if !self[:html]
29
+
30
+ @reader.each do |buf|
31
+ if !self[:html]
32
+ buf = escape_html(buf)
33
+ buf.gsub!(/\n/, "<br>")
34
+ end
35
+ buf = escape_js(buf)
36
+ yield "<script>document.write('#{buf}');</script>"
37
+ end
38
+
39
+ yield "</pre>" if !self[:html]
40
+ yield foot
41
+ end
42
+
21
43
  def head
22
44
  ["<html>",
23
45
  "<head><title>#{self[:title] || 'bcat'}</title></head>",
@@ -34,35 +56,8 @@ class Bcat
34
56
  string
35
57
  end
36
58
 
37
- def each
38
- yield "\n" * 1000
39
- yield "<!DOCTYPE html>\n"
40
- yield head
41
- yield "<pre>" if !self[:html]
42
-
43
- begin
44
- @fds.each do |fd|
45
- begin
46
- while buf = fd.readpartial(4096)
47
- if !self[:html]
48
- buf = escape_html(buf)
49
- buf.gsub!(/\n/, "<br>")
50
- end
51
- buf = escape_js(buf)
52
- yield "<script>document.write('#{buf}');</script>"
53
- end
54
- rescue EOFError
55
- ensure
56
- fd.close rescue nil
57
- end
58
- end
59
- end
60
-
61
- yield "</pre>" if !self[:html]
62
- yield foot
63
- end
64
-
65
59
  def close
60
+ notice "closing with interrupt"
66
61
  raise Interrupt
67
62
  end
68
63
 
@@ -77,4 +72,9 @@ class Bcat
77
72
  def serve!(&bk)
78
73
  Rack::Handler::KidGloves.run to_app, @config, &bk
79
74
  end
75
+
76
+ def notice(message)
77
+ return if !@config[:debug]
78
+ warn "#{File.basename($0)}: #{message}"
79
+ end
80
80
  end
@@ -0,0 +1,24 @@
1
+ class Bcat
2
+ module Browser
3
+ def browser(url, application=ENV['BCAT_APPLICATION'])
4
+ command = browser_command
5
+ fork do
6
+ [$stdin, $stdout].each { |fd| fd.close }
7
+ ENV['BCAT_ARGS'] = "-a '#{application}'" if !application.to_s.empty?
8
+ ENV['BCAT_URL'] = url
9
+ ENV['BCAT_COMMAND'] = command
10
+ exec "/bin/sh -c \"#{command.gsub(/"/, '\"')}\""
11
+ end
12
+ end
13
+
14
+ def browser_command
15
+ return ENV['BCAT_COMMAND'] if !ENV['BCAT_COMMAND'].to_s.empty?
16
+
17
+ case `uname`
18
+ when /Darwin/ ; 'open $BCAT_ARGS "$BCAT_URL"'
19
+ when /Linux/, /BSD/ ; 'xdg-open $BCAT_ARGS "$BCAT_URL"'
20
+ else ; 'xdg-open "$BCAT_URL"'
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ class Bcat
2
+ # ARGF style multi-file streaming interface. Input is read with IO#readpartial
3
+ # to avoid buffering.
4
+ class Reader
5
+ attr_reader :files
6
+ attr_reader :fds
7
+
8
+ def initialize(files=[])
9
+ @files = files
10
+ @fds =
11
+ files.map do |f|
12
+ if f == '-'
13
+ $stdin
14
+ else
15
+ File.open(f, 'rb')
16
+ end
17
+ end
18
+ end
19
+
20
+ def each
21
+ fds.each do |fd|
22
+ fd.sync = true
23
+ begin
24
+ while buf = fd.readpartial(4096)
25
+ yield buf
26
+ end
27
+ rescue EOFError
28
+ ensure
29
+ fd.close
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ # Like Reader but writes all input to an output IO object in addition to
36
+ # yielding to the block.
37
+ class TeeReader < Reader
38
+ def initialize(files=[], out=$stdout)
39
+ @out = out
40
+ super(files)
41
+ end
42
+
43
+ def each
44
+ super() do |chunk|
45
+ @out.write chunk
46
+ yield chunk
47
+ end
48
+ end
49
+ end
50
+ end
@@ -3,13 +3,23 @@ bcat(1) -- browser cat
3
3
 
4
4
  ## SYNOPSIS
5
5
 
6
- `bcat` [-ht] [<file>...]<br>
6
+ `bcat` [-htad] [<file>...]<br>
7
+ `btee` [-htad] [<file>...]
7
8
 
8
9
  ## DESCRIPTION
9
10
 
10
- The `bcat` utility reads from standard input, or one or more <file>s, and writes
11
- output to a web browser progressively. A <file> may be '-', in which case
12
- standard input is concatenated at the position specified.
11
+ The `bcat` utility reads from standard input, or one or more <file>s, and pipes
12
+ output into a web browser. <file> may be '-', in which case standard input is
13
+ concatenated at that position.
14
+
15
+ When invoked as `btee`, all input is written immediately to standard output in
16
+ addition to being piped into the browser.
17
+
18
+ ## OPTIONS
19
+
20
+ * `-t`, `--title`=<text>:
21
+ Use <text> as the page `<title>`. By default, the path to the current working
22
+ directory is used as the title.
13
23
 
14
24
  * `-h`, `--html`:
15
25
  Do not HTML encode input. By default, `bcat` assumes input is plain text
@@ -17,9 +27,14 @@ standard input is concatenated at the position specified.
17
27
  characters (`\n`) to `<br>` tags, and wraps output in a `<pre>` block. The
18
28
  `--html` option disables all of these conversions, causing the input text
19
29
  to be written directly as HTML.
20
- * `-t`, `--title`=<text>:
21
- Use <text> as the page `<title>`. By default, the path to the current working
22
- directory is used as the title.
30
+
31
+ * `-a`, `--app`=`Safari`|`Google Chrome`|`Firefox`:
32
+ MacOS only. The name of the browser application. This can also be set using
33
+ the `BCAT_APPLICATION` environment variable.
34
+
35
+ * `-d`, `--debug`:
36
+ Turn on verbose debug logging to standard error. Include this output when
37
+ reporting defects if applicable.
23
38
 
24
39
  ## ENVIRONMENT
25
40
 
@@ -27,11 +42,11 @@ standard input is concatenated at the position specified.
27
42
  * `BCAT_COMMAND`=`xdg-open "$url"`:
28
43
  The command used to launch to the browser application.
29
44
 
30
- * `BCAT_APPLICATION`=[`Safari | Google Chrome | Firefox`]:
31
- MacOS X only. The name of the Application . This is given as the value of
32
- the `-a` argument to open(1). When `BCAT_APPLICATION` is undefined, the
33
- system default browser is used.
45
+ * `BCAT_APPLICATION`=[`Safari` | `Google Chrome` | `Firefox`]:
46
+ MacOS X only. The name of the browser application . This is given as the
47
+ value of the `-a` argument to open(1). When `BCAT_APPLICATION` is undefined,
48
+ the system default browser is used.
34
49
 
35
50
  ## SEE ALSO
36
51
 
37
- cat(1), open(1)
52
+ cat(1), tee(1), open(1)
@@ -0,0 +1,52 @@
1
+ bcat(1) -- browser cat
2
+ ======================
3
+
4
+ ## SYNOPSIS
5
+
6
+ `bcat` [-htad] [<file>...]<br>
7
+ `btee` [-htad] [<file>...]
8
+
9
+ ## DESCRIPTION
10
+
11
+ The `bcat` utility reads from standard input, or one or more <file>s, and pipes
12
+ output into a web browser. <file> may be '-', in which case standard input is
13
+ concatenated at that position.
14
+
15
+ When invoked as `btee`, all input is written immediately to standard output in
16
+ addition to being piped into the browser.
17
+
18
+ ## OPTIONS
19
+
20
+ * `-t`, `--title`=<text>:
21
+ Use <text> as the page `<title>`. By default, the path to the current working
22
+ directory is used as the title.
23
+
24
+ * `-h`, `--html`:
25
+ Do not HTML encode input. By default, `bcat` assumes input is plain text
26
+ and entity encodes any `<` or `&` characters, converts raw end-of-line
27
+ characters (`\n`) to `<br>` tags, and wraps output in a `<pre>` block. The
28
+ `--html` option disables all of these conversions, causing the input text
29
+ to be written directly as HTML.
30
+
31
+ * `-a`, `--app`=`Safari`|`Google Chrome`|`Firefox`:
32
+ MacOS only. The name of the browser application. This can also be set using
33
+ the `BCAT_APPLICATION` environment variable.
34
+
35
+ * `-d`, `--debug`:
36
+ Turn on verbose debug logging to standard error. Include this output when
37
+ reporting defects if applicable.
38
+
39
+ ## ENVIRONMENT
40
+
41
+ * `BCAT_COMMAND`=`open -a $BCAT_APPLICATION $url`:
42
+ * `BCAT_COMMAND`=`xdg-open "$url"`:
43
+ The command used to launch to the browser application.
44
+
45
+ * `BCAT_APPLICATION`=[`Safari` | `Google Chrome` | `Firefox`]:
46
+ MacOS X only. The name of the browser application . This is given as the
47
+ value of the `-a` argument to open(1). When `BCAT_APPLICATION` is undefined,
48
+ the system default browser is used.
49
+
50
+ ## SEE ALSO
51
+
52
+ cat(1), tee(1), open(1)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bcat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 2
8
9
  - 0
9
- - 1
10
- version: 0.0.1
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Tomayko
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-19 00:00:00 -07:00
18
+ date: 2010-06-20 00:00:00 -07:00
19
19
  default_executable: bcat
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -45,9 +45,14 @@ files:
45
45
  - Rakefile
46
46
  - bcat.gemspec
47
47
  - bin/bcat
48
+ - bin/bcat-encodehtml
49
+ - bin/btee
48
50
  - lib/bcat.rb
51
+ - lib/bcat/browser.rb
49
52
  - lib/bcat/kidgloves.rb
53
+ - lib/bcat/reader.rb
50
54
  - man/bcat.1.ronn
55
+ - man/btee.1.ronn
51
56
  - COPYING
52
57
  has_rdoc: true
53
58
  homepage: http://github.com/rtomayko/bcat/