Created
January 18, 2015 09:44
-
-
Save trengrj/7f2291dfad2a24fecc02 to your computer and use it in GitHub Desktop.
smarter ls for a directory of text files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'smart_colored/extend' | |
filter = nil | |
def usage | |
puts "usage: 'notes [filter]'" | |
end | |
# add some more command line options like -d for different directory | |
def parse_arguments | |
if ARGV.length > 1 | |
usage | |
exit | |
end | |
if ARGV.length == 1 | |
filter = ARGV[0] | |
end | |
end | |
def parse_directory | |
file_names = Dir.entries(".") | |
file_data = [] | |
directories = [] | |
# fix this to check if a directory | |
for name in file_names | |
if not name =~ /.txt/ | |
next if name == "." or name == ".." | |
puts (name + "/").yellow | |
next | |
end | |
head = File.open("./"+name).first | |
if head == nil | |
head = "<empty>" | |
end | |
head = head.gsub("\n",'')[0,70] | |
line = File.mtime("./"+name).strftime("%d/%m").red+ | |
" "+name.bold+" - "+head | |
file_data.push([File.mtime("./"+name),line]) | |
end | |
file_data.sort! { |a,b| a[0] <=> b[0] } | |
for file in file_data | |
puts file[1] | |
end | |
end | |
parse_arguments | |
parse_directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment