Module: Dsl::Python

Defined in:
lib/dsl/python.rb,
lib/dsl/python/cli.rb,
lib/dsl/python/version.rb,
lib/dsl/python/repl/zen.rb,
lib/dsl/python/types/array.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.20.0"

Class Method Summary collapse

Class Method Details

.open_replObject



8
9
10
11
12
13
14
15
# File 'lib/dsl/python/cli.rb', line 8

def self.open_repl
  text = <<~TEXT
    nPython #{VERSION} (main, Nov 16 1970) [GCC] on linux
    Type "help", "copyright", "credits" or "license" for more information.
  TEXT
  puts text
  system("irb --nobanner --prompt simple --nocolorize --noecho-on-assignment -Ilib -rdsl/python")
end

.puts_in_columns(values) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dsl/python/types/array.rb', line 40

def self.puts_in_columns(values)
  space = " "
  tab = space * 3
  max_value = values.map {_1.length}.max
  max_value += tab.size

  max_screen = IO.console.winsize[1]
  max_col = max_screen / max_value
  values.each_slice(max_col) do |items|
    words = items.map { _1 + space * (max_value - _1.length)}
    puts words.join 
  end
  nil
end

.show_helpObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dsl/python/cli.rb', line 21

def self.show_help
  text = <<~TEXT
    usage: npython [option | file ]
    Options:
    -h     : print this help message and exit (also -? or --help)
    -v     : show current version (also -V or --version)
    
    Arguments:
    file   : program read from script file
  TEXT
  puts text
end

.show_versionObject



17
18
19
# File 'lib/dsl/python/cli.rb', line 17

def self.show_version
  puts "npython #{VERSION}"
end

.show_zen(ofuscate: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dsl/python/repl/zen.rb', line 19

def self.show_zen(ofuscate: false)
  text = <<~TEXT
    El Zen de nPython (Inspirado por Matz)

     1. La felicidad del programador es el fin último.
     2. Lo natural es mejor que lo explícito.
     3. La libertad es mejor que la restricción.
     4. La elegancia supera a la brevedad.
     5. Muchos caminos son mejores que uno solo (TIMTOWTDI).
     6. Si el código se lee como prosa, es buen código.
     7. No castigues al programador por ser inteligente.
     8. La pureza del objeto es sagrada.
     9. La metaprogramación es un superpoder, úsalo con sabiduría.
    10. El lenguaje debe adaptarse al humano, no el humano al lenguaje.
    11. Un bloque es a menudo la respuesta.
    12. Los símbolos son mejores que los strings para la identidad.
    13. El principio de Menor Sorpresa es subjetivo, pero vital.
    14. Si te hace sonreír al escribirlo, es Ruby.
  TEXT

  lines = text.split("\n")
  lines.each_with_index do |line, index|
    if line.empty?
      puts ""
      next
    end
    words = line.split
    index = if index.zero?
      words.shift
    else
      ANSI.new.green(words.shift)
    end
    text = words.join(" ")
    text = text.gsub(/[^\d\s]/, "*") if ofuscate
    puts "  #{index} #{text}"
  end
  nil
end