stty 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/stty.rb +406 -0
  2. metadata +76 -0
@@ -0,0 +1,406 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of stty.
5
+ #
6
+ # stty is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # stty is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with stty. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'termios'
21
+
22
+ module STTY
23
+ include Termios
24
+ DEFAULT_CC = [3, 28, 127, 21, 4, 0, 1, 0, 17, 19, 26, 0, 18, 15, 23, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
25
+
26
+ def ispeed=(bauds)
27
+ return unless bauds.is_a?(Integer)
28
+ t = self.tcgetattr
29
+ t.ispeed = bauds
30
+ self.tcsetattr(TCSANOW, t)
31
+ self
32
+ end
33
+
34
+ def ospeed=(bauds)
35
+ return unless bauds.is_a?(Integer)
36
+ t = self.tcgetattr
37
+ t.ospeed = bauds
38
+ self.tcsetattr(TCSANOW, t)
39
+ self
40
+ end
41
+
42
+ def speed=(bauds)
43
+ self.ispeed = bauds
44
+ self.ospeed = bauds
45
+ end
46
+
47
+
48
+ {
49
+ iflag: IFLAG_NAMES,
50
+ oflag: OFLAG_NAMES,
51
+ cflag: CFLAG_NAMES,
52
+ lflag: LFLAG_NAMES
53
+ }.each {|meth, syms|
54
+ getter = Termios.instance_method(meth)
55
+ setter = Termios.instance_method("#{meth}=")
56
+
57
+ syms.each {|sym|
58
+ val = self.const_get(sym)
59
+ sym = sym.to_s.downcase
60
+
61
+ if sym !~ /^(?!i).*\d+$/
62
+ define_method("#{sym}=") {|bool|
63
+ to = self.tcgetattr
64
+ t = getter.bind(to)
65
+ s = setter.bind(to)
66
+ s.call(bool ? t.call | val : t.call & ~val)
67
+ self.tcsetattr(TCSANOW, to)
68
+ self
69
+ }
70
+
71
+ define_method("#{sym}?") {
72
+ (getter.bind(self.tcgetattr).call & val) == val
73
+ }
74
+
75
+ define_method("no#{sym}") {|&blk|
76
+ old = self.send("#{sym}?")
77
+ self.send("#{sym}=", false)
78
+
79
+ if blk
80
+ return blk.call(self).tap {
81
+ self.send("#{sym}=", old)
82
+ }
83
+ end
84
+
85
+ nil
86
+ }
87
+
88
+ define_method(sym) {|&blk|
89
+ old = self.send("#{sym}?")
90
+ self.send("#{sym}=", true)
91
+
92
+ if blk
93
+ return blk.call(self).tap {
94
+ self.send("#{sym}=", old)
95
+ }
96
+ end
97
+
98
+ nil
99
+ }
100
+ else
101
+ define_method(sym) {
102
+ to = self.tcgetattr
103
+ t = getter.bind(to)
104
+ s = setter.bind(to)
105
+ s.call(t.call | val)
106
+ self.tcsetattr(TCSANOW, to)
107
+ self
108
+ }
109
+ end
110
+ }
111
+ }
112
+
113
+ CCINDEX.each {|i, n|
114
+ n = n.to_s.downcase.sub(/^v/, '')
115
+
116
+ define_method(n) {
117
+ self.tcgetattr.cc[i]
118
+ }
119
+
120
+ define_method("#{n}=") {|v|
121
+ t = self.tcgetattr
122
+ t.cc[i] = v
123
+ self.tcsetattr(TCSANOW, t)
124
+ self
125
+ }
126
+ }
127
+
128
+ def tabs
129
+ self.tab0
130
+ end
131
+
132
+ def notabs
133
+ self.tab3
134
+ end
135
+
136
+ def ek
137
+ self.erase, self.kill = DEFAULT_CC[VERASE], DEFAULT_CC[VKILL]
138
+ end
139
+
140
+ def evenp
141
+ self.parenb; self.noparodd; self.cs7
142
+ end
143
+
144
+ def noevenp
145
+ self.noparenb; self.cs8
146
+ end
147
+
148
+ def lcase
149
+ if block_given?
150
+ return xcase { iuclc { olcuc {
151
+ yield self
152
+ } } }
153
+ else
154
+ self.xcase; self.iuclc; self.olcuc
155
+ end
156
+
157
+ nil
158
+ end
159
+
160
+ def nolcase
161
+ if block_given?
162
+ return noxcase { noiuclc { noolcuc {
163
+ yield self
164
+ } } }
165
+ else
166
+ self.noxcase; self.noiuclc; self.noolcuc
167
+ end
168
+
169
+ nil
170
+ end
171
+
172
+ def nolitout
173
+ self.parenb; self.istrip; self.opost; self.cs7
174
+ nil
175
+ end
176
+
177
+ def crt
178
+ if block_given?
179
+ return echoe { echoctl { echoke {
180
+ yield self
181
+ } } }
182
+ else
183
+ echoe; echoctl; echoke
184
+ end
185
+
186
+ nil
187
+ end
188
+
189
+ def dec
190
+ if block_given?
191
+ x = [self.intr, self.erase, self.kill]
192
+
193
+ return echoe { echoctl { echoke { noixany {
194
+ self.intr, self.erase, self.kill = 0x03, 0x7f, 0x16
195
+ yield(self).tap {
196
+ self.intr, self.erase, self.kill = x
197
+ }
198
+ } } } }
199
+ else
200
+ echoe; echoctl; echoke; noixany
201
+ self.intr, self.erase, self.kill = 0x03, 0x7f, 0x16
202
+ end
203
+
204
+ nil
205
+ end
206
+
207
+ def oddp
208
+ self.parenb; self.parodd; self.cs7
209
+ end
210
+
211
+ def nooddp
212
+ self.noparenb; self.cs8
213
+ end
214
+
215
+ def pass8
216
+ self.noparenb; self.noistrip; self.cs8
217
+ end
218
+
219
+ def nopass8
220
+ self.noparenb; self.noistrip; self.cs7
221
+ end
222
+
223
+ def nl
224
+ if block_given?
225
+ return noicrnl { noonlcr {
226
+ yield self
227
+ } }
228
+ else
229
+ noicrnl; noonlcr
230
+ end
231
+
232
+ nil
233
+ end
234
+
235
+ def nonl
236
+ if block_given?
237
+ return icrnl { noinlcr { noigncr { onlcr { noocrnl { noonlret {
238
+ yield self
239
+ } } } } } }
240
+ else
241
+ icrnl; noinlcr; noigncr; onlcr; noocrnl; noonlret
242
+ end
243
+
244
+ nil
245
+ end
246
+
247
+ def crtkill?
248
+ echoprt? and echoe?
249
+ end
250
+
251
+ def crtkill
252
+ if block_given?
253
+ return echoprt { echoe {
254
+ yield self
255
+ } }
256
+ else
257
+ echoprt; echoe
258
+ end
259
+
260
+ nil
261
+ end
262
+
263
+ def nocrtkill
264
+ if block_given?
265
+ return echoctl { echok {
266
+ yield self
267
+ } }
268
+ else
269
+ echoctl; echok
270
+ end
271
+
272
+ nil
273
+ end
274
+
275
+ def cbreak?
276
+ !self.icanon?
277
+ end
278
+
279
+ def cbreak=(bool)
280
+ self.icanon = !bool
281
+ end
282
+
283
+ def cbreak(&blk)
284
+ noicanon(&blk)
285
+ end
286
+
287
+ def nocbreak(&blk)
288
+ icanon(&blk)
289
+ end
290
+
291
+ def sane
292
+ self.cread; self.noignbrk; self.brkint; self.noinlcr; self.igncr; self.icrnl
293
+ self.noiutf8; self.noixoff; self.noiuclc; self.noixany; self.imaxbel; self.opost
294
+ self.noolcuc; self.noocrnl; self.onlcr; self.noonocr; self.noonlret; self.noofill
295
+ self.noofdel; self.nl0; self.cr0; self.tab0; self.bs0; self.vt0; self.ff0
296
+ self.isig; self.icanon; self.iexten; self.echo; self.echoe; self.echok
297
+ self.noechonl; self.nonoflsh; self.noxcase; self.notostop; self.noechoprt
298
+ self.echoctl; self.echoke
299
+ t = self.tcgetattr
300
+ t.cc = DEFAULT_CC[0, NCCS]
301
+ self.tcsetattr(TCSANOW, t)
302
+ nil
303
+ end
304
+
305
+ def raw?
306
+ %w{ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixon ixoff iuclc ixany imaxbel opost isig icanon xcase}.map {|sym|
307
+ !self.send("#{sym}?")
308
+ }.inject(:&) and self.min == 1 and self.time == 0
309
+ end
310
+
311
+ def raw=(bool)
312
+ %w{ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixon ixoff iuclc ixany imaxbel opost isig icanon xcase}.each {|sym|
313
+ self.send("#{sym}=", !bool)
314
+ }
315
+
316
+ if bool
317
+ self.min = 1
318
+ self.time = 0
319
+ end
320
+
321
+ self
322
+ end
323
+
324
+ def raw(&blk)
325
+ if blk
326
+ %w{ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixon ixoff iuclc ixany imaxbel opost isig icanon xcase}.inject(blk) {|block, sym|
327
+ lambda {|*|
328
+ self.send("no#{sym}", &block)
329
+ }
330
+ }.call
331
+ else
332
+ %w{ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixon ixoff iuclc ixany imaxbel opost isig icanon xcase}.each {|sym|
333
+ self.send("no#{sym}")
334
+ }
335
+ end
336
+ end
337
+
338
+ def noraw(&blk)
339
+ if blk
340
+ %w{ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixon ixoff iuclc ixany imaxbel opost isig icanon xcase}.inject {|block, sym|
341
+ lambda {
342
+ self.send(sym, &block)
343
+ }
344
+ }.call
345
+ else
346
+ %w{ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixon ixoff iuclc ixany imaxbel opost isig icanon xcase}.each {|sym|
347
+ self.send(sym)
348
+ }
349
+ end
350
+ end
351
+
352
+ def cooked?
353
+ %w{ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixon ixoff iuclc ixany imaxbel opost isig icanon xcase}.map {|sym|
354
+ self.send("#{sym}?")
355
+ }.inject(:&)
356
+ end
357
+
358
+ def cooked=(bool)
359
+ self.raw = !bool
360
+ end
361
+
362
+ def cooked(&blk)
363
+ noraw(&blk)
364
+ end
365
+
366
+ def nocooked(&blk)
367
+ raw(&blk)
368
+ end
369
+
370
+ def size
371
+ data = [0, 0, 0, 0].pack('SSSS')
372
+
373
+ if self.ioctl(TIOCGWINSZ, data) >= 0
374
+ rows, cols, * = data.unpack('SSSS')
375
+ [rows, cols]
376
+ else
377
+ [80, 80]
378
+ end
379
+ end
380
+
381
+ def cols(n)
382
+ data = [self.size[0], n, 0, 0].pack('SSSS')
383
+
384
+ self.ioctl(TIOCSWINSZ, data) >= 0 ? true : false
385
+ end
386
+ alias columns cols
387
+
388
+ def rows(n)
389
+ data = [n, self.size[1], 0, 0].pack('SSSS')
390
+
391
+ self.ioctl(TIOCSWINSZ, data) >= 0 ? true : false
392
+ end
393
+
394
+ %w{echoctl ctlecho echoe crterase echoke crtkill echoprt prterase ixany decctlq
395
+ hupcl hup ixoff tandem evenp parity}.each_slice(2) {|args|
396
+ o, a = args.map(&:to_sym)
397
+ next unless (self.instance_method(o) rescue nil)
398
+
399
+ eval("alias #{a} #{o}")
400
+
401
+ next unless (self.instance_method("#{o}?") rescue nil)
402
+ eval("alias #{a}? #{o}?")
403
+ eval("alias #{a}= #{o}=")
404
+ eval("alias no#{a} no#{o}")
405
+ }
406
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stty
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - shura
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-06-01 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: termios
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: lib to set and get terminal line settings
34
+ email: shura1991@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - lib/stty.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/shurizzle/ruby-stty
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: lib to set and get terminal line settings
75
+ test_files: []
76
+