Created
October 7, 2024 22:40
-
-
Save apainintheneck/77e41c4240a1234ca7f56b5bf1e3476f to your computer and use it in GitHub Desktop.
Query `urbandictionary.com` at the command line.
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/env ruby | |
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "urbandict", "~> 1.1", require: "urbandict" | |
gem "tty-pager", "~> 0.14", require: "tty-pager" | |
end | |
if ARGV.empty? | |
puts <<~USAGE | |
>>> udict | |
A command line interface for accessing `urbandictionary.com`. | |
Usage: udict TERM_OR_PHRASE | |
USAGE | |
exit 0 | |
end | |
search_term = ARGV.map(&:capitalize).join(" ") | |
search_results = UrbanDictionary.define(search_term) | |
boxed_search_term = <<~BOX | |
#{"-" * (search_term.size + 4)} | |
| #{search_term} | | |
#{"-" * (search_term.size + 4)} | |
BOX | |
def format_paragraph(text) | |
text.lines.map { _1.strip.prepend(" ") }.join("\n") | |
end | |
text = search_results.map do |slang| | |
<<~DEFINITION | |
#{boxed_search_term} | |
Definition: | |
#{format_paragraph(slang.definition)} | |
Example: | |
#{format_paragraph(slang.example)} | |
Author: | |
#{slang.author} | |
Reception: | |
(+#{slang.upvotes}/-#{slang.downvotes}) | |
Link: | |
#{slang.permalink} | |
DEFINITION | |
end.join("\n\n") | |
TTY::Pager.new(width: 100).page(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment