Class: Ruber::CommandPlugin::Output

Inherits:
Object
  • Object
show all
Defined in:
plugins/command/output.rb

Overview

Fake IO object for use by the command plugin. Despite the name, it can be used to display standard error as well as standard output.

It displays the text in a Qt::PlainTextEdit using a given color.

All the methods in this class are implementations of the API provided by IO.

Examples:

Typical usage

old_stdout = $stdout #store the old standard output to restore it later
$stdout = Output.new plain_text_edit, Qt::Color.new(Qt.blue)
puts "hello" # => the string "hello" is displayed in the widget
$stdout = old_stout # restore the old standard output

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Output) initialize(widget, color, fileno = 1)

A new instance of Output

Parameters:

  • widget (Qt::PlainTextEdit)

    the widget to display the output into

  • color (Qt::Color)

    the color to use for the text

  • fileno (Integer) (defaults to: 1)

    the number that the #fileno method should return



47
48
49
50
51
52
53
54
# File 'plugins/command/output.rb', line 47

def initialize widget, color, fileno = 1
  @widget = widget
  @close_on_exec = false
  @closed = false
  @sync = true
  @brush = Qt::Brush.new color
  @fileno = fileno
end

Instance Attribute Details

- (Boolean) sync

Whether byffering is disabled or enabled. This class doesn’t buffer output, so this makes no difference. It’s only provided for compatibility with IO API

Returns:

  • (Boolean)

    whether byffering is disabled or enabled. This class doesn’t buffer output, so this makes no difference. It’s only provided for compatibility with IO API



61
62
63
# File 'plugins/command/output.rb', line 61

def sync
  @sync
end

Instance Method Details

- (Output) <<(text)

As IO#<<

Returns:



68
69
70
71
# File 'plugins/command/output.rb', line 68

def << text
  write text
  self
end

- (nil) binmode

As IO#binmode

This method doesn’t actually do anything. It’s just for compatibility with the IO API

Returns:

  • (nil)


80
81
# File 'plugins/command/output.rb', line 80

def binmode
end

- (TrueClass) binmode?

As IO#binmode?

This method always returns true. It’s just for compatibility with the IO API

Returns:

  • (TrueClass)

    true



90
91
92
# File 'plugins/command/output.rb', line 90

def binmode?
  true
end

- (Enumerator) bytes

As IO#bytes

Returns:

  • (Enumerator)

    an enumerator which calls #each_byte



99
100
101
# File 'plugins/command/output.rb', line 99

def bytes
  each_byte
end

- (Enumerator) chars

As IO#chars

Returns:

  • (Enumerator)

    an enumerator which calls #each_char



109
110
111
# File 'plugins/command/output.rb', line 109

def chars
  each_char
end

- (nil) close

As IO#close

Returns:

  • (nil)


118
119
120
121
# File 'plugins/command/output.rb', line 118

def close
  @closed = true
  nil
end

- (Object) close_on_exec=(val)

As IO#close_on_exec

This method does nothing. It’s only for compatibility with IO API

Parameters:

  • val (Object)

    the new value

Returns:



131
132
133
# File 'plugins/command/output.rb', line 131

def close_on_exec= val
  @close_on_exec = val.to_bool
end

- (Boolean) close_on_exec?

As IO#close_on_exec?

#close_on_exec= has no influence on this class, so the value returned here is meaningless. It’s only provided for compatibility with IO API

Returns:

  • (Boolean)

    whether close on exec is enabled or not. Nothing changes in either case



143
144
145
# File 'plugins/command/output.rb', line 143

def close_on_exec?
  @close_on_exec
end

- (nil) close_read

As IO#close_read

Returns:

  • (nil)

Raises:

  • (IOError)

    always as this class isn’t open for reading



153
154
155
# File 'plugins/command/output.rb', line 153

def close_read
  raise IOError, 'closing non-duplex IO for reading'
end

- (nil) close_write

As IO#close_write

Returns:

  • (nil)


162
163
164
165
# File 'plugins/command/output.rb', line 162

def close_write
  @closed = true
  nil
end

- (Boolean) closed?

As IO#closed?

Returns:

  • (Boolean)

    whether the stream is closed or not



172
173
174
# File 'plugins/command/output.rb', line 172

def closed?
  @closed
end

- (Enumerator) each

As IO#each

Returns:

  • (Enumerator)

    an enumerator which calls #each when called without a block

Raises:

  • (IOError)

    when called with a block because the stream is not open for reading



182
183
184
185
# File 'plugins/command/output.rb', line 182

def each
  raise IOError, "not opened for reading" if block_given?
  to_enum
end

- (Enumerator) each_byte

As IO#each_byte

