Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render images with netpbm #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions etc/t-completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ favorites)

-C|--color) completions='auto never' ;;

*) completions='--csv -c --decode_uris -d --id -i --long -l --max_id -m --number -n --relative_dates -a --reverse -r --since_id -s -H --host -C --color -P --profile' ;;
*) completions='--csv -c --decode_uris -d --id -i --long -l --max_id -m --number -n --relative_dates -a --reverse -r --since_id -s -H --host -C --color -P --profile --photos' ;;
esac;;

follow)
Expand Down Expand Up @@ -188,7 +188,7 @@ mentions)

-C|--color) completions='auto never' ;;

*) completions='--csv -c --decode_uris -d --long -l --number -n --relative_dates -a --reverse -r -H --host -C --color -P --profile' ;;
*) completions='--csv -c --decode_uris -d --long -l --number -n --relative_dates -a --reverse -r -H --host -C --color -P --profile --photos' ;;
esac;;

mute)
Expand Down Expand Up @@ -253,15 +253,15 @@ retweets)

-C|--color) completions='auto never' ;;

*) completions='--csv -c --decode_uris -d --id -i --long -l --number -n --relative_dates -a --reverse -r -H --host -C --color -P --profile' ;;
*) completions='--csv -c --decode_uris -d --id -i --long -l --number -n --relative_dates -a --reverse -r -H --host -C --color -P --profile --photos' ;;
esac;;

retweets_of_me)
case "$prev" in

-C|--color) completions='auto never' ;;

*) completions='--csv -c --decode_uris -d --long -l --number -n --relative_dates -a --reverse -r -H --host -C --color -P --profile' ;;
*) completions='--csv -c --decode_uris -d --long -l --number -n --relative_dates -a --reverse -r -H --host -C --color -P --profile --photos' ;;
esac;;

ruler)
Expand All @@ -286,7 +286,7 @@ timeline)
completions='replies retweets' ;;
-C|--color) completions='auto never' ;;

*) completions='--csv -c --decode_uris -d --exclude -e --id -i --long -l --max_id -m --number -n --relative_dates -a --reverse -r --since_id -s -H --host -C --color -P --profile' ;;
*) completions='--csv -c --decode_uris -d --exclude -e --id -i --long -l --max_id -m --number -n --relative_dates -a --reverse -r --since_id -s -H --host -C --color -P --profile --photos' ;;
esac;;

trends)
Expand Down Expand Up @@ -380,7 +380,7 @@ create) completions='-H --host -C --color -P --profile' ;;
information) completions='-H --host -C --color -P --profile' ;;
members) completions='-H --host -C --color -P --profile' ;;
remove) completions='-H --host -C --color -P --profile' ;;
timeline) completions='-H --host -C --color -P --profile' ;;
timeline) completions='-H --host -C --color -P --profile --photos' ;;
help) completions='-H --host -C --color -P --profile' ;;

-C|--color) completions='auto never' ;;
Expand All @@ -392,11 +392,11 @@ search)
case "$prev" in
search) completions='all favorites list mentions retweets timeline users help';;
all) completions='-H --host -C --color -P --profile' ;;
favorites) completions='-H --host -C --color -P --profile' ;;
list) completions='-H --host -C --color -P --profile' ;;
mentions) completions='-H --host -C --color -P --profile' ;;
retweets) completions='-H --host -C --color -P --profile' ;;
timeline) completions='-H --host -C --color -P --profile' ;;
favorites) completions='-H --host -C --color -P --profile --photos' ;;
list) completions='-H --host -C --color -P --profile --photos' ;;
mentions) completions='-H --host -C --color -P --profile --photos' ;;
retweets) completions='-H --host -C --color -P --profile --photos' ;;
timeline) completions='-H --host -C --color -P --profile --photos' ;;
users) completions='-H --host -C --color -P --profile' ;;
help) completions='-H --host -C --color -P --profile' ;;

Expand Down Expand Up @@ -427,11 +427,11 @@ stream)
case "$prev" in
stream) completions='all list matrix search timeline users help';;
all) completions='-H --host -C --color -P --profile' ;;
list) completions='-H --host -C --color -P --profile' ;;
list) completions='-H --host -C --color -P --profile --photos' ;;
matrix) completions='-H --host -C --color -P --profile' ;;
search) completions='-H --host -C --color -P --profile' ;;
timeline) completions='-H --host -C --color -P --profile' ;;
users) completions='-H --host -C --color -P --profile' ;;
search) completions='-H --host -C --color -P --profile --photos' ;;
timeline) completions='-H --host -C --color -P --profile --photos' ;;
users) completions='-H --host -C --color -P --profile --photos' ;;
help) completions='-H --host -C --color -P --profile' ;;

