nvim 1.6.1 → 1.8.0
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 +4 -4
- data/INFO.yaml +1 -1
- data/README.md +66 -16
- data/Rakefile +1 -1
- data/lib/neovim/client.rb +62 -0
- data/lib/neovim/connection.rb +5 -0
- data/lib/neovim/foreign/supplement.rb +5 -1
- data/lib/neovim/info.rb +2 -2
- data/lib/neovim/remote.rb +1 -1
- data/lib/neovim/ruby_provider.rb +26 -29
- data/lib/neovim/tools/copy.rb +3 -3
- data/nvim.gemspec +0 -1
- metadata +3 -4
- data/lib/neovim/foreign/nxxd.rb +0 -216
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1aa2670517c218f7981b8165338f7e985e2e33dcc704a3313326b3ba7de3fd8
|
4
|
+
data.tar.gz: 5bb1a4e2b677fb4f0727c46d1d4cbbe10a40ee6e77677faba5280cf1826b252c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3df9c57b705347c248c743185efeaf86a59291d16309709d4223dd20295632c1ebb413a92cacf501852877796350d37882930f2a77b04596a6efc72b993a26b
|
7
|
+
data.tar.gz: d781ec641a20ad904f82bad5162007bca0674f28ac3009dc928460de5d6943f03fcd9ea9b79e7e1a8a0016ad9c8465014c87170a495d0f60ac8c69560afd3292
|
data/INFO.yaml
CHANGED
data/README.md
CHANGED
@@ -69,14 +69,11 @@ echo Sum(13,7)
|
|
69
69
|
The `:ruby...` commands and the `rubyeval()` function behave as descibed
|
70
70
|
in `:h ruby`.
|
71
71
|
|
72
|
-
Files mentioned in the global variable `g:ruby_require` will be loaded
|
73
|
-
the first Ruby code will be run.
|
72
|
+
Files mentioned in the global variable `g:ruby_require` will be loaded
|
73
|
+
before the first Ruby code will be run.
|
74
74
|
|
75
|
-
Additionally you can directly execute the buffers contents
|
76
|
-
|
77
|
-
```vim
|
78
|
-
set number
|
79
|
-
```
|
75
|
+
Additionally you can directly execute the buffers contents (here, I did
|
76
|
+
`:set number`):
|
80
77
|
|
81
78
|
```
|
82
79
|
1 class C
|
@@ -203,10 +200,57 @@ the RPC communication.
|
|
203
200
|
:%ruby |
|
204
201
|
```
|
205
202
|
|
206
|
-
Yet,
|
203
|
+
Yet, I suggest not to use `fork` and `exec`, except when
|
207
204
|
you're absolutely sure what you're doing.
|
208
205
|
|
209
206
|
|
207
|
+
#### Exception handling
|
208
|
+
|
209
|
+
If you prefer, you may return an error as a value, too.
|
210
|
+
|
211
|
+
```
|
212
|
+
1 $rescue = true
|
213
|
+
2 z = 0
|
214
|
+
3 q = 1 / z
|
215
|
+
~
|
216
|
+
~
|
217
|
+
:set ft=ruby|1,3ruby |
|
218
|
+
```
|
219
|
+
|
220
|
+
Then:
|
221
|
+
|
222
|
+
```
|
223
|
+
1 $rescue = true
|
224
|
+
2 z = 0
|
225
|
+
3 q = 1 / z
|
226
|
+
4 #=> #<ZeroDivisionError: divided by 0>
|
227
|
+
5 puts "=begin", _.backtrace, "=end"
|
228
|
+
~
|
229
|
+
~
|
230
|
+
:5ruby |
|
231
|
+
```
|
232
|
+
|
233
|
+
Even non-standard errors wil be caught.
|
234
|
+
|
235
|
+
```
|
236
|
+
1 def f ; f ; end
|
237
|
+
2 f
|
238
|
+
~
|
239
|
+
~
|
240
|
+
:1,2ruby |
|
241
|
+
```
|
242
|
+
|
243
|
+
```
|
244
|
+
1 $$
|
245
|
+
2 #=> 49042
|
246
|
+
3 sleep 1000
|
247
|
+
~
|
248
|
+
~
|
249
|
+
:3ruby |
|
250
|
+
```
|
251
|
+
Then say 'kill 49042' somewhere else.
|
252
|
+
|
253
|
+
|
210
254
|
#### Global variables
|
211
255
|
|
212
256
|
The global variable `$vim` will be set to the client.
|
@@ -224,7 +268,7 @@ Further, the variables `$range` and `$lines` will be set.
|
|
224
268
|
2 bar
|
225
269
|
3 baz
|
226
270
|
~
|
227
|
-
:%ruby puts $range
|
271
|
+
:%ruby puts $range, $lines
|
228
272
|
```
|
229
273
|
|
230
274
|
The legacy variables `$curbuf` and `$curwin` are supported.
|
@@ -417,15 +461,20 @@ rubyfile <neovim/tools/copy>
|
|
417
461
|
'<,'>ruby tmuxbuf $lines
|
418
462
|
```
|
419
463
|
|
420
|
-
|
464
|
+
Maybe you like to install the
|
465
|
+
[Nxxd](https://github.com/BertramScharpf/ruby-nxxd) gem.
|
466
|
+
|
467
|
+
```vim
|
468
|
+
rubyfile <nxxd>
|
469
|
+
ruby r = (0...0x80).to_a.map { |c| c.chr }.join
|
470
|
+
ruby puts Nxxd::Dump.new.run r
|
471
|
+
```
|
472
|
+
|
473
|
+
Or even:
|
421
474
|
|
422
475
|
```vim
|
423
|
-
|
424
|
-
|
425
|
-
bn = $curbuf.get_name
|
426
|
-
$vim.command :enew!
|
427
|
-
File.open bn do |b| Nxxd::Dump.new.run b do |l| $vim.put [l], "l", false, true end end
|
428
|
-
EOT
|
476
|
+
rubyfile <nxxd>
|
477
|
+
HexDump /etc/localtime
|
429
478
|
```
|
430
479
|
|
431
480
|
|
@@ -433,4 +482,5 @@ EOT
|
|
433
482
|
|
434
483
|
* (C) 2024,2025 Bertram Scharpf <software@bertram-scharpf.de>
|
435
484
|
* License: [BSD-2-Clause+](./LICENSE)
|
485
|
+
* Repository: [ruby-nvim](https://github.com/BertramScharpf/ruby-nvim)
|
436
486
|
|
data/Rakefile
CHANGED
data/lib/neovim/client.rb
CHANGED
@@ -12,6 +12,9 @@ module Neovim
|
|
12
12
|
class Client
|
13
13
|
|
14
14
|
@strict = true
|
15
|
+
ENV[ "NVIM_RUBY_STRICT"]&.tap { |s|
|
16
|
+
@strict = s =~ /[1-9]|true|yes|on|enable/i
|
17
|
+
}
|
15
18
|
class <<self
|
16
19
|
attr_accessor :strict
|
17
20
|
end
|
@@ -114,5 +117,64 @@ module Neovim
|
|
114
117
|
|
115
118
|
end
|
116
119
|
|
120
|
+
class Lines
|
121
|
+
|
122
|
+
include Enumerable
|
123
|
+
|
124
|
+
def initialize client, range
|
125
|
+
case client
|
126
|
+
when Buffer then @client, @buffer = client.client, client.index
|
127
|
+
else @client, @buffer = client, 0
|
128
|
+
end
|
129
|
+
@i, @last = range.begin, range.end
|
130
|
+
end
|
131
|
+
|
132
|
+
def to_s
|
133
|
+
(@client.buf_get_lines @buffer, @i-1, @last, true).join $/
|
134
|
+
end
|
135
|
+
|
136
|
+
def method_missing sym, *args, **kwargs, &block
|
137
|
+
to_a.send sym, *args, **kwargs, &block
|
138
|
+
end
|
139
|
+
|
140
|
+
def each
|
141
|
+
while @i <= @last do
|
142
|
+
@inc = 1
|
143
|
+
l, = @client.buf_get_lines @buffer, @i-1, @i, true
|
144
|
+
yield l
|
145
|
+
@i += @inc
|
146
|
+
end
|
147
|
+
ensure
|
148
|
+
@i = nil
|
149
|
+
end
|
150
|
+
alias each_line each
|
151
|
+
|
152
|
+
def each_with_no
|
153
|
+
each do |l|
|
154
|
+
yield l, @i
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def map!
|
159
|
+
each do |l|
|
160
|
+
h = l.hash
|
161
|
+
m = yield l, @i
|
162
|
+
r =
|
163
|
+
case m
|
164
|
+
when String then m.lines unless m.hash == h
|
165
|
+
when Enumerable then m.map { |l| l.to_s }
|
166
|
+
when nil, false then []
|
167
|
+
end
|
168
|
+
if r then
|
169
|
+
r.each { |l| l.chomp! }
|
170
|
+
@client.buf_set_lines @buffer, @i-1, @i, true, r
|
171
|
+
@inc = r.length
|
172
|
+
@last += @inc-1
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
117
179
|
end
|
118
180
|
|
data/lib/neovim/connection.rb
CHANGED
@@ -47,6 +47,11 @@ module Neovim
|
|
47
47
|
api_info[ "error_types"].each { |type,info|
|
48
48
|
register_error type, info[ "id"]
|
49
49
|
}
|
50
|
+
if api_info["version"]["api_level"] >= 13 then
|
51
|
+
@client.define_singleton_method :err_writeln do |msg|
|
52
|
+
call_api :echo, [ [ msg], [ $/]], true, err: true
|
53
|
+
end
|
54
|
+
end
|
50
55
|
nil
|
51
56
|
end
|
52
57
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# neovim/foreign/supplement.rb --
|
2
|
+
# neovim/foreign/supplement.rb -- Additional useful Ruby functions
|
3
3
|
#
|
4
4
|
|
5
5
|
# The purpose of this is simply to reduce dependencies.
|
@@ -10,6 +10,9 @@ rescue LoadError
|
|
10
10
|
class NilClass ; def notempty? ; end ; end
|
11
11
|
class String ; def notempty? ; self unless empty? ; end ; end
|
12
12
|
class Array ; def notempty? ; self unless empty? ; end ; end
|
13
|
+
class Array ; def first= val ; self[ 0] = val ; end ; end
|
14
|
+
class Array ; def last= val ; self[-1] = val ;
|
15
|
+
rescue IndexError ; a.push val ; end ; end
|
13
16
|
class NilClass ; def to_bool ; false ; end ; end
|
14
17
|
class FalseClass ; def to_bool ; false ; end ; end
|
15
18
|
class Object ; def to_bool ; true ; end ; end
|
@@ -43,5 +46,6 @@ rescue LoadError
|
|
43
46
|
alias starts_with starts_with?
|
44
47
|
alias ends_with ends_with?
|
45
48
|
end
|
49
|
+
class Dir ; def entries! ; entries - %w(. ..) ; end ; end
|
46
50
|
end
|
47
51
|
|
data/lib/neovim/info.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require "neovim/meta.rb"
|
2
2
|
Neovim::INFO = Neovim::Meta.new "nvim",
|
3
|
-
version: "1.
|
3
|
+
version: "1.8.0",
|
4
4
|
license: "BSD-2-Clause+",
|
5
5
|
authors: ["Bertram Scharpf"],
|
6
6
|
email: "software@bertram-scharpf.de",
|
7
7
|
summary: "Yet another Ruby client for Neovim",
|
8
8
|
description: "A simple Ruby client for Neovim.\nClean code, minimal dependecies, no frills, no wokeness.",
|
9
9
|
homepage: "https://github.com/BertramScharpf/ruby-nvim",
|
10
|
-
commit: "
|
10
|
+
commit: "f393e6b"
|
data/lib/neovim/remote.rb
CHANGED
@@ -191,7 +191,7 @@ module Neovim
|
|
191
191
|
if @responses.key? message.request_id then
|
192
192
|
@responses[ message.request_id] = message
|
193
193
|
else
|
194
|
-
log :warning, "Dropped response", message.request_id
|
194
|
+
log :warning, "Dropped response", rid: message.request_id
|
195
195
|
end
|
196
196
|
when Message::Request, Message::Notification then
|
197
197
|
h = find_handler message.method_name
|
data/lib/neovim/ruby_provider.rb
CHANGED
@@ -81,9 +81,14 @@ module Neovim
|
|
81
81
|
yield client, *args
|
82
82
|
end
|
83
83
|
end
|
84
|
-
rescue
|
85
|
-
|
86
|
-
|
84
|
+
rescue Exception
|
85
|
+
msg = $!.to_s
|
86
|
+
if msg =~ /\A.*?:(\d+):\s*/ then
|
87
|
+
line, msg = $1, $'
|
88
|
+
else
|
89
|
+
line = $@.first[ /:(\d+):in\b/, 1]
|
90
|
+
end
|
91
|
+
client.err_writeln "Ruby #{$!.class}:#{line}: #{msg}"
|
87
92
|
end
|
88
93
|
end
|
89
94
|
|
@@ -100,18 +105,13 @@ module Neovim
|
|
100
105
|
|
101
106
|
def set_globals client, range
|
102
107
|
set_global_client client do
|
103
|
-
lines =
|
104
|
-
|
105
|
-
yield lines
|
108
|
+
$range, $lines = range, (Lines.new client, range)
|
109
|
+
yield $lines
|
106
110
|
end
|
107
111
|
ensure
|
108
112
|
$range, $lines = nil, nil
|
109
113
|
end
|
110
114
|
|
111
|
-
def get_lines client, range
|
112
|
-
client.buf_get_lines 0, range.begin-1, range.end, true
|
113
|
-
end
|
114
|
-
|
115
115
|
def plugin_provider &block
|
116
116
|
run_dsl DslProvider, &block
|
117
117
|
end
|
@@ -123,11 +123,13 @@ module Neovim
|
|
123
123
|
dsl.setup do |client|
|
124
124
|
r = client.get_var "ruby_require" rescue nil
|
125
125
|
case r
|
126
|
-
when
|
127
|
-
when
|
128
|
-
|
126
|
+
when String then r = r.split
|
127
|
+
when Array then
|
128
|
+
when Hash then r = r.keys
|
129
|
+
when nil then nil
|
130
|
+
else r = [r.to_s]
|
129
131
|
end
|
130
|
-
if r then
|
132
|
+
if r.notempty? then
|
131
133
|
WriteOut.redirect client do # Protect the RPC interface against erroneous output.
|
132
134
|
r.each do |l|
|
133
135
|
require l
|
@@ -164,11 +166,16 @@ module Neovim
|
|
164
166
|
code.rstrip!
|
165
167
|
if code =~ /\A\|?(-)?\z/ then # | is a workaround because Neovim doesn't allow empty code (the ultimate Quine).
|
166
168
|
no_out = $1
|
167
|
-
|
169
|
+
set_globals client, fst..lst do |lines|
|
168
170
|
client.command "#{lst}"
|
169
|
-
code = (get_lines client, fst..lst).join $/
|
170
171
|
WriteBuf.redirect client do
|
171
|
-
r =
|
172
|
+
r = begin
|
173
|
+
script_binding.eval lines.to_s, "ruby_run"
|
174
|
+
rescue Exception
|
175
|
+
$@.pop until $@.empty? or $@.last.ends_with? ":in `empty_binding'"
|
176
|
+
raise unless $rescue
|
177
|
+
$!
|
178
|
+
end
|
172
179
|
unless no_out or r.nil? then
|
173
180
|
script_binding.local_variable_set :_, r
|
174
181
|
puts "#=> #{r.inspect}"
|
@@ -219,20 +226,10 @@ module Neovim
|
|
219
226
|
# This is called by the +:rubydo+ command.
|
220
227
|
dsl.rpc :ruby_do_range do |client,fst,lst,code|
|
221
228
|
set_globals client, fst..lst do |lines|
|
222
|
-
|
223
|
-
lines.each do |l|
|
224
|
-
h = l.hash
|
229
|
+
lines.map! do |l,i|
|
225
230
|
(script_binding.eval 'proc do |l,i| $_, $. = l, i end').call l, i
|
226
231
|
script_binding.eval code, "ruby_do_range"
|
227
|
-
|
228
|
-
if m.hash != h then
|
229
|
-
m = m.lines
|
230
|
-
m.each { |x| x.chomp! }
|
231
|
-
client.buf_set_lines 0, i-1, i, true, m
|
232
|
-
i += m.length
|
233
|
-
else
|
234
|
-
i += 1
|
235
|
-
end
|
232
|
+
script_binding.eval '$_'
|
236
233
|
end
|
237
234
|
end
|
238
235
|
nil
|
data/lib/neovim/tools/copy.rb
CHANGED
@@ -63,9 +63,9 @@ module Kernel
|
|
63
63
|
|
64
64
|
def cmd_write cmd, data
|
65
65
|
case data
|
66
|
-
when
|
67
|
-
when String
|
68
|
-
else
|
66
|
+
when Enumerable then data = data.join $/
|
67
|
+
when String then nil
|
68
|
+
else data = data.to_s
|
69
69
|
end
|
70
70
|
IO.popen cmd, "w" do |t| t.write data end
|
71
71
|
end
|
data/nvim.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nvim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bertram Scharpf
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-03-30 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
email: software@bertram-scharpf.de
|
13
13
|
executables:
|
@@ -46,7 +46,6 @@ files:
|
|
46
46
|
- lib/neovim/connection.rb
|
47
47
|
- lib/neovim/foreign/mplight.rb
|
48
48
|
- lib/neovim/foreign/mplight/bufferio.rb
|
49
|
-
- lib/neovim/foreign/nxxd.rb
|
50
49
|
- lib/neovim/foreign/supplement.rb
|
51
50
|
- lib/neovim/foreign/supplement/socket.rb
|
52
51
|
- lib/neovim/handler.rb
|
@@ -80,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
79
|
- !ruby/object:Gem::Version
|
81
80
|
version: '0'
|
82
81
|
requirements: []
|
83
|
-
rubygems_version: 3.6.
|
82
|
+
rubygems_version: 3.6.6
|
84
83
|
specification_version: 4
|
85
84
|
summary: Yet another Ruby client for Neovim
|
86
85
|
test_files: []
|
data/lib/neovim/foreign/nxxd.rb
DELETED
@@ -1,216 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# neovim/foreign/nxxd.rb -- Hex Dump Tool
|
3
|
-
#
|
4
|
-
|
5
|
-
# The purpose of this is simply to reduce dependencies.
|
6
|
-
|
7
|
-
begin
|
8
|
-
require "nxxd"
|
9
|
-
rescue LoadError
|
10
|
-
|
11
|
-
# ----------------------------------------------------------------
|
12
|
-
#
|
13
|
-
# nxxd.rb -- Hex Dump Tool
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
module Nxxd
|
18
|
-
|
19
|
-
module ReadChunks
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def read_chunks input
|
24
|
-
case input
|
25
|
-
when String then
|
26
|
-
i = 0
|
27
|
-
while i < input.bytesize do
|
28
|
-
b = input.byteslice i, @line_size
|
29
|
-
yield b
|
30
|
-
i += @line_size
|
31
|
-
end
|
32
|
-
else
|
33
|
-
loop do
|
34
|
-
b = input.read @line_size
|
35
|
-
break unless b
|
36
|
-
yield b
|
37
|
-
break if b.length < @line_size
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
class Dump
|
45
|
-
|
46
|
-
include ReadChunks
|
47
|
-
|
48
|
-
LINE_SIZE = 16
|
49
|
-
ADDR_FMT = "%%0%dx:"
|
50
|
-
|
51
|
-
def initialize full: nil, upper: false, line_size: nil, addr_len: nil, input: nil
|
52
|
-
@full = full
|
53
|
-
@input = input
|
54
|
-
@line_size = line_size||LINE_SIZE
|
55
|
-
@addr_fmt = ADDR_FMT % (addr_len||8)
|
56
|
-
@nib_fmt = "%02x"
|
57
|
-
if upper then
|
58
|
-
@addr_fmt.upcase!
|
59
|
-
@nib_fmt.upcase!
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def run input
|
64
|
-
addr = 0
|
65
|
-
prev, repeat = nil, false
|
66
|
-
yield "# #@input" if @input
|
67
|
-
read_chunks input do |b|
|
68
|
-
if b == prev and not @full then
|
69
|
-
unless repeat then
|
70
|
-
yield "*"
|
71
|
-
repeat = true
|
72
|
-
end
|
73
|
-
else
|
74
|
-
r = @addr_fmt % addr
|
75
|
-
r << " "
|
76
|
-
h = b.unpack "C*"
|
77
|
-
sp = false
|
78
|
-
@line_size.times {
|
79
|
-
x = h.shift
|
80
|
-
r << (x ? @nib_fmt % x : " ")
|
81
|
-
r << " " if sp
|
82
|
-
sp = !sp
|
83
|
-
}
|
84
|
-
r << " " << (b.gsub /[^ -~]/, ".")
|
85
|
-
yield r
|
86
|
-
prev, repeat = b, false
|
87
|
-
end
|
88
|
-
addr += b.size
|
89
|
-
end
|
90
|
-
yield @addr_fmt % addr
|
91
|
-
nil
|
92
|
-
end
|
93
|
-
|
94
|
-
|
95
|
-
class <<self
|
96
|
-
|
97
|
-
def reverse input, output = nil
|
98
|
-
output ||= ""
|
99
|
-
o = String === output ? (WriteChunksString.new output) : output
|
100
|
-
o.set_encoding Encoding::ASCII_8BIT
|
101
|
-
r, repeat = nil, false
|
102
|
-
input.each_line { |l|
|
103
|
-
l.chomp!
|
104
|
-
case l
|
105
|
-
when /^\s*(?:#|$)/ then nil
|
106
|
-
when /^\*/ then repeat = true
|
107
|
-
when /^(?:(\h+):)?\s*((?:\h\h ?)*)/ then
|
108
|
-
addr, nibs = $~.captures
|
109
|
-
if addr then
|
110
|
-
addr = $1.to_i 0x10
|
111
|
-
if repeat then
|
112
|
-
loop do
|
113
|
-
s = addr - o.tell
|
114
|
-
break if s <= 0
|
115
|
-
o.write s >= r.length ? r : r[ 0, s]
|
116
|
-
end
|
117
|
-
repeat = false
|
118
|
-
else
|
119
|
-
end
|
120
|
-
o.seek addr
|
121
|
-
end
|
122
|
-
r = (nibs.scan /\h\h/).map { |x| x.to_i 0x10 }.pack "C*"
|
123
|
-
o.write r
|
124
|
-
else
|
125
|
-
raise "Uninterpretable hex dump: #{l.chomp}"
|
126
|
-
end
|
127
|
-
}
|
128
|
-
output
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
class DumpNums
|
136
|
-
|
137
|
-
include ReadChunks
|
138
|
-
|
139
|
-
LINE_SIZE = 12
|
140
|
-
|
141
|
-
def initialize upper: false, line_size: nil, capitals: nil, input: nil
|
142
|
-
@line_size = line_size||LINE_SIZE
|
143
|
-
@nib_fmt = "%#04x"
|
144
|
-
@nib_fmt.upcase! if upper
|
145
|
-
if input then
|
146
|
-
@varname = input.dup
|
147
|
-
@varname.insert 0, "__" if @varname =~ /\A\d/
|
148
|
-
@varname.gsub! /[^a-zA-Z0-9]/, "_"
|
149
|
-
@varname.upcase! if capitals
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
def run input, &block
|
154
|
-
if @varname then
|
155
|
-
yield "unsigned char #@varname[] = {"
|
156
|
-
len = run_plain input, &block
|
157
|
-
yield "};"
|
158
|
-
yield "unsigned int #@varname\_len = %d;" % len
|
159
|
-
else
|
160
|
-
run_plain input, &block
|
161
|
-
end
|
162
|
-
nil
|
163
|
-
end
|
164
|
-
|
165
|
-
private
|
166
|
-
|
167
|
-
def run_plain input
|
168
|
-
prev, len = nil, 0
|
169
|
-
read_chunks input do |b|
|
170
|
-
if prev then
|
171
|
-
prev << ","
|
172
|
-
yield prev
|
173
|
-
end
|
174
|
-
prev = " " + ((b.unpack "C*").map { |x| @nib_fmt % x }.join ", ")
|
175
|
-
len += b.bytesize
|
176
|
-
end
|
177
|
-
yield prev if prev
|
178
|
-
len
|
179
|
-
end
|
180
|
-
|
181
|
-
end
|
182
|
-
|
183
|
-
class WriteChunksString
|
184
|
-
def initialize str
|
185
|
-
@str = str
|
186
|
-
end
|
187
|
-
def set_encoding enc
|
188
|
-
@str.force_encoding enc
|
189
|
-
end
|
190
|
-
def tell ; @pos || @str.length ; end
|
191
|
-
def seek pos
|
192
|
-
s = pos - @str.length
|
193
|
-
if s >= 0 then
|
194
|
-
s.times { @str << "\0".b }
|
195
|
-
else
|
196
|
-
@pos = pos
|
197
|
-
end
|
198
|
-
end
|
199
|
-
def write b
|
200
|
-
if @pos then
|
201
|
-
l = b.length
|
202
|
-
@str[ @pos, l] = b
|
203
|
-
@pos += l
|
204
|
-
@pos = nil if @pos >= @str.length
|
205
|
-
else
|
206
|
-
@str << b
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
end
|
212
|
-
|
213
|
-
# ----------------------------------------------------------------
|
214
|
-
|
215
|
-
end
|
216
|
-
|