Module: Aniview::Util

Defined Under Namespace

Classes: Term

Class Method Summary collapse

Class Method Details

.decode_object(e) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/aniview/util/util.rb', line 139

def self.decode_object e
  begin
    Marshal.load(Base64.decode64(e))
  rescue ArgumentError
    {}
  rescue TypeError
    {}
  end
end

.encode_object(d) ⇒ Object



135
136
137
# File 'lib/aniview/util/util.rb', line 135

def self.encode_object d
  Base64.strict_encode64(Marshal.dump d)
end

.format_duration(v) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aniview/util/util.rb', line 21

def self.format_duration v
  hours = Integer((v-(v%3600))/3600)
  minutes = Integer( ((v-(v%60))/60) - (hours * 60))
  seconds = Integer(v%60)
  seconds = "0" + String(seconds) if seconds < 10
  if hours < 1 and minutes < 1
    "0:#{seconds}"
  else
    if hours < 1
      return "#{String(minutes)}:#{String(seconds)}"
    else
      minutes = "0" + String(minutes) if minutes < 10
      return "#{String(hours)}:#{String(minutes)}:#{String(seconds)}"
    end
  end
end

.format_progress(p) ⇒ Object



56
57
58
# File 'lib/aniview/util/util.rb', line 56

def self.format_progress p
  return "#{Integer(p)}.#{Integer(p * 10.0) % 10}"
end

.format_size(s) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aniview/util/util.rb', line 38

def self.format_size s
  fs = Float(s)
  
  bytecount = {
    "TB" => 1000000000000.0,
    "GB" => 1000000000.0,
    "MB" => 1000000.0,
    "KB" => 1000.0,
    "B"  => 1.0,
  }
  r = "TB"
  r = "GB" if s < bytecount["TB"]
  r = "MB" if s < bytecount["GB"]
  r = "KB" if s < bytecount["KB"]
  
  return String( Float(fs/bytecount[r] * 10.0 ).round / 10.0 ) + r
end

.parse_format(string, d, cols) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
128
129
130
131
132
133
# File 'lib/aniview/util/util.rb', line 60

def self.parse_format string, d, cols
  le = "\e[K\n"
  
  pstr = []
  add_to = ""
  buffer_char = []
  
  mode = "char"
  
  padding_collected = 0
  
  string.split('').each { |c|
  
    if mode == "e"
      add_to += String(c)
      mode = "char"
    
    elsif c == '%'
      mode = "%"
    
    elsif c == '$'
      mode = "$"
    
    elsif c == '@'
      pstr << add_to
      add_to = ""
      mode = "@"
    
    elsif c == "#"
      padding_collected += 1
    
    elsif c == "\\"
      mode = "e"
    
    elsif mode == "@"
      buffer_char << c
      mode = "char"
    
    elsif mode == "%"
      addstr = ""
      addstr = String(d[c]) if d.key?(c)
      ol = addstr.length
      addstr = " " + addstr while addstr.length < (ol + padding_collected)
      add_to += addstr
      mode = "char"
  
    else
      add_to += c
    
    end
  }
  pstr << add_to
  
  buffer_char << " " if buffer_char.length == 0
  
  midstr = ""
  tl = 0
  pstr.each{ |s| tl += s.length }
  buffer_space = 0
  buffer_space = (cols - tl) / (pstr.length - 1) if pstr.length > 1
  
  addl_space = (cols - tl) % pstr.length
  
  pstr.each_with_index{ |str, i|
    break if i == pstr.length - 1
    buffer_ = ""
    buffer_ = buffer_char[i] * buffer_space if buffer_space >= 0
  
    midstr += str + buffer_
  }
  midstr += pstr[pstr.length - 1]
  
  return midstr[0..cols-1]
end

.readline(term, prompt = "", default = "") ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/aniview/util/util.rb', line 6

def self.readline(term, prompt = "", default = "")
  term.bold.echo_on
  print "\033[" + String(term.rows) + ";1H" + Aniview::View::Color.white
  term.show_cursor
  Readline.completion_append_character = ""
  Readline.pre_input_hook = -> do
    Readline.insert_text default
    Readline.redisplay
    Readline.pre_input_hook = nil
  end
  input = Readline.readline(prompt, false)
  term.hide_cursor.nobold.echo_off.clear
  return input
end