-C|--color) completions='auto never' ;;
Expand Down
11 changes: 6 additions & 5 deletions lib/t/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CLI < Thor

class_option 'color', aliases: '-C', type: :string, enum: %w(icon auto never), default: 'auto', desc: 'Control how color is used in output'
class_option 'profile', aliases: '-P', type: :string, default: File.join(File.expand_path('~'), T::RCFile::FILE_NAME), desc: 'Path to RC file', banner: 'FILE'
class_option 'photos', type: :boolean, default: false, desc: 'Render photos as ASCII (requires netpbm)'

def initialize(*)
@rcfile = T::RCFile.instance
Expand Down Expand Up @@ -279,7 +280,7 @@ def favorites(user = nil)
count = options['number'] || DEFAULT_NUM_RESULTS
opts = {}
opts[:exclude_replies] = true if options['exclude'] == 'replies'
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
opts[:include_rts] = false if options['exclude'] == 'retweets'
opts[:max_id] = options['max_id'] if options['max_id']
opts[:since_id] = options['since_id'] if options['since_id']
Expand Down Expand Up @@ -520,7 +521,7 @@ def matrix
def mentions
count = options['number'] || DEFAULT_NUM_RESULTS
opts = {}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
tweets = collect_with_count(count) do |count_opts|
client.mentions(count_opts.merge(opts))
end
Expand Down Expand Up @@ -657,7 +658,7 @@ def retweet(status_id, *status_ids)
def retweets(user = nil)
count = options['number'] || DEFAULT_NUM_RESULTS
opts = {}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
tweets = if user
require 't/core_ext/string'
user = options['id'] ? user.to_i : user.strip_ats
Expand All @@ -683,7 +684,7 @@ def retweets(user = nil)
def retweets_of_me
count = options['number'] || DEFAULT_NUM_RESULTS
opts = {}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
tweets = collect_with_count(count) do |count_opts|
client.retweets_of_me(count_opts.merge(opts))
end
Expand Down Expand Up @@ -762,7 +763,7 @@ def timeline(user = nil)
count = options['number'] || DEFAULT_NUM_RESULTS
opts = {}
opts[:exclude_replies] = true if options['exclude'] == 'replies'
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
opts[:include_rts] = false if options['exclude'] == 'retweets'
opts[:max_id] = options['max_id'] if options['max_id']
opts[:since_id] = options['since_id'] if options['since_id']
Expand Down
2 changes: 1 addition & 1 deletion lib/t/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def timeline(user_list)
owner, list_name = extract_owner(user_list, options)
count = options['number'] || DEFAULT_NUM_RESULTS
opts = {}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
tweets = collect_with_count(count) do |count_opts|
client.list_timeline(owner, list_name, count_opts.merge(opts))
end
Expand Down
23 changes: 21 additions & 2 deletions lib/t/printable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,44 @@ def print_table_with_headings(array, headings, format)
STDOUT.flush
end

def print_message(from_user, message)
def print_message(from_user, message, media = [])
require 'htmlentities'

case options['color']
when 'icon'
print_identicon(from_user, message)
print_media(media.first) if options['photos'] && media.first
say
say
when 'auto'
say(" @#{from_user}", [:bold, :yellow])
print_wrapped(HTMLEntities.new.decode(message), indent: 3)
print_media(media.first) if options['photos'] && media.first
say
else
say(" @#{from_user}")
print_wrapped(HTMLEntities.new.decode(message), indent: 3)
print_media(media.first) if options['photos'] && media.first
say
end
end

def print_media(media)
# Guess image format from file extension in URL
convert =
{'jpeg' => 'jpegtopnm - 2>/dev/null',
'jpg' => 'jpegtopnm - 2>/dev/null',
'png' => 'pngtopnm -',
'gif' => 'giftopnm -'}[media.media_uri.to_s.downcase[/\.([^.]+)$/, 1]]
return if convert.nil?

indent = options['color'] == 'icon' ? 10 : 3
scale = "pamscale -xyfill #{terminal_width - indent} 20 -"
output = `curl -s '#{media.media_uri}' | #{convert} | #{scale} | ppmtoascii -1x2`

$stdout.puts output.lines.map { |x| ' ' * indent << x }
end

