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



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

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

#binmodeObject



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

def binmode
  self
end

#cloneObject



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

def clone
  fail NotImplementedError, 'cannot clone TkTextIO'
end

#closeObject



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

def close
  close_read
  close_write
  nil
end

#close_readObject



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

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

#close_writeObject



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

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

#closed?(dir = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#destroyObject



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

def destroy
  @flusher.kill rescue nil

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

  @receiver.kill rescue nil

  super()
end

#dupObject



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

def dup
  fail NotImplementedError, 'cannot duplicate TkTextIO'
end

#each_charObject Also known as: each_byte



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

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

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



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

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

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


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

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

#fcntl(*args) ⇒ Object



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

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

#filenoObject



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

def fileno
  nil
end

#flushObject



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

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



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

def fsync
  0
end

#getcObject



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

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



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

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



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

def index_pos
  index(@txtpos)
end

#index_pos=(idx) ⇒ Object



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

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

#ioctrl(*args) ⇒ Object



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

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

#isattyObject



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

def isatty
  false
end

#linenoObject



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

def lineno
  @lineno + @line_offset
end

#lineno=(num) ⇒ Object



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

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

#overwrite=(ovwt) ⇒ Object



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

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

#overwrite?Boolean

Returns:

  • (Boolean)


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

def overwrite?
  @overwrite
end

#pidObject



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

def pid
  nil
end

#posObject Also known as: tell



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

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

#pos=(idx) ⇒ Object



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

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

#pos_gravityObject



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

def pos_gravity
  @txtpos.gravity
end

#pos_gravity=(side) ⇒ Object



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

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


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

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



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

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

#putc(c) ⇒ Object



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

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

#puts(*args) ⇒ Object



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

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



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

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



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

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



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

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

  _check_readable
  fail EOFError if eof?
  _readline(rs)
end

#readlines(rs = $/) ⇒ Object



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

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



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

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



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

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

#rewindObject



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

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



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

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



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

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

#show_mode=(mode) ⇒ Object



781
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
# File 'sample/tktextio.rb', line 781

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



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

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

#syncObject



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

def sync
  @sync
end

#sync=(mode) ⇒ Object



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

def sync=(mode)
  @sync = mode
end

#sysread(len, buf = nil) ⇒ Object



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

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



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

def syswrite(obj)
  _write(obj)
end

#to_ioObject



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

def to_io
  self
end

#trancate(len) ⇒ Object



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

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

#tty?Boolean

Returns:

  • (Boolean)


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

def tty?
  false
end

#ungetc(c) ⇒ Object



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

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



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

def write(obj)
  _check_writable
  _write(obj)
end