add functions module

This commit is contained in:
Alexander Popov 2022-07-31 02:23:58 +03:00
parent 626e7cbe0b
commit 70a2e47727
Signed by: iiiypuk
GPG Key ID: D8C9B59A9F04A70C
3 changed files with 163 additions and 131 deletions

View File

@ -3,7 +3,9 @@ require "yaml"
require "colorize"
require "./pmng/*"
require "./pmng/functions/*"
module Pmng
# check password file exists
if File.exists?(PASSWORD_FILE_PATH)
yaml = File.open(PASSWORD_FILE_PATH) do |file|
@ -42,6 +44,8 @@ puts ASCII_LOGO.colorize(:yellow)
puts "The very simple password manager for humans".colorize(:yellow).mode(:bold)
puts "-------------------------------------------".colorize(:yellow).mode(:bold)
statistics = Functions::Statistics.new(passwords_array)
# main loop
loop = true
while loop
@ -76,24 +80,13 @@ while loop
elsif password_string.to_s == ":s"
# if ':s' to view Statistics
system "clear"
puts "Statistics\n----------".colorize(:yellow).mode(:bold)
print "All elements: ".colorize(:yellow).mode(:bold)
puts passwords_array.size
puts statistics.size
print "Passwords outdated: ".colorize(:red).mode(:bold)
outdated_count = 0
current_time = Time.local.to_unix
a = [] of String
passwords_array.each do |item|
if item.update + (2629743 * 3) < current_time # 2629743 * 3 -- 3 month
outdated_count += 1
a << item.url
end
end
puts outdated_count
puts statistics.outdated
else
# list search password
system "clear"
@ -102,33 +95,7 @@ while loop
passwords_array.each do |item|
if item.url.includes?(password_string.to_s)
print "🌐 "
puts item.url.colorize(:magenta).mode(:bold).mode(:underline)
if !item.email.blank?
print "📧 "
puts item.email.colorize(:red)
end
if !item.login.blank?
print "🗿 "
puts item.login.colorize(:yellow)
end
if !item.password.blank?
print "🔐 "
puts item.password.colorize(:red).back(:red)
end
if !item.desc.blank?
print "📄 "
puts item.desc.colorize(:cyan)
end
if !item.profile_url.blank?
print "👦 "
puts item.profile_url.colorize(:green)
end
Functions.show_item(item)
puts "-----".colorize(:dark_gray).mode(:bold)
@ -148,3 +115,4 @@ end
system "clear"
puts "Bye! 👋"
end

View File

@ -0,0 +1,33 @@
module Pmng::Functions
extend self
def show_item(password)
print "🌐 "
puts password.url.colorize(:magenta).mode(:bold).mode(:underline)
if !password.email.blank?
print "📧 "
puts password.email.colorize(:red)
end
if !password.login.blank?
print "🗿 "
puts password.login.colorize(:yellow)
end
if !password.password.blank?
print "🔐 "
puts password.password.colorize(:red).back(:red)
end
if !password.desc.blank?
print "📄 "
puts password.desc.colorize(:cyan)
end
if !password.profile_url.blank?
print "👦 "
puts password.profile_url.colorize(:green)
end
end
end

View File

@ -0,0 +1,31 @@
module Pmng::Functions
class Statistics
getter passwords : Array(Password)
getter outdated : Int32
getter size : Int32
def initialize(@passwords)
@size = passwords.size
outdated_count = 0
current_time = Time.local.to_unix
a = [] of String
@passwords.each do |item|
if item.update + (2629743 * 3) < current_time # 2629743 * 3 -- 3 month
outdated_count += 1
a << item.url
end
end
@outdated = outdated_count
end
def size
return @size
end
def outdated
return @outdated
end
end
end