Module: Aniview::Util

Defined Under Namespace

Modules: FolderListen Classes: Term

Class Method Summary collapse

Class Method Details

.decode_object(e) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/aniview/util/util.rb', line 144

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

.encode_object(d) ⇒ Object



140
141
142
# File 'lib/aniview/util/util.rb', line 140

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

.error_message(message) ⇒ Object



21
22
23
24
# File 'lib/aniview/util/util.rb', line 21

def self.error_message(message)
  #use statusline to show error messages
  print "\e[#{Term.rows - 1};1H\e[31m #{message}\e[K"
end

.format_duration(v) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aniview/util/util.rb', line 26

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



61
62
63
# File 'lib/aniview/util/util.rb', line 61

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

.format_size(s) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aniview/util/util.rb', line 43

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

.mounted_filesystem?(dir) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/aniview/util/util.rb', line 154

def self.mounted_filesystem? dir
  %x(mount).split("\n").map { |l| 
    mounted = /on (.*?) /.match(l)[1]
    if mounted == "/"
      false
    else
      dir.split("/").zip(mounted.split("/")).reduce(true) { |m,v|
        m and (v[0] == v[1] or v[1] == nil)
      }
    end
  }.reduce { |a,b| a or b}
end

.parse_format(string, d, cols) ⇒ Object



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
134
135
136
137
138
# File 'lib/aniview/util/util.rb', line 65

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 = d.key?(c) ? String(d[c]) : ""
      padding = padding_collected > addstr.length ? " " * (padding_collected - addstr.length) : ""
      addstr = padding + addstr
      add_to += addstr
      mode = "char"
      padding_collected = 0
  
    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