Top Level Namespace
Defined Under Namespace
Modules: CLI, Clir, InputsTTYMethods, ReplayedTTYMethods, TTY Classes: Array, CSV, File, Integer, Labelizor, String, Symbol, Time
Constant Summary collapse
- Config =
Expose outside
Clir::Configuration.new
- MOIS =
{ 1 => {court: 'jan', long: 'janvier'}, 2 => {court: 'fév', long: 'février'}, 3 => {court: 'mars', long: 'mars'}, 4 => {court: 'avr', long: 'avril'}, 5 => {court: 'mai', long: 'mai'}, 6 => {court: 'juin', long: 'juin'}, 7 => {court: 'juil', long: 'juillet'}, 8 => {court: 'aout', long: 'aout'}, 9 => {court: 'sept', long: 'septembre'}, 10 => {court: 'oct', long: 'octobre'}, 11 => {court: 'nov', long: 'novembre'}, 12 => {court: 'déc', long: 'décembre'} }
- DAYNAMES =
[ 'Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi' ]
- Q =
/module InputsTTYMethods
TTY::MyPrompt.new(symbols: {radio_on:"☒", radio_off:"☐"})
Instance Method Summary collapse
-
#clear ⇒ Object
To wach (empty) the console.
-
#clip(ca, silent = false) ⇒ Object
To copy in the clipboard.
-
#console_height ⇒ Object
Lines count of the console.
-
#console_width ⇒ Object
Column count of the console.
-
#date_for_file(time = nil, with_hour = false, del = '-') ⇒ Object
A date for a file, now.
-
#date_from(foo) ⇒ Object
(also: #time_from)
Date corresponding to
foo
. - #debug? ⇒ Boolean
-
#delete_if_exist(pth) ⇒ Object
To delete (unlink) folder of file if it exists.
-
#formate_date(date, options = nil) ⇒ Object
Formate de date as JJ MM AAAA (or MM JJ AAAA in english).
- #help? ⇒ Boolean
-
#human_date(ladate = nil, **options) ⇒ String
(also: #date_humaine)
Une date formatée avec le moins verbal.
-
#ilya(laps, options = nil) ⇒ Object
/class Integer.
- #label_value_line(label, value, params = nil) ⇒ Object
-
#labelize(ary, params = nil) ⇒ Object
Pour “labeliser” une table de label <> valeur.
-
#less(texte) ⇒ Object
Use ‘less’ command to display
texte
. -
#mkdir(pth) ⇒ Object
(also: #mkdir_p)
Like ‘mkdir -p’ command.
-
#pcent(value, with_cents = nil) ⇒ String
Value
value
as pourcentage. -
#require_folder(dossier) ⇒ Object
Require all ruby file deep in folder
dossier
. -
#round(n, decim = 2) ⇒ Object
To round a number.
- #test? ⇒ Boolean
- #true_or_false(value) ⇒ Object
- #verbose? ⇒ Boolean
- #version? ⇒ Boolean
-
#write_at(str, line, column) ⇒ Object
Write
str
at columncolumn
and lineline
. -
#ymd(time = nil, delimitor = '-') ⇒ Object
@reçoit une date et la retourne sous la forme “YYYY-MM-DD”.
-
#€(value, with_cents = false) ⇒ String
La valeur exprimée en euro.
Instance Method Details
#clear ⇒ Object
To wach (empty) the console
8 9 10 11 |
# File 'lib/clir/console_methods.rb', line 8 def clear # puts "\n" # pour certaines méthodes STDOUT.write "\n\033c" end |
#clip(ca, silent = false) ⇒ Object
To copy in the clipboard
31 32 33 34 |
# File 'lib/clir/utils_methods.rb', line 31 def clip(ca, silent = false) `printf "#{ca.gsub(/"/, '\\"').strip}" | pbcopy` silent || puts("\n(“#{ca}“ copié dans le presse-papier)".gris) end |
#console_height ⇒ Object
Returns lines count of the console.
30 31 32 |
# File 'lib/clir/console_methods.rb', line 30 def console_height `tput lines`.strip.to_i end |
#console_width ⇒ Object
Returns column count of the console.
25 26 27 |
# File 'lib/clir/console_methods.rb', line 25 def console_width `tput cols`.strip.to_i end |
#date_for_file(time = nil, with_hour = false, del = '-') ⇒ Object
Returns A date for a file, now.
49 50 51 52 53 54 |
# File 'lib/clir/Date_utils.rb', line 49 def date_for_file(time = nil, with_hour = false, del = '-') time ||= Time.now fmt = "%Y#{del}%m#{del}%d" fmt = "#{fmt}#{del}%H#{del}%M" if with_hour time.strftime(fmt) end |
#date_from(foo) ⇒ Object Also known as: time_from
Alias :time_from (more semanticaly correct)
Returns Date corresponding to foo
.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/clir/Date_utils.rb', line 72 def date_from(foo) case foo when Time then foo when Date then foo.to_time when Integer then Time.at(foo) when String a, b, c = foo.split('/') if c.length == 4 Time.new(c.to_i, b.to_i, a.to_i) else Time.new(a.to_i, b.to_i, c.to_i) end else raise "Unable to transform #{foo.inspect} to Time." end end |
#debug? ⇒ Boolean
7 8 9 |
# File 'lib/clir/state_methods.rb', line 7 def debug? Clir::State.debug? end |
#delete_if_exist(pth) ⇒ Object
To delete (unlink) folder of file if it exists
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/clir/utils_methods.rb', line 41 def delete_if_exist(pth) if File.exist?(pth) if File.directory?(pth) FileUtils.rm_rf(pth) else File.delete(pth) end return not(File.exist?(pth)) else false end end |
#formate_date(date, options = nil) ⇒ Object
Formate de date as JJ MM AAAA (or MM JJ AAAA in english)
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 |
# File 'lib/clir/Date_utils.rb', line 100 def formate_date(date, = nil) ||= {} @last_format = nil if [:update_format] || [:template] @last_format ||= begin as_verbal = [:verbal]||[:sentence] if [:template] [:template] else fmt = [] fmt << 'le ' if [:sentence] if as_verbal forday = date.day == 1 ? '1er' : '%-d' fmt << "#{forday} #{MOIS[date.month][:long]} %Y" else fmt << '%d %m %Y' end delh = [:sentence] ? 'à' : '-' unless [:no_time] fmt << (as_verbal ? " à %H h %M" : " #{delh} %H:%M") end if [:seconds] fmt << (as_verbal ? ' mn et %S s' : ':%S' ) end fmt.join('') end end date.strftime(@last_format) end |
#help? ⇒ Boolean
15 16 17 |
# File 'lib/clir/state_methods.rb', line 15 def help? Clir::State.help? end |
#human_date(ladate = nil, **options) ⇒ String Also known as: date_humaine
Returns Une date formatée avec le moins verbal.
35 36 37 38 39 40 41 |
# File 'lib/clir/Date_utils.rb', line 35 def human_date(ladate = nil, **) ladate ||= Time.now .key?(:length) || .merge!(length: :long) lemois = MOIS[ladate.month][[:length]] lemois = "#{lemois}." if [:length] == :court "#{ladate.day} #{lemois} #{ladate.year}" end |
#ilya(laps, options = nil) ⇒ Object
/class Integer
158 159 160 161 |
# File 'lib/clir/Date_utils.rb', line 158 def ilya(laps, = nil) ||= {} (Time.now - laps).jj_mm_aaaa([:separator] || '/') end |
#label_value_line(label, value, params = nil) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/clir/helpers_methods.rb', line 62 def label_value_line(label, value, params = nil) str = "#{label.to_s.ljust(params[:label_width])}#{value}" str = str.send(params[:color]) unless params[:color].nil? str = str + ' ' + params[:detail].gris if params[:detail] return str end |
#labelize(ary, params = nil) ⇒ Object
Pour “labeliser” une table de label <> valeur
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/clir/helpers_methods.rb', line 35 def labelize(ary, params = nil) # # On peut traiter une simple ligne ou un tableau # ary = [ary] unless ary.is_a?(Array) # # Les paramètres à appliquer # params ||= {} params.key?(:gutter) || params.merge!(gutter: 4) params.key?(:label_width) || begin params.merge!(label_width: ary.max { |a,b| a[0].length <=> b[0].length }[0].length + params[:gutter] ) end (params.key?(:indent) && params[:indent]) || params.merge!(indent:'') params[:indent] = ' '*params[:indent] if params[:indent].is_a?(Integer) # # On construit la ligne ou chaque ligne du tableau # ary.collect do |lib, val, | params_line = params.dup case when Symbol then params_line.merge!(color: ) when Hash then params_line.merge!() end params[:indent] + label_value_line(lib, val, params_line) end.join("\n") end |
#less(texte) ⇒ Object
Use ‘less’ command to display texte
35 36 37 38 39 40 41 |
# File 'lib/clir/console_methods.rb', line 35 def less(texte) if debug? puts texte else exec "echo \"#{texte.gsub(/\"/,'\\"')}\" | less -r" end end |
#mkdir(pth) ⇒ Object Also known as: mkdir_p
Like ‘mkdir -p’ command
13 14 15 16 |
# File 'lib/clir/utils_methods.rb', line 13 def mkdir(pth) FileUtils.mkdir_p(pth) return pth end |
#pcent(value, with_cents = nil) ⇒ String
Returns Value value
as pourcentage.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/clir/utils_numbers.rb', line 35 def pcent(value, with_cents = nil) if value.is_a?(Integer) value = value.to_s elsif value == value.to_i.to_s value = value.to_i.to_s elsif value.to_f.to_s != value.to_s raise "Bad value. #{value.inspect}:#{value.class} can't be converted to pourcentage." end with_cents = 1 if with_cents == true if with_cents n, d = value.to_f.round(with_cents).to_s.split('.') d = d.ljust(with_cents,'0') value = "#{n}.#{d}" end "#{value} %" end |
#require_folder(dossier) ⇒ Object
Require all ruby file deep in folder dossier
6 7 8 |
# File 'lib/clir/utils_methods.rb', line 6 def require_folder(dossier) Dir["#{dossier}/**/*.rb"].each{|m|require(m)} end |
#round(n, decim = 2) ⇒ Object
To round a number
23 24 25 26 |
# File 'lib/clir/utils_methods.rb', line 23 def round(n, decim = 2) r = n.to_f.round(decim) r.to_f == r.to_i ? r.to_i : r.to_f end |
#test? ⇒ Boolean
11 12 13 |
# File 'lib/clir/state_methods.rb', line 11 def test? Clir::State.test? end |
#true_or_false(value) ⇒ Object
54 55 56 |
# File 'lib/clir/utils_methods.rb', line 54 def true_or_false(value) value ? :TRUE : :FALSE end |
#verbose? ⇒ Boolean
3 4 5 |
# File 'lib/clir/state_methods.rb', line 3 def verbose? Clir::State.verbose? end |
#version? ⇒ Boolean
19 20 21 |
# File 'lib/clir/state_methods.rb', line 19 def version? Clir::State.version? end |
#write_at(str, line, column) ⇒ Object
Write str
at column column
and line line
14 15 16 17 18 19 20 21 |
# File 'lib/clir/console_methods.rb', line 14 def write_at(str, line, column) msg = "\e[#{line};#{column}H#{str}" if test? puts msg else STDOUT.write msg end end |
#ymd(time = nil, delimitor = '-') ⇒ Object
@reçoit une date et la retourne sous la forme “YYYY-MM-DD”
57 58 59 60 |
# File 'lib/clir/Date_utils.rb', line 57 def ymd(time = nil, delimitor = '-') time ||= Time.now time.strftime("%Y#{delimitor}%m#{delimitor}%d") end |
#€(value, with_cents = false) ⇒ String
Returns La valeur exprimée en euro.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/clir/utils_numbers.rb', line 10 def €(value, with_cents = false) if value.is_a?(Integer) "#{value}#{".00" if with_cents} €" elsif value.to_f.to_s != value.to_s raise "Bad value. #{value.inspect} can't be converted to euros." else n, d = value.to_f.round(2).to_s.split('.') d = d.ljust(2,'0') "#{n}.#{d} €" end end |