term-slides 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/term/slides.rb +211 -213
- data/lib/term/slides/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 634623c4210879641f82e3d3da25252de978705ad1744e9e241d9fd5021166e9
|
4
|
+
data.tar.gz: 7093b0243258b5af5173d1d27e2b870690748258c5a600b69d166c0b94d52082
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4d580861d5d89e77cb5a635287d8c2f18cdfde3777cf80edf544c4bf00d51908bdd0afe325bee8a062da57725103ca032dc63251da397596824d65b4a014e4f
|
7
|
+
data.tar.gz: f3fb4db4ccaf368cbabecbc613f919efac1c81043d55c77a272a70c2535fc437add686deee254d7608d8456df5d704167df6a1bdf747e12587c93421ab14198f
|
data/lib/term/slides.rb
CHANGED
@@ -1,241 +1,239 @@
|
|
1
1
|
require "term/slides/version"
|
2
2
|
|
3
3
|
module Term
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
require 'highline'
|
4
|
+
class Error < StandardError; end
|
5
|
+
require 'io/console'
|
6
|
+
require 'colorize'
|
7
|
+
require 'tty-table'
|
8
|
+
require 'tty-command'
|
9
|
+
require 'highline'
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
def render_text text
|
27
|
-
puts center(text.text)
|
28
|
-
end
|
29
|
-
def render_image_file src
|
30
|
-
kitty = '/Applications/kitty.app/Contents/MacOS/kitty'
|
31
|
-
kitty = 'kitty' unless File.exists? kitty
|
32
|
-
`#{kitty} +kitten icat #{src}`
|
33
|
-
end
|
34
|
-
def render_image image
|
35
|
-
render_image_file image.src
|
36
|
-
end
|
37
|
-
def render_diagram diagram
|
38
|
-
render_image_file diagram.build
|
39
|
-
end
|
40
|
-
def render_slide slide
|
41
|
-
puts center(slide.name.colorize(:light_blue).bold)
|
42
|
-
puts
|
43
|
-
slide.content.each { |c| c.render }
|
44
|
-
end
|
11
|
+
class TTYRenderer
|
12
|
+
def render_code code
|
13
|
+
path = "code.#{code.format}"
|
14
|
+
File.write(path, code.content)
|
15
|
+
out, err = TTY::Command.new(pty: true, printer: :null).run('vimcat', path)
|
16
|
+
puts out
|
17
|
+
end
|
18
|
+
def render_table table
|
19
|
+
puts center(TTY::Table.new(table.headers, table.rows).render(:unicode))
|
20
|
+
end
|
21
|
+
def center text
|
22
|
+
width = HighLine::SystemExtensions.terminal_size.first
|
23
|
+
text.split("\n").map { |x| x.center(width) }.join("\n")
|
45
24
|
end
|
25
|
+
def render_text text
|
26
|
+
puts center(text.text)
|
27
|
+
end
|
28
|
+
def render_image_file src
|
29
|
+
kitty = '/Applications/kitty.app/Contents/MacOS/kitty'
|
30
|
+
kitty = 'kitty' unless File.exists? kitty
|
31
|
+
`#{kitty} +kitten icat #{src}`
|
32
|
+
end
|
33
|
+
def render_image image
|
34
|
+
render_image_file image.src
|
35
|
+
end
|
36
|
+
def render_diagram diagram
|
37
|
+
render_image_file diagram.build
|
38
|
+
end
|
39
|
+
def render_slide slide
|
40
|
+
puts center(slide.name.colorize(:light_blue).bold)
|
41
|
+
puts
|
42
|
+
slide.content.each { |c| c.render }
|
43
|
+
end
|
44
|
+
end
|
46
45
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
68
|
-
def render_diagram diagram
|
69
|
-
puts ".. image:: #{diagram.build}"
|
70
|
-
puts " :width: #{diagram.width}" if not diagram.width.nil?
|
71
|
-
puts " :height: #{diagram.height}" if not diagram.height.nil?
|
72
|
-
puts
|
73
|
-
end
|
74
|
-
def render_slide slide
|
75
|
-
puts
|
76
|
-
puts slide.name
|
77
|
-
puts slide.name.gsub(/./, "=")
|
78
|
-
puts
|
79
|
-
slide.content.each { |c| c.render }
|
80
|
-
puts
|
81
|
-
puts "----"
|
82
|
-
puts
|
83
|
-
end
|
46
|
+
class HovercraftRenderer
|
47
|
+
def render_code code
|
48
|
+
puts ".. code:: #{code.format}"
|
49
|
+
puts
|
50
|
+
puts code.content.gsub(/^/, " ")
|
51
|
+
end
|
52
|
+
def render_table table
|
53
|
+
puts
|
54
|
+
puts ".. table::"
|
55
|
+
puts
|
56
|
+
a = TTY::Table.new(table.headers, table.rows).render(:ascii).split("\n")
|
57
|
+
s = (a[0..2] + a[3..-1].map { |l| l += "\n#{a[0]}"}).join("\n")
|
58
|
+
puts s.gsub(/^/, " ")
|
59
|
+
puts
|
60
|
+
end
|
61
|
+
def render_text text
|
62
|
+
puts text.text
|
63
|
+
end
|
64
|
+
def render_image image
|
65
|
+
puts ".. image:: #{image.src}"
|
84
66
|
end
|
67
|
+
def render_diagram diagram
|
68
|
+
puts ".. image:: #{diagram.build}"
|
69
|
+
puts " :width: #{diagram.width}" if not diagram.width.nil?
|
70
|
+
puts " :height: #{diagram.height}" if not diagram.height.nil?
|
71
|
+
puts
|
72
|
+
end
|
73
|
+
def render_slide slide
|
74
|
+
puts
|
75
|
+
puts slide.name
|
76
|
+
puts slide.name.gsub(/./, "=")
|
77
|
+
puts
|
78
|
+
slide.content.each { |c| c.render }
|
79
|
+
puts
|
80
|
+
puts "----"
|
81
|
+
puts
|
82
|
+
end
|
83
|
+
end
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
end
|
93
|
-
def render
|
94
|
-
@renderer.render_code self
|
95
|
-
end
|
85
|
+
class Code
|
86
|
+
attr_reader :format, :content
|
87
|
+
def initialize renderer, format, content
|
88
|
+
@renderer = renderer
|
89
|
+
@format = format
|
90
|
+
@content = content
|
96
91
|
end
|
92
|
+
def render
|
93
|
+
@renderer.render_code self
|
94
|
+
end
|
95
|
+
end
|
97
96
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
end
|
103
|
-
def initialize renderer, headers, &block
|
104
|
-
@renderer = renderer
|
105
|
-
@headers = headers
|
106
|
-
@rows = []
|
107
|
-
instance_eval &block
|
108
|
-
end
|
109
|
-
def render
|
110
|
-
@renderer.render_table self
|
111
|
-
end
|
97
|
+
class Table
|
98
|
+
attr_reader :rows, :headers
|
99
|
+
def row *r
|
100
|
+
@rows << r
|
112
101
|
end
|
102
|
+
def initialize renderer, headers, &block
|
103
|
+
@renderer = renderer
|
104
|
+
@headers = headers
|
105
|
+
@rows = []
|
106
|
+
instance_eval &block
|
107
|
+
end
|
108
|
+
def render
|
109
|
+
@renderer.render_table self
|
110
|
+
end
|
111
|
+
end
|
113
112
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
113
|
+
class Text
|
114
|
+
attr_reader :text
|
115
|
+
def initialize renderer, text
|
116
|
+
@renderer = renderer
|
117
|
+
@text = text
|
118
|
+
end
|
120
119
|
|
121
|
-
|
122
|
-
|
123
|
-
end
|
120
|
+
def render
|
121
|
+
@renderer.render_text self
|
124
122
|
end
|
123
|
+
end
|
125
124
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
end
|
125
|
+
class Diagram
|
126
|
+
attr_reader :dot, :width, :height
|
127
|
+
def initialize renderer, dot, width = nil, height = nil
|
128
|
+
@renderer = renderer
|
129
|
+
@dot = dot
|
130
|
+
@width = width
|
131
|
+
@height = height
|
132
|
+
end
|
133
|
+
def build
|
134
|
+
$i ||= 0
|
135
|
+
path = "graph#{$i}.png"
|
136
|
+
`echo "#{@dot.gsub('"', '\\"')}" | dot -Tpng > #{path}`
|
137
|
+
$i += 1
|
138
|
+
path
|
139
|
+
end
|
140
|
+
def render
|
141
|
+
@renderer.render_diagram self
|
144
142
|
end
|
143
|
+
end
|
145
144
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
end
|
145
|
+
class Image
|
146
|
+
attr_reader :src
|
147
|
+
def initialize renderer, src
|
148
|
+
@renderer = renderer
|
149
|
+
@src = src
|
150
|
+
end
|
151
|
+
def render
|
152
|
+
@renderer.render_image self
|
155
153
|
end
|
154
|
+
end
|
156
155
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
end
|
175
|
-
def initialize renderer, name, &block
|
176
|
-
@renderer = renderer
|
177
|
-
@name = name
|
178
|
-
@content = []
|
179
|
-
instance_eval &block
|
180
|
-
end
|
181
|
-
def render
|
182
|
-
@renderer.render_slide self
|
183
|
-
end
|
156
|
+
class Slide
|
157
|
+
attr_accessor :name, :content, :renderer
|
158
|
+
def text s
|
159
|
+
@content << Text.new(@renderer, s)
|
160
|
+
end
|
161
|
+
alias t text
|
162
|
+
def code format, s
|
163
|
+
@content << Code.new(@renderer, format, s)
|
164
|
+
end
|
165
|
+
def table *headers, &block
|
166
|
+
@content << Table.new(@renderer, headers, &block)
|
167
|
+
end
|
168
|
+
def diagram dot
|
169
|
+
@content << Diagram.new(@renderer, dot)
|
170
|
+
end
|
171
|
+
def image src
|
172
|
+
@content << Image.new(@renderer, src)
|
184
173
|
end
|
174
|
+
def initialize renderer, name, &block
|
175
|
+
@renderer = renderer
|
176
|
+
@name = name
|
177
|
+
@content = []
|
178
|
+
instance_eval &block
|
179
|
+
end
|
180
|
+
def render
|
181
|
+
@renderer.render_slide self
|
182
|
+
end
|
183
|
+
end
|
185
184
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
185
|
+
class Slides
|
186
|
+
def slide name, &block
|
187
|
+
@slides << Slide.new(@renderer, name, &block)
|
188
|
+
end
|
189
|
+
def initialize args, &block
|
190
|
+
@args = args
|
191
|
+
if @args.size > 0 and @args[0] == "hovercraft"
|
192
|
+
@renderer = HovercraftRenderer.new
|
193
|
+
else
|
194
|
+
@renderer = TTYRenderer.new
|
195
|
+
end
|
196
|
+
@slides = []
|
197
|
+
instance_eval &block
|
198
|
+
end
|
199
|
+
def read_char
|
200
|
+
STDIN.echo = false
|
201
|
+
STDIN.raw!
|
203
202
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
203
|
+
input = STDIN.getc.chr
|
204
|
+
if input == "\e" then
|
205
|
+
input << STDIN.read_nonblock(3) rescue nil
|
206
|
+
input << STDIN.read_nonblock(2) rescue nil
|
207
|
+
end
|
208
|
+
ensure
|
209
|
+
STDIN.echo = true
|
210
|
+
STDIN.cooked!
|
212
211
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
end
|
222
|
-
else
|
223
|
-
@slides[@args[0].to_i].render
|
212
|
+
return input
|
213
|
+
end
|
214
|
+
def run
|
215
|
+
i = 0
|
216
|
+
if @args.size > 0
|
217
|
+
if @args[0] == "hovercraft"
|
218
|
+
@slides.each do |slide|
|
219
|
+
slide.render
|
224
220
|
end
|
225
|
-
|
221
|
+
else
|
222
|
+
@slides[@args[0].to_i].render
|
226
223
|
end
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
224
|
+
return
|
225
|
+
end
|
226
|
+
while true
|
227
|
+
puts `clear`
|
228
|
+
puts "#{i + 1}/#{@slides.size}"
|
229
|
+
@slides[i].render
|
230
|
+
s = read_char
|
231
|
+
if s == "q"
|
232
|
+
break
|
233
|
+
elsif s == "p"
|
234
|
+
i -= 1 if i > 0
|
235
|
+
elsif s == "n"
|
236
|
+
i += 1 if i < (@slides.size - 1)
|
239
237
|
end
|
240
238
|
end
|
241
239
|
end
|
data/lib/term/slides/version.rb
CHANGED