Class: TkTextIO

Inherits:
TkText show all
Defined in:
sample/tktextio.rb

Constant Summary collapse

OPT_DEFAULTS =
{
  'mode'       => nil,
  'overwrite'  => false,
  'text'       => nil,
  'show'       => :pos,
  'wrap'       => 'char',
  'sync'       => true,
  'prompt'     => nil,
  'prompt_cmd' => nil,
  'hist_size'  => 1000,
}
@@create_queues =

keep safe level

proc{ [Queue.new, Mutex.new, Queue.new, Mutex.new] }

Instance Method Summary collapse

Instance Method Details

#<<(obj) ⇒ Object



398
399
400
401
# File 'sample/tktextio.rb', line 398

def <<(obj)
  _write(obj)
  self
end

#binmodeObject



403
404
405
# File 'sample/tktextio.rb', line 403

def binmode
  self
end

#cloneObject



407
408
409
# File 'sample/tktextio.rb', line 407

def clone
  fail NotImplementedError, 'cannot clone TkTextIO'
end

#closeObject



414
415
416
417
418
# File 'sample/tktextio.rb', line 414

def close
  close_read
  close_write
  nil
end

#close_readObject



419
420
421
422
# File 'sample/tktextio.rb', line 419

def close_read
  @open[:r] = false if @open[:r]
  nil
end

#close_writeObject



423
424
425
426
# File 'sample/tktextio.rb', line 423

def close_write
  @open[:w] = false if @opne[:w]
  nil
end

#closed?(dir = nil) ⇒ Boolean

Returns:

  • (Boolean)


428
429
430
431
432
433
434
435
436
437
# File 'sample/tktextio.rb', line 428

def closed?(dir=nil)
  case dir
  when :r, 'r'
    !@open[:r]
  when :w, 'w'
    !@open[:w]
  else
    !@open[:r] && !@open[:w]
  end
end

#destroyObject



76
77
78
79
80
81
82
83
84
85
# File 'sample/tktextio.rb', line 76

def destroy
  @flusher.kill rescue nil

  @idle_flush.stop rescue nil
  @timer_flush.stop rescue nil

  @receiver.kill rescue nil

  super()
end

#dupObject



410
411
412
# File 'sample/tktextio.rb', line 410

def dup
  fail NotImplementedError, 'cannot duplicate TkTextIO'
end

#each_charObject Also known as: each_byte



458
459
460
461
462
463
464
# File 'sample/tktextio.rb', line 458

def each_char
  _check_readable
  while(c = self.getc)
    yield(c)
  end
  self
end

#each_line(rs = $/) ⇒ Object Also known as: each



449
450
451
452
453
454
455
# File 'sample/tktextio.rb', line 449

def each_line(rs = $/)
  _check_readable
  while(s = self.gets(rs))
    yield(s)
  end
  self
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


467
468
469
# File 'sample/tktextio.rb', line 467

def eof?
  compare(@txtpos, '==', 'end - 1 char')
end

#fcntl(*args) ⇒ Object



472
473
474
# File 'sample/tktextio.rb', line 472

def fcntl(*args)
  fail NotImplementedError, "fcntl is not implemented on #{self.class}"
end

#filenoObject



480
481
482
# File 'sample/tktextio.rb', line 480

def fileno
  nil
end

#flushObject



484
485
486
487
488
489
490
491
492
493
# File 'sample/tktextio.rb', line 484

def flush
  Thread.pass
  if @open[:w] || ! @write_buffer.empty?
    @write_buf_mutex.synchronize {
      _sync_write_buf(@write_buffer)
      @write_buffer[0..-1] = ''
    }
  end
  self
end

#fsyncObject



476
477
478
# File 'sample/tktextio.rb', line 476

def fsync
  0
end

#getcObject



495
496
497
498
499
500
501
502
503
504
# File 'sample/tktextio.rb', line 495

def getc
  return _block_read(1) if @console_mode

  _check_readable
  return nil if eof?
  c = get(@txtpos)
  @txtpos.set(@txtpos + '1 char')
  _see_pos
  c
end

#gets(rs = $/) ⇒ Object



506
507
508
509
510
511
512
# File 'sample/tktextio.rb', line 506

def gets(rs = $/)
  return _block_read(rs) if @console_mode

  _check_readable
  return nil if eof?
  _readline(rs)
end

#index_posObject Also known as: tell_index



546
547
548
# File 'sample/tktextio.rb', line 546

def index_pos
  index(@txtpos)
end

#index_pos=(idx) ⇒ Object



551
552
553
554
555
556
# File 'sample/tktextio.rb', line 551

def index_pos=(idx)
  @txtpos.set(idx)
  @txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
  _see_pos
  idx
end

