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

Instance Method Details

#clearObject

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_heightObject

Returns lines count of the console.

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_widthObject

Returns column count of the console.

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.

Examples:

date_for_file # => "2022-12-14"
date_for_file(nil, true) # => "2022-12-14-23-11"

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

Note:

Alias :time_from (more semanticaly correct)

Returns Date corresponding to foo.

Parameters:

  • foo (String|Integer|Time|Date)
    • Time

      Return itself

    • Date

      Return itself.to_time

    • String

      JJ/MM/AAAA or ‘YYYY/MM/DD’

    • Integer

      Number of seconds

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

Returns:

  • (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

Returns:

  • TRUE if file existed (and have been deleted).



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)

Parameters:

  • date (Time)
  • options (Hash) (defaults to: nil)

    table:

Options Hash (options):

  • :verbal (Boolean)

    If true, the format will be “month the day-th etc.”

  • :no_time (Boolean)

    If true, only day, without time

  • :seconds (Boolean)

    If true, add seconds with time

  • :update_format (Boolean)

    If true, the format is updated. Otherwise, the last format is used for all next date

  • :sentence (Boolean)

    If true, on met “le … à .…”



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, options = nil)
  options ||= {}
  @last_format = nil if options[:update_format] || options[:template]
  @last_format ||= begin
    as_verbal = options[:verbal]||options[:sentence]
    if options[:template]
      options[:template]
    else
      fmt = []
      fmt << 'le ' if options[:sentence]
      if as_verbal
        forday = date.day == 1 ? '1er' : '%-d'
        fmt << "#{forday} #{MOIS[date.month][:long]} %Y" 
      else
        fmt << '%d %m %Y'
      end
      delh = options[:sentence] ? 'à' : '-'
      unless options[:no_time]
        fmt << (as_verbal ? " à %H h %M" : " #{delh} %H:%M")
      end
      if options[:seconds]
        fmt << (as_verbal ? ' mn et %S s' : ':%S' )
      end
      fmt.join('')
    end
  end
  date.strftime(@last_format)
end

#help?Boolean

Returns:

  • (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.

Parameters:

  • La (Time|Date|NIl)

    date. Si non fournie, on prend maintenant

  • options (Hash)

    Les options de formatage

  • lenght (Hash)

    a customizable set of options

Returns:

  • (String)

    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, **options)
  ladate ||= Time.now
  options.key?(:length) || options.merge!(length: :long)
  lemois = MOIS[ladate.month][options[:length]]
  lemois = "#{lemois}." if options[: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, options = nil)
  options ||= {}
  (Time.now - laps).jj_mm_aaaa(options[: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

Parameters:

  • ary (Array)

    Liste des valeurs, sous la forme

    [
      ['<label>', '<valeur>'[ :couleur|options]],
      ['<label>', '<valeur>'[ :couleur|options]],
      etc,
    ]
    

    Produira :

    label   valeur
    label   valeur
    etc.
    

    Le 3e paramètre de chaque item peut définir SOIT la couleur comme un symbole (par exemple :vert), SOIT une table qui peut contenir :

    color:    La couleur à appliquer
    detail:   Un texte à ajouter après la valeur (en gris)
    
  • params (Hash|Nil) (defaults to: nil)

    Paramètres définissant la table à obtenir avec :

    :indent Identation avant le label (soit un nombre

    d'espace soit l'indentation elle-même)
    

    :gutter Taille (en espace) de la gouttière entre les

    libellés et les valeurs (4 par défaut)
    

    :label_width Pour forcer la taille des libellés. Sinon,

    elle sera calculée d'après le plus grand
    label.
    


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, options|
    params_line = params.dup
    case options
    when Symbol then params_line.merge!(color: options) 
    when Hash   then params_line.merge!(options)
    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.

Examples:

pcent(12)           # => "12 %"
pcent(12, 3)        # => "12.000 %"
pcent(12.436, 1)    # => "12.4 %"
pcent(12.436, true) # => "12.4 %"
pcent(12.436, 2)    # => "12.44 %"

Parameters:

  • value (Integer|String|Float)

    The value to convert

  • with_cents (TrueClass|Integer) (defaults to: nil)

    Decimal value. If true, 1 decimal is used (“10.1 %”)

Returns:

  • (String)

    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

Returns:

  • (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

Returns:

  • (Boolean)


3
4
5
# File 'lib/clir/state_methods.rb', line 3

def verbose?
  Clir::State.verbose?
end

#version?Boolean

Returns:

  • (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.

Examples:

("12")       # => '12 €'
("12", true) # => '12.00 €'
(12.5698)    # => '12.57 €'

Parameters:

Returns:

  • (String)

    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