#!/usr/bin/env ruby require 'fileutils' require 'slop' require 'neatjson' require 'clock_window' opts = Slop.parse do |o| o.banner = <<-BANNER Clock Window - "Clock your desktop activity!" Copyright (c) 2016 Daniel P. Clark via the MIT License Version: #{ClockWindow::VERSION} usage: clock_window [options] BANNER o.bool "-c", "--clean", "Use all known name filters" o.string "-o", "--output", "File/directory output in strftime" o.bool "-e", "--eager", "Use strftime of time at start of script\n" o.on "--version", "Print the version" do puts ClockWindow::VERSION exit end o.bool "-h", "--help", "This help menu\n\n" end if opts[:help] puts opts exit else puts opts.to_s.partition("\n\n").first puts "\nPress CTRL-C to exit the script." puts "Starting active window tracking!" end dir_name, _, file_name = opts[:output].to_s.rpartition(File::SEPARATOR) file_name = "timelog.json" if file_name.empty? @time = Time.now if opts[:eager] @hash = {} begin loop do filter_opts = opts.clean? ? {filter_opts: {no_notify: true}} : {} x = ClockWindow::ClockIt.new(**filter_opts).active_window @hash[x] = @hash[x].to_f + 0.25 sleep 15 end ensure @hash = {"*---------- WINDOW NAME ----------*" => "minutes"}.merge(@hash) @time ||= Time.now dir = @time.strftime(dir_name) FileUtils.mkdir_p(dir) unless dir.empty? file = @time.strftime(file_name) File.open("#{dir}#{File::SEPARATOR unless dir.empty?}#{file}","w") do |f| f.write(JSON.neat_generate(@hash,aligned:true,around_colon:1)) end end