module JungiCli

JungiCli utility module

Public Class Methods

ask_integer(prompt) click to toggle source

Ask for integer

# File lib/jungi/cli.rb, line 35
def self.ask_integer(prompt)
  result = nil
  until yield result
    print prompt
    result = gets.chomp.to_i
  end
  result
end
ask_scale(scale, randomize = false) click to toggle source

Nicely ask a scale

# File lib/jungi/cli.rb, line 74
def self.ask_scale(scale, randomize = false)
  blah = parse_scale scale
  if /\n/.match blah
    display_doc blah
  else
    puts blah
  end

  if randomize
    int = rand(1..5)
    puts "> #{int}"
    int
  else
    ask_integer '> ' do |r|
      Question::Answer.scale? r
    end
  end
end
ask_scale7(scale, randomize = false) click to toggle source

Nicely ask a scale7

# File lib/jungi/cli.rb, line 123
def self.ask_scale7(scale, randomize = false)
  blah = parse_scale7 scale
  if /\n/.match blah
    display_doc blah
  else
    puts blah
  end

  if randomize
    int = rand(1..7)
    puts "> #{int}"
    int
  else
    ask_integer '> ' do |r|
      Question::Answer.scale7? r
    end
  end
end
center_doc(doc) click to toggle source

Pad a document to width

# File lib/jungi/cli.rb, line 143
def self.center_doc(doc)
  doc = doc.split "\n"
  out = []
  doc.length.times do |int|
    out[int] = doc[int].center width
  end
  out
end
clr() click to toggle source

Clear screen

# File lib/jungi/cli.rb, line 165
def self.clr
  system('clear') || system('cls')
end
display_doc(doc) click to toggle source

Display document centered

# File lib/jungi/cli.rb, line 153
def self.display_doc(doc)
  center_doc(doc).each do |line|
    puts line
  end
end
line() click to toggle source

Render line

# File lib/jungi/cli.rb, line 160
def self.line
  puts '-' * width
end
parse_scale(scale) click to toggle source

Handle question via scale

# File lib/jungi/cli.rb, line 51
def self.parse_scale(scale)
  p1, p2 = scale.split '|'
  if p2
    s1, c1, s2 = scale_proto
    s1[s1.length - p1.length, s1.length - 1] = p1
    s2[0, p2.length - 1] = p2
    res = s1 + c1 + s2
    if res.length > width + 1
      res = (p1 + ' |1|2|3|4|5| ' + p2).center width
      if res.length > width
        return "#{p1}\n |1|2|3|4|5| \n#{p2}"
      else
        return res
      end
    else
      return res
    end
  else
    "#{p1}\n |1|2|3|4|5| "
  end
end
parse_scale7(scale) click to toggle source

Handle question via scale7

# File lib/jungi/cli.rb, line 100
def self.parse_scale7(scale)
  p1, p2 = scale.split '|'
  if p2
    s1, c1, s2 = scale7_proto
    s1[s1.length - p1.length, s1.length - 1] = p1
    s2[0, p2.length - 1] = p2
    res = s1 + c1 + s2
    if res.length > width + 1
      res = (p1 + ' |1|2|3|4|5|6|7| ' + p2).center width
      if res.length > width
        return "#{p1}\n |1|2|3|4|5|6|7| \n#{p2}"
      else
        return res
      end
    else
      return res
    end
  else
    "#{p1}\n |1|2|3|4|5|6|7| "
  end
end
scale7_proto() click to toggle source

Fetch prototype objects7

# File lib/jungi/cli.rb, line 94
def self.scale7_proto
  start = (' |1|2|3|4|5|6|7| '.center width)
  start.partition(' |1|2|3|4|5|6|7| ')
end
scale_proto() click to toggle source

Fetch prototype objects

# File lib/jungi/cli.rb, line 45
def self.scale_proto
  start = (' |1|2|3|4|5| '.center width)
  start.partition(' |1|2|3|4|5| ')
end
width() click to toggle source

Fetch terminal width

# File lib/jungi/cli.rb, line 26
def self.width
  val = `tput cols`.chomp.to_i
  val = 80 if val == 0
  val
rescue Errno::ENOENT
  80
end