Returns:

  • (Enumerator)

    an enumerator which calls #each_byte when called without a block

Raises:

  • (IOError)

    when called with a block because the stream is not open for reading



193
194
195
196
# File 'plugins/command/output.rb', line 193

def each_byte
  raise IOError, "not opened for reading" if block_given?
  to_enum :each_byte
end

- (Enumerator) each_char

As IO#each_char

Returns:

  • (Enumerator)

    an enumerator which calls #each_char when called without a block

Raises:

  • (IOError)

    when called with a block because the stream is not open for reading



204
205
206
207
# File 'plugins/command/output.rb', line 204

def each_char
  raise IOError, "not opened for reading" if block_given?
  to_enum :each_char
end

- (Enumerator) each_line(sep)

As IO#each

Returns:

  • (Enumerator)

    an enumerator which calls #each_line when called without a block

Raises:

  • (IOError)

    when called with a block because the stream is not open for reading



215
216
217
218
# File 'plugins/command/output.rb', line 215

def each_line sep
  raise IOError, "not opened for reading" if block_given?
  to_enum :each_line
end

- (nil) eof

As IO#eof

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



226
227
228
# File 'plugins/command/output.rb', line 226

def eof
  raise IOError, "not opened for reading"
end

- (nil) eof?

As IO#eof?

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



236
237
238
# File 'plugins/command/output.rb', line 236

def eof?
  raise IOError, "not opened for reading"
end

- (nil) external_encoding

As IO#external_encoding

Returns:

  • (nil)


245
246
247
# File 'plugins/command/output.rb', line 245

def external_encoding
  nil
end

- (Integer) fileno

As IO#fileno

Returns:

  • (Integer)

    the third argument passed to the constructor



254
255
256
# File 'plugins/command/output.rb', line 254

def fileno
  @fileno
end

- (Output) flush

As IO#flush

As this class doesn’t do any buffering, this method does nothing and is only provided for compatibility with IO API

Returns:



266
267
268
# File 'plugins/command/output.rb', line 266

def flush
  self
end

- (nil) getbyte

As IO#getbyte

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



276
277
278
# File 'plugins/command/output.rb', line 276

def getbyte
  raise IOError, "not opened for reading"
end

- (nil) getc

As IO#getc

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



286
287
288
# File 'plugins/command/output.rb', line 286

def getc
  raise IOError, "not opened for reading"
end

- (nil) gets

As IO#gets

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



296
297
298
# File 'plugins/command/output.rb', line 296

def gets
  raise IOError, "not opened for reading"
end

- (nil) internal_encoding

As IO#internal_encoding

Returns:

  • (nil)


305
306
307
# File 'plugins/command/output.rb', line 305

def internal_encoding
  nil
end

- (false) isatty Also known as: tty?

As IO#isatty

Returns:

  • (false)


314
315
316
# File 'plugins/command/output.rb', line 314

def isatty
  false
end

- (nil) lineno

As IO#lineno

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



325
326
327
# File 'plugins/command/output.rb', line 325

def lineno
  raise IOError, "not opened for reading"
end

- (nil) lineno=(val)

As IO#lineno=

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



335
336
337
# File 'plugins/command/output.rb', line 335

def lineno= val
  raise IOError, "not opened for reading"
end

- (Enumerator) lines

As IO#lines

Returns:

  • (Enumerator)

    an enumerator which calls #each_line



344
345
346
# File 'plugins/command/output.rb', line 344

def lines
  each_line
end

- (nil) pid

As IO#pid

Returns:

  • (nil)


353
354
355
# File 'plugins/command/output.rb', line 353

def pid
  nil
end

As IO#print

Parameters:

Returns:

  • (nil)


363
364
365
366
367
368
369
# File 'plugins/command/output.rb', line 363

def print *args
  args = [$_] if args.empty?
  args.each{|a| write a}
  STDOUT.write $\
  write $\ if $\
  nil
end

- (nil) printf(format_string, *args)

As IO#printf

Parameters:

  • format_string (String)

    the format string. See Kernel.sprintf

  • args (Array<Object>)

    the parameter to substitute in the format string

Returns:

  • (nil)


378
379
380
381
382
# File 'plugins/command/output.rb', line 378

def printf format_string, *args
  str = sprintf format_string, *args
  write str
  nil
end

- (Object) putc(obj)

As IO#putc

Parameters:

Returns:



390
391
392
393
394
395
# File 'plugins/command/output.rb', line 390

def putc obj
  if obj.is_a? Numeric then write obj.floor.chr
  else obj.to_s.each_char.to_a[0]
  end
  obj
end

- (nil) puts(*args)

As IO#puts

Parameters:

Returns:

  • (nil)


403
404
405
406
407
408
409
410
411
# File 'plugins/command/output.rb', line 403

