module But::HomeKeeper
Constants
- BLOCK_SIZE
Public Class Methods
create_disk_usage_file(options = {})
click to toggle source
Q: is $HOME always set?
# File lib/but/home_keeper.rb, line 28 def self.create_disk_usage_file(options = {}) tmpfile = ::Tempfile.new("diskusage") dir = options[:dir] || ENV['HOME'] total_lines = `ls -d ~/.*/ ~/*/ | wc -l`.to_i #puts total_lines progressbar = ProgressBar.create(title: "inspecting #{dir}", total: total_lines , :format => "%a %c/%C %b\u{15E7}%i %p%% %t", :progress_mark => ' ', :remainder_mark => "\u{FF65}", ) ::Open3.popen3("du","-d 1", dir) do |stdin, stdout, stderr, wait_thr| # inspired by from https://gist.github.com/chrisn/7450808 stdin.close_write begin files = [stdout, stderr] until all_eof(files) do ready = IO.select(files) progressbar.refresh if ready readable = ready[0] readable.each do |f| fileno = f.fileno begin data = f.read_nonblock(BLOCK_SIZE) #puts "fileno: #{fileno}, data: #{data.inspect}" progressbar.progress += data.lines.count progressbar.title = "completed: %s" % data.split(/\s+/).last tmpfile.write(data) rescue EOFError => e #puts "fileno: #{fileno} EOF" files.delete f end end end end rescue IOError => e puts "IOError: #{e}" end #if wait_thr.value != 0 # STDERR.write "Exitcode: #{wait_thr.value}\n".light_black # STDERR.write stderr.gets(nil).light_black #end #tmpfile.write(stdout.gets(nil)) end progressbar.finish tmpfile.rewind return tmpfile.path end
info(options = {})
click to toggle source
# File lib/but/home_keeper.rb, line 10 def self.info(options = {}) file = ENV['BUTHOMEKEEPER'] || create_disk_usage_file(options) file_info = File.open(file).readlines.map(&:chomp) end
judge(options = {})
click to toggle source
# File lib/but/home_keeper.rb, line 15 def self.judge(options = {}) total_disc_usage, dir = info(options).last.split(/\s+/) total_disc_usage = total_disc_usage.to_i if total_disc_usage >= But::HOMELIMIT return "Dare you! Your directory #{dir} contains ~#{h_readable total_disc_usage}, which is ~#{h_readable (total_disc_usage - But::HOMELIMIT)} above the limit of #{h_readable But::HOMELIMIT}. Dare you!".red else return "Good job! Your directory #{dir} takes only ~#{h_readable total_disc_usage} disk storage. Good job!".light_green end end
Private Class Methods
all_eof(files)
click to toggle source
# File lib/but/home_keeper.rb, line 88 def self.all_eof(files) files.find { |f| !f.eof }.nil? end
h_readable(bytes)
click to toggle source
# File lib/but/home_keeper.rb, line 92 def self.h_readable(bytes) return "%.2f GB"% (bytes / (1024.0 * 1024.0)) end