#ioctrl(*args) ⇒ Object



514
515
516
# File 'sample/tktextio.rb', line 514

def ioctrl(*args)
  fail NotImplementedError, 'iocntl is not implemented on TkTextIO'
end

#isattyObject



518
519
520
# File 'sample/tktextio.rb', line 518

def isatty
  false
end

#linenoObject



525
526
527
# File 'sample/tktextio.rb', line 525

def lineno
  @lineno + @line_offset
end

#lineno=(num) ⇒ Object



529
530
531
532
# File 'sample/tktextio.rb', line 529

def lineno=(num)
  @line_offset = num - @lineno
  num
end

#overwrite=(ovwt) ⇒ Object



538
539
540
# File 'sample/tktextio.rb', line 538

def overwrite=(ovwt)
  @overwrite = (ovwt)? true: false
end

#overwrite?Boolean

Returns:

  • (Boolean)


534
535
536
# File 'sample/tktextio.rb', line 534

def overwrite?
  @overwrite
end

#pidObject



542
543
544
# File 'sample/tktextio.rb', line 542

def pid
  nil
end

#posObject Also known as: tell



558
559
560
561
# File 'sample/tktextio.rb', line 558

def pos
  s = get('1.0', @txtpos)
  number(tk_call('string', 'length', s))
end

#pos=(idx) ⇒ Object



564
565
566
567
# File 'sample/tktextio.rb', line 564

def pos=(idx)
  seek(idx, IO::SEEK_SET)
  idx
end

#pos_gravityObject



569
570
571
# File 'sample/tktextio.rb', line 569

def pos_gravity
  @txtpos.gravity
end

#pos_gravity=(side) ⇒ Object



573
574
575
576
# File 'sample/tktextio.rb', line 573

def pos_gravity=(side)
  @txtpos.gravity = side
  side
end


578
579
580
581
582
583
584
585
586
# File 'sample/tktextio.rb', line 578

def print(arg=$_, *args)
  _check_writable
  args.unshift(arg)
  args.map!{|val| (val == nil)? 'nil': val.to_s }
  str = args.join($,)
  str << $\ if $\
  _write(str)
  nil
end

#printf(*args) ⇒ Object



587
588
589
590
591
# File 'sample/tktextio.rb', line 587

def printf(*args)
  _check_writable
  _write(sprintf(*args))
  nil
end

#putc(c) ⇒ Object



593
594
595
596
597
598
# File 'sample/tktextio.rb', line 593

def putc(c)
  _check_writable
  c = c.chr if c.kind_of?(Fixnum)
  _write(c)
  c
end

#puts(*args) ⇒ Object



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'sample/tktextio.rb', line 600

def puts(*args)
  _check_writable
  if args.empty?
    _write("\n")
    return nil
  end
  args.each{|arg|
    if arg == nil
      _write("nil\n")
    elsif arg.kind_of?(Array)
      puts(*arg)
    elsif arg.kind_of?(String)
      _write(arg.chomp)
      _write("\n")
    else
      begin
        arg = arg.to_ary
        puts(*arg)
      rescue
        puts(arg.to_s)
      end
    end
  }
  nil
end

#read(len = nil, buf = nil) ⇒ Object



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'sample/tktextio.rb', line 636

def read(len=nil, buf=nil)
  return _block_read(len, buf) if @console_mode

  _check_readable
  if len
    return "" if len == 0
    return nil if eof?
    s = _read(len)
  else
    s = get(@txtpos, 'end - 1 char')
    @txtpos.set('end - 1 char')
    _see_pos
  end
  buf.replace(s) if buf.kind_of?(String)
  s
end

#readcharObject



653
654
655
656
657
658
659
660
661
662
# File 'sample/tktextio.rb', line 653

def readchar
  return _block_read(1) if @console_mode

  _check_readable
  fail EOFError if eof?
  c = get(@txtpos)
  @txtpos.set(@txtpos + '1 char')
  _see_pos
  c
end

#readline(rs = $/) ⇒ Object



699
700
701
702
703
704
705
# File 'sample/tktextio.rb', line 699

def readline(rs = $/)
  return _block_readline(rs) if @console_mode

  _check_readable
  fail EOFError if eof?
  _readline(rs)
end

#readlines(rs = $/) ⇒ Object



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'sample/tktextio.rb', line 707

def readlines(rs = $/)
  if @console_mode
    lines = []
    while (line = _block_readline(rs))
      lines << line
    end
    return lines
  end

  _check_readable
  lines = []
  until(eof?)
    lines << _readline(rs)
  end
  $_ = nil
  lines
end

#readpartial(maxlen, buf = nil) ⇒ Object



725
726
727
728
729
730
731
732
733
734
# File 'sample/tktextio.rb', line 725