def print_identicon(from_user, message)
require 'htmlentities'
require 't/identicon'
Expand Down Expand Up @@ -177,7 +196,7 @@ def print_tweets(tweets)
print_table_with_headings(array, TWEET_HEADINGS, format)
else
tweets.each do |tweet|
print_message(tweet.user.screen_name, decode_uris(tweet.full_text, options['decode_uris'] ? tweet.uris : nil))
print_message(tweet.user.screen_name, decode_uris(tweet.full_text, options['decode_uris'] ? tweet.uris : nil), tweet.media)
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/t/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(*)
def all(query)
count = options['number'] || DEFAULT_NUM_RESULTS
opts = {count: MAX_SEARCH_RESULTS}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
tweets = client.search(query, opts).take(count)
tweets.reverse! if options['reverse']
if options['csv']
Expand All @@ -51,7 +51,7 @@ def all(query)
else
say unless tweets.empty?
tweets.each do |tweet|
print_message(tweet.user.screen_name, decode_full_text(tweet, options['decode_uris']))
print_message(tweet.user.screen_name, decode_full_text(tweet, options['decode_uris']), tweet.media)
end
end
end
Expand All @@ -66,7 +66,7 @@ def favorites(*args)
query = args.pop
user = args.pop
opts = {count: MAX_NUM_RESULTS}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
if user
require 't/core_ext/string'
user = options['id'] ? user.to_i : user.strip_ats
Expand Down Expand Up @@ -96,7 +96,7 @@ def favorites(*args)
def list(user_list, query)
owner, list_name = extract_owner(user_list, options)
opts = {count: MAX_NUM_RESULTS}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
tweets = collect_with_max_id do |max_id|
opts[:max_id] = max_id unless max_id.nil?
client.list_timeline(owner, list_name, opts)
Expand All @@ -114,7 +114,7 @@ def list(user_list, query)
method_option 'relative_dates', aliases: '-a', type: :boolean, desc: 'Show relative dates.'
def mentions(query)
opts = {count: MAX_NUM_RESULTS}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
tweets = collect_with_max_id do |max_id|
opts[:max_id] = max_id unless max_id.nil?
client.mentions(opts)
Expand All @@ -136,7 +136,7 @@ def retweets(*args)
query = args.pop
user = args.pop
opts = {count: MAX_NUM_RESULTS}
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
if user
require 't/core_ext/string'
user = options['id'] ? user.to_i : user.strip_ats
Expand Down Expand Up @@ -171,7 +171,7 @@ def timeline(*args)
user = args.pop
opts = {count: MAX_NUM_RESULTS}
opts[:exclude_replies] = true if options['exclude'] == 'replies'
opts[:include_entities] = !!options['decode_uris']
opts[:include_entities] = !!options['decode_uris'] || !!options['photos']
opts[:include_rts] = false if options['exclude'] == 'retweets'
opts[:max_id] = options['max_id'] if options['max_id']
opts[:since_id] = options['since_id'] if options['since_id']
Expand Down
10 changes: 5 additions & 5 deletions lib/t/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def all
end
print_table([array], truncate: STDOUT.tty?)
else
print_message(tweet.user.screen_name, tweet.text)
print_message(tweet.user.screen_name, tweet.text, tweet.media)
end
end
end
Expand Down Expand Up @@ -82,7 +82,7 @@ def list(user_list)
end
print_table([array], truncate: STDOUT.tty?)
else
print_message(tweet.user.screen_name, tweet.text)
print_message(tweet.user.screen_name, tweet.text, tweet.media)
end
end
end
Expand Down Expand Up @@ -125,7 +125,7 @@ def search(keyword, *keywords)
end
print_table([array], truncate: STDOUT.tty?)
else
print_message(tweet.user.screen_name, tweet.text)
print_message(tweet.user.screen_name, tweet.text, tweet.media)
end
end
end
Expand Down Expand Up @@ -153,7 +153,7 @@ def timeline
end
print_table([array], truncate: STDOUT.tty?)
else
print_message(tweet.user.screen_name, tweet.text)
print_message(tweet.user.screen_name, tweet.text, tweet.media)
end
end
end
Expand Down Expand Up @@ -186,7 +186,7 @@ def users(user_id, *user_ids)
end
print_table([array], truncate: STDOUT.tty?)
else
print_message(tweet.user.screen_name, tweet.text)
print_message(tweet.user.screen_name, tweet.text, tweet.media)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/statuses_with_entities.json

Large diffs are not rendered by default.

Loading