require "yaml" module Focus class Config BASE_PATH = ENV["HOME"] DEFAULT_BLOCK_HOSTS = %w( reddit.com youtube.com facebook.com instagram.com twitter.com linkedin.com snapchat.com tumblr.com pinterest.com tiktok.com ) def initialize(filepath:) @filepath = File.join(BASE_PATH, filepath) dir = File.dirname(@filepath) if !File.exist?(dir) puts "Create config directory #{dir}" Dir.mkdir(dir) end if !File.exist?(@filepath) puts "Initialize default configuration #{@filepath}" write( { block_hosts: DEFAULT_BLOCK_HOSTS, }, ) end end def read YAML.load_file(@filepath) end def write(data) data[:block_hosts].uniq! File.write(@filepath, data.to_yaml) end def block_hosts read[:block_hosts] end def add_block_host(blocks) puts "This operation adds \"#{blocks}\" to the list of blocked hosts.".yellow puts "This operation does not block the hosts. Execute -b to execute the blocking.".yellow blocks.split(",").each do |block| data = read data[:block_hosts] << block write(data) puts "Added host \"#{block}\"!".green end end def delete_block_host(blocks) puts "This operation removes \"#{blocks}\" to the list of blocked hosts.".yellow puts "This operation does not unblock the hosts. Execute -b to refresh the blocking or -r to reset it.".yellow blocks.split(",").each do |block| data = read data[:block_hosts].delete_if { |host| host == block } write(data) puts "Removed host \"#{block}\"!".green end end end end