Skip to content

Instantly share code, notes, and snippets.

@DivineDominion
Last active April 4, 2020 08:38
Show Gist options
  • Save DivineDominion/0e72082a79359866a15558cba5c67e9a to your computer and use it in GitHub Desktop.
Save DivineDominion/0e72082a79359866a15558cba5c67e9a to your computer and use it in GitHub Desktop.
Find a random plain text file in a directory
#!/usr/bin/env ruby
# Usage: random_file.rb [options]
# -d, --dir [VALUE] Path to the note archive. Default: the current working directory.
# -c, --count [VALUE] Amount of unique random notes to fetch
# -h, --help Prints this help
require "optparse"
CURRENT_PATH = Dir.pwd
options = {
directory: CURRENT_PATH,
amount: 1
}
parser = OptionParser.new do |o|
o.banner = "Usage: #{__FILE__} [options]"
o.on("-d", "--dir [VALUE]", "Path to the note archive. Default: the current working directory (#{CURRENT_PATH})") do |val|
path = File.expand_path(val.to_s)
if File.exist?(path)
options[:directory] = path
else
abort %Q{Directory at path #{path} does not exist}
end
end
o.on("-c", "--count [VALUE]", "Amount of unique random notes to fetch") do |val|
options[:amount] = val.to_i
end
o.on_tail("-h", "--help", "Prints this help") do
STDOUT.puts o
exit
end
end
parser.parse!
# puts options
EXTENSIONS = ["markdown", "md", "mmd", "txt", "text"]
def extension(path)
filename = File.basename(path)
filename_without_ext = File.basename(path, ".*")
# Take the remainder, without the dot, after the filename
return filename[filename_without_ext.length + 1 .. -1]
end
results = []
Dir.chdir(options[:directory]) do
all_files = Dir.glob("*").select { |f| EXTENSIONS.include?(extension(f))}
options[:amount].times do
remainder = all_files - results
next_file = remainder.sample
break if next_file.nil?
results << next_file
end
end
STDOUT.puts results
@DivineDominion
Copy link
Author

Example

Command

$ ruby random_note.rb -d ~/Archive/ -c 12

Example output:

201701161531 Automatic version numbering.md
201708211509 Buffer 3rd Wave Feminismus.txt
201309271825 Portable Kanban board.md
201607031406 User login session management in the Fabric app.md
201602201129 ambient interface.txt
201707272141 The 7 habits of highly effective artists.txt
201708231440 Why does ReSwift Middleware have dispatch.txt
2010-11-23_0757_R1_How to make introductions.md
201907060747 Send HTTP request from Ruby.txt
201611201032 Discipline necessitates purpose.md
201803221749 chmod settings for web server scripts.txt
201606050858 Function declaration kinds in JavaScript.txt

Sorted Example

Command, piping the output to sort:

$ ruby random_note.rb -d ~/Archive/ -c 12 | sort

Example output:

2010-11-23_0757_R1_How to make introductions.md
201309271825 Portable Kanban board.md
201602201129 ambient interface.txt
201606050858 Function declaration kinds in JavaScript.txt
201607031406 User login session management in the Fabric app.md
201611201032 Discipline necessitates purpose.md
201701161531 Automatic version numbering.md
201707272141 The 7 habits of highly effective artists.txt
201708211509 Buffer 3rd Wave Feminismus.txt
201708231440 Why does ReSwift Middleware have dispatch.txt
201803221749 chmod settings for web server scripts.txt
201907060747 Send HTTP request from Ruby.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment