Class: Terminal

Inherits:
Object show all
Defined in:
lib/ruiby_gtk/ruiby_terminal.rb

Instance Method Summary collapse

Constructor Details

#initialize(tv, win) ⇒ Terminal

Returns a new instance of Terminal.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 7

def initialize(tv,win)
  @tv=tv
  #@tv.override_font(  Pango::FontDescription.new("courier bold 10")) 
  @tb=tv.buffer
  @win=win
  def tv.terminal(term=nil) @term ? @term : (@term=term) end
  tv.terminal(self)
  @@history||= ["nop"]
  @history=@@history
  @position=@history.size-1
  append_and_prompt ""
end

Instance Method Details

#append(text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 29

def append(text)
  text=text.to_s
  buf_start, end_iter = @tb.bounds
  @tb.insert(end_iter,text)
  insert=@tb.create_mark("insert", @tb.bounds.last, true)
  @win.after(100) {
    vscroll=@tv.parent.vadjustment
    vscroll.value = vscroll.upper+8
  }
end

#append_and_prompt(text) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 19

def append_and_prompt(text)
  text=text.to_s
  buf_start, end_iter = @tb.bounds
  @tb.insert(end_iter,"\n#{text}\n> ")
  insert=@tb.create_mark("insert", @tb.bounds.last, true)
  @win.after(100) {
    vscroll=@tv.parent.vadjustment
    vscroll.value = vscroll.upper+8
  }
end

#execute(line = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 59

def execute(line=nil)
  line||=get_line
  line=line.strip
  return if line.size==0
  
  @history << line if line!= @history.last
  @position= @history.size
  cmd,*args=line.split(/\s+/)
  case cmd 
    when /^vim?$/
      system("vim",*args)
      append_and_prompt ""
    when "clear","clr","cls"
      @tb.text=""
      append_and_prompt ""        
    when "tree"
      @win.wtree(@win)
      append_and_prompt ""
    when "help"
      append_and_prompt <<-EEND
        help                       : show this message
        vim filename               : edit file
        clear (alias clr or cls)   : clear console
        tree                       : print widget tree of main window in log
        h [number]                 : show history commands / recal command
        show object                : show methods of object in log window
        echo params...             : evaluate params in main window and print values
        techo params...            : (debug terminal) evaluate in terminal object and print
        os commands (ls,find,perl...)
        exit                       : destroy this terminal
        ; see ruiby_terminal.rb for append some commands :)
      EEND
    when "h","history"
      if args.size==0
        s=@history.size
        append_and_prompt @history.each_with_index.map {|l,i| 
           "   %3d >%s" % [s-i,l]
          }.last(40).join("\n")
      else
        cmd=@history[-args.first.to_i-1]
        append_and_prompt "<<#{cmd}>>"
        execute cmd
      end
    when "show"
      @win.show_methods( @win.instance_eval(args.first) )
      append_and_prompt ""
    when "echo"
      append_and_prompt( @win.instance_eval(args.join(" ")))
    when "techo"
      append_and_prompt( self.instance_eval(args.join(" ")))
    when "exit"
      @tv.parent.parent.parent.parent.close
    when /^(ls|ll|mv|grep|find|gfind|awk|ruby|sh|python|perl|julia)$/
      if @win.ask("execute <<#{line}>>  ?")
        append ""
        Open3.popen3(*([cmd]+args)) do |i,o,e|
          while q=IO.select([o]) 
            append s=q.first.first.read
            break if !s || s.size==0
            Ruiby.update
          end
        end
        append_and_prompt(">>> end execute.")
      end
    else
      begin
        res=@win.instance_eval(line)
        append_and_prompt(res.to_s)
      end
  end
end

#get_history(n = -1)) ⇒ Object



47
48
49
50
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 47

def get_history(n=-1)
  @position= [0,@history.size-1,@position+n].sort[1]
  @history[@position]
end

#get_lineObject



55
56
57
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 55

def get_line()
  (@tb.text||"").split(/\r?\n/).last.gsub(/^\s*>\s*/,"")
end

#replace_current(text) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 39

def replace_current(text)
  l = @tb.line_count
  start = @tb.get_iter_at(line: l-1)  
  eend = @tb.end_iter()
  @tb.delete(start,eend)    
  append(text)
end

#set_history(n = -1)) ⇒ Object



51
52
53
54
# File 'lib/ruiby_gtk/ruiby_terminal.rb', line 51

def set_history(n=-1)
  text=get_history(n)
  replace_current(text)
end