#! /usr/bin/env ruby FINGER_DIR = "#{ENV["HOME"]}/extra-stuff/fingers/" # strip newlines, but not indent def file_strip(str) # .slice! removes match from the start s = str.rstrip s.slice! /\n+/ s end ######### # LOCAL # ######### def gather_users_local Dir.children "/home/" end def gather_finger_local(user) project_path = "/home/#{user}/.project" plan_path = "/home/#{user}/.plan" project_readable = File.readable? project_path plan_readable = File.readable? plan_path return "" unless project_readable or plan_readable data = "### #{user}\n```\n" if project_readable data += "Project:\n" data += file_strip(File.read project_path) + "\n" data += "\n" if plan_readable end if plan_readable data += "Plan:\n" data += file_strip(File.read plan_path) + "\n" end data + "```\n" end ######### # GREEN # ######### def gather_users_green # scrape users from the website # this are only users with a website, but I would guess most users using # finger also have at least something in their website site = `curl https://tilde.green/ 2>/dev/null` site.scan(/\/~[\w\d_-]+\//).map {|i| i[2..-2]} end def gather_finger_green(user) entry = `finger #{user}@tilde.green` # catch errors return "" if entry.empty? # filter out empty users return "" if entry.match? "Project: No Project.\nPlan: No Plan." # tilde.green puts 'Hello 0.0.0.0,' before the actual files # This includes empty plans and projects, but I'm not sure what engine is # being used and if there even have to be plan and project sections "### #{user}\n```\n#{entry.split("Hello 0.0.0.0,\n\n")[1].strip}\n```\n" end ########################### # TIE EVERYTHING TOGETHER # ########################### def gen_stuff(collections) collections.each { |col| contents = col["gather_users"].call.map{|i| col["gather_finger"].call i}.join File.write FINGER_DIR + col["file_name"], contents } end gen_stuff [ { "file_name" => "ctrl-c", "gather_users" => method(:gather_users_local), "gather_finger" => method(:gather_finger_local), }, { "file_name" => "tilde.green", "gather_users" => method(:gather_users_green), "gather_finger" => method(:gather_finger_green), }, ]