Module: But::HomeKeeper
- Defined in:
- lib/but/home_keeper.rb
Constant Summary collapse
- BLOCK_SIZE =
1024
Class Method Summary collapse
-
.create_disk_usage_file(options = {}) ⇒ Object
Q: is $HOME always set?.
- .info(options = {}) ⇒ Object
- .judge(options = {}) ⇒ Object
Class Method Details
.create_disk_usage_file(options = {}) ⇒ Object
Q: is $HOME always set?
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 57 58 59 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 |
# File 'lib/but/home_keeper.rb', line 28 def self.create_disk_usage_file( = {}) tmpfile = ::Tempfile.new("diskusage") dir = [:dir] || ENV['HOME'] total_lines = `ls -d ~/.*/ ~/*/ | wc -l`.to_i #puts total_lines = 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) .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}" .progress += data.lines.count .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 .finish tmpfile.rewind return tmpfile.path end |
.info(options = {}) ⇒ Object
10 11 12 13 |
# File 'lib/but/home_keeper.rb', line 10 def self.info( = {}) file = ENV['BUTHOMEKEEPER'] || create_disk_usage_file() file_info = File.open(file).readlines.map(&:chomp) end |
.judge(options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/but/home_keeper.rb', line 15 def self.judge( = {}) total_disc_usage, dir = info().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 |