kdwatch 0.4.0 → 0.5.1

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
  SHA256:
3
- metadata.gz: 77f59a0c0a41535d090f2b858a44759a830b7cddc4e760a39c75613f06d2b37f
4
- data.tar.gz: 6c53346765beb3f0391991db58bda4f00debfed880b8dca8763462567bbcd435
3
+ metadata.gz: ec097b76b694691e1a066ec87c23ebe004f085940b5ce222e1ec8b383f7cbbd8
4
+ data.tar.gz: 3f3b1dff065d860cc01a8a7e6598013faa679aeb9c0206dffd17a0dd85b9a6c8
5
5
  SHA512:
6
- metadata.gz: d9c2df05241e752a0d7d80e34cf8324872110a0aa4bc800bb9ef7ec1afe0ad2034b06691b56d84c14a6462c252606ef371cad1f85f021ac424046c09dcdc5182
7
- data.tar.gz: 001ca06bd402115c0d63e8cd06e3558a99787f12ba97b9641c158ec8f9f8d87f99cdea3850a5121b28f53336d14c1312c13ec037ace624b97c19effba70fdea8
6
+ metadata.gz: f105edcc85863d728f629c9b49fae44343b67e7f98af8943a1f22202b0f7ff4f88ad347a9a69b77672539dad159ab2ba0c51f08877a1a18e429794c365b764b7
7
+ data.tar.gz: 17801699b473113c2b66b8aedf3ac5c15ee124d05a460a25b9023409130aed0c703dac48c831404f410eb53d9be148e64d4ca8566d52e2ddad716d8e6bc06e79
data/README.md CHANGED
@@ -51,9 +51,11 @@ There can only be one kdwatch active on each host and port. You will
51
51
  need to specify a different port (or host!) to run more than one
52
52
  kdwatch at the same time.
53
53
 