def readpartial(maxlen, buf=nil)
  #return @console_buffer.readpartial(maxlen, buf) if @console_mode
  return _block_read(maxlen, buf, nil) if @console_mode

  _check_readable
  fail EOFError if eof?
  s = _read(maxlen)
  buf.replace(s) if buf.kind_of?(String)
  s
end

#reopen(*args) ⇒ Object



736
737
738
# File 'sample/tktextio.rb', line 736

def reopen(*args)
  fail NotImplementedError, 'reopen is not implemented on TkTextIO'
end

#rewindObject



740
741
742
743
744
745
746
# File 'sample/tktextio.rb', line 740

def rewind
  @txtpos.set('1.0')
  _see_pos
  @lineno = 0
  @line_offset = 0
  self
end

#seek(offset, whence = IO::SEEK_SET) ⇒ Object Also known as: sysseek



748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'sample/tktextio.rb', line 748

def seek(offset, whence=IO::SEEK_SET)
  case whence
  when IO::SEEK_SET
    offset = "1.0 + #{offset} char" if offset.kind_of?(Numeric)
    @txtpos.set(offset)

  when IO::SEEK_CUR
    offset = "#{offset} char" if offset.kind_of?(Numeric)
    @txtpos.set(@txtpos + offset)

  when IO::SEEK_END
    offset = "#{offset} char" if offset.kind_of?(Numeric)
    @txtpos.set("end - 1 char + #{offset}")

  else
    fail Errno::EINVAL, 'invalid whence argument'
  end

  @txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end)
  _see_pos

  0
end

#show_modeObject



778
779
780
# File 'sample/tktextio.rb', line 778

def show_mode
  (@show == @txtpos)? :pos : @show
end

#show_mode=(mode) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'sample/tktextio.rb', line 782

def show_mode=(mode)
  # define show mode  when file position is changed.
  #  mode == :pos or "pos" or true :: see current file position.
  #  mode == :insert or "insert"   :: see insert cursor position.
  #  mode == nil or false          :: do nothing
  #  else see 'mode' position ('mode' should be text index or mark)
  case mode
  when :pos, 'pos', true
    @show = @txtpos
  when :insert, 'insert'
    @show = :insert
  when nil, false
    @show = false
  else
    begin
      index(mode)
    rescue
      fail ArgumentError, 'invalid show-position'
    end
    @show = mode
  end

  _see_pos

  mode
end

#statObject



809
810
811
# File 'sample/tktextio.rb', line 809

def stat
  fail NotImplementedError, 'stat is not implemented on TkTextIO'
end

#syncObject



813
814
815
# File 'sample/tktextio.rb', line 813

def sync
  @sync
end

#sync=(mode) ⇒ Object



817
818
819
# File 'sample/tktextio.rb', line 817

def sync=(mode)
  @sync = mode
end

#sysread(len, buf = nil) ⇒ Object



821
822
823
824
825
826
827
828
829
# File 'sample/tktextio.rb', line 821

def sysread(len, buf=nil)
  return _block_read(len, buf) if @console_mode

  _check_readable
  fail EOFError if eof?
  s = _read(len)
  buf.replace(s) if buf.kind_of?(String)
  s
end

#syswrite(obj) ⇒ Object



831
832
833
# File 'sample/tktextio.rb', line 831

def syswrite(obj)
  _write(obj)
end

#to_ioObject



835
836
837
# File 'sample/tktextio.rb', line 835

def to_io
  self
end

#trancate(len) ⇒ Object



839
840
841
842
# File 'sample/tktextio.rb', line 839

def trancate(len)
  delete("1.0 + #{len} char", :end)
  0
end

#tty?Boolean

Returns:

  • (Boolean)


521
522
523
# File 'sample/tktextio.rb', line 521

def tty?
  false
end

#ungetc(c) ⇒ Object



844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'sample/tktextio.rb', line 844

def ungetc(c)
  if @console_mode
    @read_buf_mutex.synchronize {
      @read_buffer[0,0] = c.chr
    }
    return nil
  end

  _check_readable
  c = c.chr if c.kind_of?(Fixnum)
  if compare(@txtpos, '>', '1.0')
    @txtpos.set(@txtpos - '1 char')
    delete(@txtpos)
    insert(@txtpos, tk_call('string', 'range', c, 0, 1))
    @txtpos.set(@txtpos - '1 char') if @txtpos.gravity == 'right'
    _see_pos
  else
    fail IOError, 'cannot ungetc at head of stream'
  end
  nil
end

#write(obj) ⇒ Object

end



912
913
914
915
# File 'sample/tktextio.rb', line 912

def write(obj)
  _check_writable
  _write(obj)
end