Top Level Namespace
Defined Under Namespace
Classes: Array, Entry, Format, Integer, Parse, String, Time, Validate, Yornal
Instance Method Summary
collapse
Instance Method Details
#defbin(x, fallback: []) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/misc.rb', line 42
def defbin(x, fallback: [])
define_method(x) do
binaries = fallback
path = ENV["PATH"].split(":")
ENV[x.to_s.upcase] or
binaries.find { |b| path.any? { |p| File.exist? "#{p}/#{b}" } } or "cat"
end
end
|
#die(status = 0) ⇒ Object
18
19
20
21
|
# File 'lib/misc.rb', line 18
def die(status = 0)
yield if block_given?
exit(status)
end
|
#err(message, *args) ⇒ Object
23
24
25
26
|
# File 'lib/misc.rb', line 23
def err(message, *args)
$stderr.printf "Error: #{message} \n", *args
die 1
end
|
#git(*command) ⇒ Object
66
67
68
|
# File 'lib/misc.rb', line 66
def git(*command)
system command.insert(0, "git").join(" ")
end
|
#mkdir(path) ⇒ Object
54
55
56
57
|
# File 'lib/misc.rb', line 54
def mkdir(path)
system "mkdir -p #{path} > /dev/null" or
err("could not create directory '%s'", path)
end
|
#touch(path) ⇒ Object
59
60
61
62
63
|
# File 'lib/misc.rb', line 59
def touch(path)
spath = path.split "/"
mkdir(spath[..-2].join("/")) if spath.size > 1
File.open(path, "w") { }
end
|
#tree(dir) ⇒ Object
36
37
38
39
40
|
# File 'lib/misc.rb', line 36
def tree(dir)
return [dir] if !(File.directory? dir) || dir =~ /\.git/
Dir.children(dir).map { |f| tree [dir, f].join("/") }.flatten
end
|
#yes_or_no?(question, pre = nil, post = nil) ⇒ Boolean
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/misc.rb', line 3
def yes_or_no?(question, pre = nil, post = nil)
puts pre if pre
loop do
print "#{question} (yes|no): "
answer = $stdin.gets&.chomp&.downcase
die 1 if answer.nil?
if %w[yes y no n].any?(answer) || answer.empty?
puts post if post
return %w[yes y].any?(answer)
else
puts "Please enter 'yes', 'y', 'n', or 'no'."
end
end
end
|
#yornal_depth(dir) ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/misc.rb', line 28
def yornal_depth(dir)
return 0 unless File.directory? dir
Dir.children(dir)
.delete_if { |i| i =~ /\D/ }
.map { |i| 1 + yornal_depth([dir, i].join("/")) }.max or 1
end
|