54
- * `-p port` to select a port number (default: 7991)
55
- * `-1` to `6` as a shortcut for `-p 7991` (default) to `-p 7996` (must be last
54
+ * `-p port` to select a specific port number
55
+ * `-1` to `6` as a shortcut for `-p 7991` to `-p 7996` (must be last
56
56
  option because of an idiosyncrasy of the optionparser library)
57
+ * if no port number is given, `kdwatch` will hunt for a port number
58
+ that doesn't have a listener, starting with 7991.
57
59
 
58
60
  kdwatch has two flags to simplify handling servers that might be
59
61
  accumulating on one host/port:
@@ -64,18 +66,19 @@ accumulating on one host/port:
64
66
  So the most likely use is going to be:
65
67
 
66
68
  ```
67
- kdwatch -r
69
+ kdwatch
68
70
  ```
69
71
 
70
- or maybe
72
+ to start a new server on a new port number.
73
+
74
+ These tend to accumulate a bit, so sometimes you'll want to replace an
75
+ existing one (here: port number 7992):
71
76
 
72
77
  ```
73
78
  kdwatch -r2
74
79
  ```
75
80
 
76
- for the second draft you are editing at the same time,
77
-
78
- or, if your drafts are weirdly named or you need to select one out of
81
+ If your drafts are weirdly named or you need to select one out of
79
82
  many
80
83
 
81
84
  ```
data/bin/kds ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ require 'socket'
3
+ require 'uri'
4
+ require 'open-uri'
5
+ require 'optparse'
6
+
7
+ KDWATCH_VERSION=Gem.loaded_specs["kdwatch"].version rescue "unknown-version"
8
+
9
+ loport = 7991
10
+ hiport = 7999
11
+ match = nil
12
+
13
+ op = OptionParser.new do |opts|
14
+ opts.banner = <<BANNER
15
+ Usage: kds [options] [match...]
16
+ Version: #{KDWATCH_VERSION} (from kdwatch)
17
+ BANNER
18
+ opts.on("-v", "--version", "Show version and exit") do |v|
19
+ puts "kds, from kdwatch #{KDWATCH_VERSION}"
20
+ exit
21
+ end
22
+ opts.on("-h", "--help", "Show option summary and exit") do |v|
23
+ puts opts
24
+ exit
25
+ end
26
+ opts.on("-fNUM", "--from=NUM", Integer, "Search from port number") do |v|
27
+ loport = v
28
+ end
29
+ opts.on("-tNUM", "--to=NUM", Integer, "Search to port number") do |v|
30
+ hiport = v
31
+ end
32
+ end
33
+ op.parse!
34
+
35
+ if ARGV.size != 0
36
+ match = ARGV.join(" ")
37
+ end
38
+
39
+ (loport..hiport).each do |p|
40
+ url = "http://127.0.0.1:#{p}"
41
+ URI(url).open do |f|
42
+ s = f.readpartial(40000)
43
+ sc = s.scan(/id="title".*?<|Internal server error/)
44
+ sc.each do |scl|
45
+ scl.sub!(/id="title">/, "")
46
+ scl.chomp!('<')
47
+ puts "#{url} #{scl}"
48
+ if match && scl.downcase.include?(match.downcase)
49
+ spawn("open #{url} || xdg-open #{url} || echo @@@ Please open #{url}")
50
+ end
51
+ break
52
+ end
53
+ end rescue nil
54
+ end
data/bin/kdwatch CHANGED
@@ -6,11 +6,24 @@ ENV["KRAMDOWN_PERSISTENT"]="yes"
6
6
 
7
7
  require 'optparse'
8
8
  require 'ostruct'
9
+ require 'socket'
9
10
 
10
11
  require_relative "../lib/kdwatch/version"
11
12
 
12
13
  KDWATCH_PORT = 7991 # currently unregistered...
13
14
 
15
+ def socket_is_free(host, port, uri)
16
+ begin
17
+ TCPSocket.new(host, port, connect_timeout: 1)
18
+ rescue Errno::ECONNREFUSED => e
19
+ return true
20
+ rescue => e
21
+ warn "*** #{uri} -- #{e.inspect}"
22
+ return nil # break, but don't use
23
+ end
24
+ return false
25
+ end
26
+
14
27
  options = OpenStruct.new
15
28
 
16
29
  op = OptionParser.new do |opts|
@@ -30,7 +43,7 @@ BANNER
30
43
  opts.on("-r", "--[no-]replace", "Replace existing server")
31
44
  opts.on("-oNAME", "--host=NAME", String, "Server host (127.0.0.1)")
32
45
  opts.on("-i", "--internet", 'Abbr. "on the Internet" (host = "::")')
33
- opts.on("-pNUM", "--port=NUM", Integer, "Port number (#{KDWATCH_PORT})")
46
+ opts.on("-pNUM", "--port=NUM", Integer, "Port number (#{KDWATCH_PORT}...)")
34
47
  opts.on("-[1-6]", "--[1-6]", Integer, "Abbr. port number (#{KDWATCH_PORT/10}x), must be last")
35
48
  end
36
49
  begin
@@ -52,7 +65,26 @@ if short_port = options[""]
52
65
  warn "** overriding port #{options.port} with #{port}" if options.port
53
66
  options.port = port
54
67
  else
55
- options.port ||= KDWATCH_PORT
68
+ unless options.port
69
+ port = KDWATCH_PORT
70
+ host = options.host
71
+ host = "[#{host}]" if host =~ /:/
72
+ uri = nil
73
+ found = false
74
+ 100.times do
75
+ uri = "http://#{host}:#{port}"
76
+ # warn "Trying URI #{uri}"
77
+ found = socket_is_free(options.host, port, uri)
78
+ break unless found == false
79
+ port += 1
80
+ end
81
+ unless found
82
+ warn "*** Cannot find port number available to serve from!"
83
+ exit 1
84
+ end
85
+ warn "*** Using URI: #{uri}"
86
+ options.port = port
87
+ end
56
88
  end
57
89
 
58
90
  # p options
data/kdwatch.gemspec CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
24
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
25
  end
26
- spec.executables = ['kdwatch']
26
+ spec.executables = ['kdwatch', 'kds']
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  spec.add_dependency "bundler", '~> 2.2'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kdwatch
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.1"
5
5
  end
data/lib/kdwatch-app.rb CHANGED
@@ -9,7 +9,7 @@ require "rack-livereload"
9
9
  require "guard"
10
10
  require "sinatra"
11
11
  require "kramdown-rfc2629"
12
- ENV["KDRFC_PREPEND"] = "time"
12
+ ENV["KDRFC_PREPEND"] = "time" if system("time", "true", err: "/dev/null")
13
13
  require "kramdown-rfc/kdrfc-processor"
14
14
  require "net/http/persistent"
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kdwatch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-17 00:00:00.000000000 Z
11
+ date: 2022-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -127,6 +127,7 @@ email:
127
127
  - cabo@tzi.org
128
128
  executables:
129
129
  - kdwatch
130
+ - kds
130
131
  extensions: []
131
132
  extra_rdoc_files: []
132
133
  files:
@@ -134,6 +135,7 @@ files:
134
135
  - Gemfile
135
136
  - LICENSE.txt
136
137
  - README.md
138
+ - bin/kds
137
139
  - bin/kdwatch
138
140
  - kdwatch.gemspec
139
141
  - lib/kdwatch-app.rb
@@ -157,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
159
  - !ruby/object:Gem::Version
158
160
  version: '0'
159
161
  requirements: []
160
- rubygems_version: 3.2.22
162
+ rubygems_version: 3.3.11
161
163
  signing_key:
162
164
  specification_version: 4
163
165
  summary: 'kdwatch: open auto-reloaded HTML for kramdown-rfc in a browser.'