jota 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/helper.rb ADDED
@@ -0,0 +1,129 @@
1
+
2
+ # $Id: helper.rb 37 2009-02-09 10:06:02Z dz $
3
+
4
+ # if you use vim and don't like folds type zR
5
+
6
+ def get_filename(filename, mode)
7
+ #{{{1
8
+ if filename.class == Symbol and filename == :ask then
9
+ case mode
10
+ when :save
11
+ action = Gtk::FileChooser::ACTION_SAVE
12
+ ok_button = Gtk::Stock::SAVE
13
+ when :open
14
+ action = Gtk::FileChooser::ACTION_OPEN
15
+ ok_button = Gtk::Stock::OPEN
16
+ else
17
+ raise "Wrong mode #{mode} in helper:get_filenamen"
18
+ end
19
+
20
+ title = "Select Filename"
21
+
22
+ dialog = Gtk::FileChooserDialog.new(
23
+ title,
24
+ nil,
25
+ action,
26
+ nil,
27
+ [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
28
+ [ok_button, Gtk::Dialog::RESPONSE_ACCEPT])
29
+
30
+ case dialog.run
31
+ when Gtk::Dialog::RESPONSE_ACCEPT
32
+ filename = dialog.filename
33
+ else
34
+ filename = nil
35
+ end
36
+ dialog.destroy
37
+ end
38
+
39
+ if filename.nil? then
40
+ return
41
+ end
42
+
43
+ filename = Time.now.strftime(filename)
44
+
45
+ return filename
46
+ end #}}}1
47
+
48
+
49
+ def array_to_color(arr)
50
+ #{{{1
51
+ return Gdk::Color.new(arr[0],arr[1],arr[2])
52
+ end #}}}1
53
+
54
+ def color_to_array(gdk_color)
55
+ #{{{1
56
+ return [gdk_color.red, gdk_color.green, gdk_color.blue]
57
+ end #}}}1
58
+
59
+ def escape_from(str)
60
+ #{{{1
61
+ str.gsub!(/\n(>*)From /,"\n\\1>From ")
62
+ return str
63
+ end #}}}1
64
+
65
+ def unescape_from(str)
66
+ #{{{1
67
+ str.gsub!(/\n(>*)>From /,"\n\\1From ")
68
+ return str
69
+ end #}}}1
70
+
71
+ # expand strftime() time in str and expand %s to 'filename'
72
+ def expand_filename(str, dirname, filename)
73
+ #{{{1
74
+ result = Time.now.strftime(str)
75
+ result = result.gsub(/\$f/,filename)
76
+ result = result.gsub(/\$d/,dirname)
77
+ result = result.gsub(/\$\$/,"$")
78
+ return result
79
+ end #}}}1
80
+
81
+ #
82
+ # Locking
83
+ #
84
+
85
+ def obtain_shared_lock(file)
86
+ #{{{1
87
+ release_lock(file) # necessary for win32
88
+
89
+ # a shared, non-blocking lock
90
+ print_debug "obtaining SHARED LOCK on '#{file.path}'"
91
+ if not file.flock(File::LOCK_SH|File::LOCK_NB) then
92
+ print_debug " cannot lock"
93
+ #file.flock(File::LOCK_SH) TODO
94
+ end
95
+ end #}}}1
96
+
97
+ def obtain_exclusive_lock(file)
98
+ #{{{1
99
+ release_lock(file) # necessary for win32
100
+
101
+ # an exclusive, non-blocking lock
102
+ print_debug "obtaining EXCLUSIVE LOCK on '#{file.path}'"
103
+ if not file.flock(File::LOCK_EX|File::LOCK_NB) then
104
+ print_debug " cannot lock"
105
+ #file.flock(File::LOCK_EX) TODO
106
+ end
107
+ end #}}}1
108
+
109
+ def release_lock(file)
110
+ #{{{1
111
+ print_debug "releasing LOCK from '#{file.path}'"
112
+ file.flock(File::LOCK_UN)
113
+ end #}}}1
114
+
115
+ def print_verbose(msg)
116
+ #{{{1
117
+ if $VERBOSE then
118
+ puts "#{Process.pid}# #{msg}"
119
+ end
120
+ end #}}}1
121
+
122
+ def print_debug(msg)
123
+ #{{{1
124
+ if $DEBUG then
125
+ puts "#{Process.pid}- #{msg}"
126
+ end
127
+ end #}}}1
128
+
129
+ # vim600: set foldmethod=marker: