Top Level Namespace

Defined Under Namespace

Modules: Jota Classes: AppError, Class, Clip, ClipArray, Gui, Immediate, JotaCurses, Preferences, Version

Instance Method Summary collapse

Instance Method Details

#debug_fileObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/helper.rb', line 46

def debug_file
  if $debug_file.nil? then
    begin
      if $debug_filename then
        $debug_file = File.open($debug_filename,"a")
        print_verbose "logging to #{$debug_filename}"
      else
        $debug_file = $stdout
        print_verbose "logging to stdout"
      end
    rescue Exception => msg
      $debug_file = $stdout
      print_verbose "logging to '#{$debug_filename}' "+
              "not possible: #{msg}, using stdout"
    end

  end
  return $debug_file
end

#dump_objectspaceObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/helper.rb', line 120

def dump_objectspace
  GC.start
  sleep 60

  count = Hash.new 0
  ObjectSpace.each_object do |o|
    count[o.class] += 1
  end

  File.open "objectspace-#{Time.now.iso8601}","w" do |f|
    f.puts "ObjectSpace.memsize_of_all = #{ObjectSpace.memsize_of_all.to_s}"
    f.puts "ps -o rss = #{`ps -o rss= -p #{Process.pid}`.to_i} kB"
    f.puts

    count.keys.sort.each do |k|
      f.puts "#{k} #{count[k]}"
    end
  end
end

#editor(str) ⇒ Object



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
# File 'lib/helper.rb', line 82

def editor(str)
  file = Tempfile.new("jota")
  filename = file.path
  print_verbose "creating temporary file '#{filename}'"

  file.puts str
  file.close

  if ENV["EDITOR"] then
    cmdline = ENV["EDITOR"]
  else
    cmdline = "vi"
  end
  cmdline = "%s %s" % [cmdline, filename]
  print_verbose "executing '#{cmdline}'"

  if system(cmdline) then
    file = File.open(filename,"r")
    lines = file.readlines.join("")
    file.close
  else
    puts "Error executing '#{cmdline}' with exit code #{$?}"
    lines = str
  end

  File.unlink(filename)

  return lines
end

#escape_from(str) ⇒ Object



5
6
7
8
# File 'lib/helper.rb', line 5

def escape_from(str)
  str.gsub!(/\n(>*)From /,"\n\\1>From ")
  return str
end

#expand_filename(str, dirname, filename) ⇒ Object

expand strftime() time in str and expand %s to ‘filename’



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/helper.rb', line 17

def expand_filename(str, dirname, filename)
        # test on $RUBY_PLATFORM is less reliable, can be mswin, mingw, java

        if ENV["HOMEDRIVE"] then
                homedir = ENV["HOMEDRIVE"]+ENV["HOMEPATH"]
        else
                homedir = ENV["HOME"]
        end
  result = Time.now.strftime(str)
  result = result.gsub(/\$f/,filename)
  result = result.gsub(/\$p/,Process::pid.to_s)
  result = result.gsub(/\$h/,homedir)
  result = result.gsub(/\$\$/,"$")
  print_debug "expanding '#{filename}' to '#{result}'"
  return result
end

#expand_tabs(str) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/helper.rb', line 33

def expand_tabs(str)
        # see http://www.rootr.net/rubyfaq-9.html, Sec. 9.17


        # The pack-A version does not work, because A expects a binary/ASCII

        # string, not a Unicode string


        #return str.gsub(/([^\t]{8})|([^\t]*)\t/n){[$+].pack("A8")}


        s = str.clone
        while s.sub!(/(^[^\t]*)\t(\t*)/){$1+' '*(8-$1.size%8+8*$2.size)};end
        return s
end


74
75
76
77
78
79
80
# File 'lib/helper.rb', line 74

def print_debug(msg)
  if $DEBUG then
    # do not write to $debug_file, this can be nil

    debug_file.puts "#{Process.pid}-2# #{msg}"
    debug_file.flush
  end
end


66
67
68
69
70
71
72
# File 'lib/helper.rb', line 66

def print_verbose(msg)
  if $VERBOSE then
    # do not write to $debug_file, this can be nil

    debug_file.puts "#{Process.pid}-1# #{msg}"
    debug_file.flush
  end
end

#unescape_from(str) ⇒ Object



10
11
12
13
# File 'lib/helper.rb', line 10

def unescape_from(str)
  str.gsub!(/\n(>*)>From /,"\n\\1From ")
  return str
end