def puts *args
  args << '' if args.empty?
  args.each do |a|
    a = a.to_s
    write a
    write "\n" unless a.end_with? "\n"
  end
  nil
end

- (nil) read(length, buffer = nil)

As IO#read

Parameters:

  • length (Integer)

    unused

  • buffer (String) (defaults to: nil)

    unused

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



421
422
423
# File 'plugins/command/output.rb', line 421

def read length, buffer = nil
  raise IOError, "not opened for reading"
end

- (nil) read_nonblock(max, outbuf = nil)

As IO#read_nonblock

Parameters:

  • max (Integer)

    unused

  • outbuf (String) (defaults to: nil)

    unused

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



433
434
435
# File 'plugins/command/output.rb', line 433

def read_nonblock max, outbuf = nil
  raise IOError, "not opened for reading"
end

- (nil) readbyte

As IO#readbyte

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



443
444
445
# File 'plugins/command/output.rb', line 443

def readbyte
  raise IOError, "not opened for reading"
end

- (nil) readchar

As IO#readchar

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



453
454
455
# File 'plugins/command/output.rb', line 453

def readchar
  raise IOError, "not opened for reading"
end

- (nil) readline(sep, limit)

As IO#readline

Parameters:

  • sep (String)

    unused

  • limit (Integer)

    unused

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



465
466
467
# File 'plugins/command/output.rb', line 465

def readline sep, limit
  raise IOError, "not opened for reading"
end

- (nil) readlines(sep, limit)

As IO#readlines

Parameters:

  • sep (String)

    unused

  • limit (Integer)

    unused

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



477
478
479
# File 'plugins/command/output.rb', line 477

def readlines sep, limit
  raise IOError, "not opened for reading"
end

- (nil) readpartial(maxlen, outbuf = nil)

As IO#readpartial

Parameters:

  • maxlen (Integer)

    unused

  • outbuf (String) (defaults to: nil)

    unused

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



489
490
491
# File 'plugins/command/output.rb', line 489

def readpartial maxlen, outbuf = nil
  raise IOError, "not opened for reading"
end

- (nil) reopen(arg1, arg2 = 'r')

As IO#reopen

Parameters:

  • a (String, IO)

    path or another IO. Unused

  • the (String)

    mode. Unused

Returns:

  • (nil)

Raises:

  • (RuntimeError)

    always



501
502
503
# File 'plugins/command/output.rb', line 501

def reopen arg1, arg2 = 'r'
  raise RuntimeError, "You can\'t reopen #{self.class}"
end

- (Object) set_encoding(*args)

As IO#set_encoding

This method does nothing. It’s only provided for compatibility with IO API

Parameters:

  • args (Array<Object>)

    the arguments. See IO#set_encoding



511
512
# File 'plugins/command/output.rb', line 511

def set_encoding *args
end

- (nil) stat

As IO#stat

Returns:

  • (nil)

    as this class isn’t associated with any file



519
520
521
# File 'plugins/command/output.rb', line 519

def stat
  nil
end

- (nil) sysread(num, outbuf = nil)

As IO#sysread

Parameters:

  • num (Integer)

    unused

  • outbuf (String) (defaults to: nil)

    unused

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



531
532
533
# File 'plugins/command/output.rb', line 531

def sysread num, outbuf = nil
  raise IOError, "not opened for reading"
end

- (Output) to_io

As IO#to_io

Returns:



540
541
542
# File 'plugins/command/output.rb', line 540

def to_io
  self
end

- (nil) ungetbyte(arg)

As IO#ungetbyte

Parameters:

  • arg (String, Integer)

    unused

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



551
552
553
# File 'plugins/command/output.rb', line 551

def ungetbyte arg
  raise IOError, "not opened for reading"
end

- (nil) ungetc(str)

As IO#ungetc

Parameters:

Returns:

  • (nil)

Raises:

  • (IOError)

    always because the stream is not open for reading



562
563
564
# File 'plugins/command/output.rb', line 562

def ungetc str
  raise IOError, "not opened for reading"
end

- (Integer) write(obj) Also known as: syswrite, write_nonblock

As IO#write

Parameters:

  • obj (Object)

    the object to write

Returns:

  • (Integer)

    the number of written bytes

Raises:

  • (IOError)

    if the stream has been closed



573
574
575
576
577
578
579
580
581
582
583
584
# File 'plugins/command/output.rb', line 573

def write obj
  if !@closed
    cur = @widget.text_cursor
    cur.move_position Qt::TextCursor::End
    text = obj.to_s
    format = cur.char_format
    format.foreground = @brush
    cur.insert_text text, format
    text.bytes.count
  else raise IOError, 'closed stream'
  end
end