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

Added command-line options --ask and --save (and --version) #15

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
180 changes: 132 additions & 48 deletions bin/showterm
Original file line number Diff line number Diff line change
@@ -1,28 +1,104 @@
#!/usr/bin/env ruby

require File.join(File.dirname(File.dirname(__FILE__)), 'lib/showterm')
$LOAD_PATH << File.expand_path('../lib', File.dirname(__FILE__))

require 'optparse'
require 'showterm'
require 'showterm/version'

class Showterm::Main

def run
if ARGV.include?('-h') || ARGV.include?('--help')
@action = :record_and_upload
@ask_before_upload = false
@edit_timings = false
@save_only = false

option_parser.parse! ARGV
send @action
end

help
def option_parser
@option_parser ||= OptionParser.new do |opts|
opts.version = Showterm::Version::STRING

elsif ARGV[0] == '--retry'
opts.summary_width = 20
opts.summary_indent = ' '

ARGV.shift
reupload

else
# newline + pad to align option descriptions
nl = "\n#{' ' * (opts.summary_width + 3)}"

opts.banner = <<EOF
showterm will record the exact output of your session,
and upload it to the internet where it can be replayed
by anyone to whom you give the URL.

EOF

opts.define_head "Usage: #{opts.program_name} [-a] [-e] <command to run>"

if '-e' == ARGV[0] || '--edit' == ARGV[0]
opts.separator ''
opts.separator 'Recording Options:'

opts.on("-a", "--ask", "Ask for confirmation before uploading") do |value|
@ask_before_upload = true
end

opts.on("-e", "--edit", "Edit the timings file before uploading. This can#{nl}be nice if you want to take long pauses (such as#{nl}searching an answer out) in between commands.") do
@edit_timings = true
ARGV.shift
end

sf, tf = record
opts.on("-s", "--save", "Save the recording locally instead of uploading.#{nl}Afterwords, use --retry to finish the upload.") do
@save_only = true
end

opts.on("-r", "--retry", "Attempt an upload again (args: <script> <times>)") do
opts.terminate
@action = :retry
end

opts.separator ''
opts.separator 'Other Options:'

opts.on("-h", "--help", "Show this message") do
puts opts
exit
end

opts.on("-v", "--version", "Show Version") do
puts opts.ver
exit
end

end
end

def ask_yesno(*args)
print(*args)
print(" [y/N] ")
case $stdin.gets
when /^y$/, /^yes$/
true
else
false
end
end

def record_and_upload
sf, tf = record

if @save_only
save_politely "Saving the recording (no upload)", sf, tf
else
sf, tf = edit(sf, tf) if @edit_timings

if @ask_before_upload
unless ask_yesno("Upload?")
die_politely "Upload canceled!", sf, tf
end
end

upload sf, tf
end
end
Expand All @@ -34,22 +110,6 @@ class Showterm::Main
[sf, tf]
end

def help
puts <<-EOF
Usage: showterm [-e] <command to run>

showterm will record the exact output of your session, and upload it to the
internet where it can be replayed by anyone to whom you give the URL.

You can pass `-e` as the first arg and it will allow you to edit the timings
file before uploading. This can be nice if you want to take long pauses (such
as searching an answer out) in between commands.

If you would like to attempt an upload again
showterm --retry <script> <times>
EOF
end

def reupload
if ARGV.size == 2
upload(*ARGV.map{ |path| File.read(path) })
Expand Down Expand Up @@ -81,21 +141,34 @@ class Showterm::Main
[sf, tf]
end

def prepare_to_edit
puts "Recording done, now it's time to dress up those timings!" +
if 'vim' == editor
<<-TIPS
Hot vim tips:
def hot_vim_tips
[ "Hot vim tips:",
"",
"Use :cq from vim to exit nonzero, and cancel the upload",
"Use :%s/^[0-9]\./0./ to get rid of all longish pauses."
].join("\n")
end

def vim?
'vim' == editor
end

Use :cq from vim to exit nonzero, and cancel the upload
Use :%s/^[0-9]\./0./ to get rid of all longish pauses.
TIPS
else
"If you can make your editor return nonzero, it will cancel the upload."
end +
"[Hit Enter to edit]"
def editor_instructions
if vim?
hot_vim_tips
else
"If you can make your editor return nonzero, it will cancel the upload."
end
end

$stdin.readline
def prepare_to_edit
puts [
'',
"Recording done, now it's time to dress up those timings!",
editor_instructions,
'',
"[Hit Enter to edit]"
].join("\n")
end

def save(which, data)
Expand All @@ -106,16 +179,27 @@ class Showterm::Main
path
end

def die_politely(message, sf, tf)
def save_politely(message, sf, tf)
script_path = save 'script', sf
times_path = save 'times', tf
puts <<-MESSAGE
#{message}
your work is safe in "#{script_path}" and "#{times_path}"
To try uploading manually, use:
showterm --retry "#{script_path}" "#{times_path}"
MESSAGE
exit 1
times_path = save 'times', tf

$stdout.print <<MESSAGE

#{message}

Your work is safe in these files:
"#{script_path}"
"#{times_path}"

To try uploading manually, use:
showterm --retry '#{script_path}' '#{times_path}'

MESSAGE
end

def die_politely(message, sf, tf, retval = 1)
save_politely(message, sf, tf)
exit retval
end

def dedent(str)
Expand Down
5 changes: 5 additions & 0 deletions lib/showterm/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Showterm
module Version
STRING = '0.4.0'
end
end
6 changes: 5 additions & 1 deletion showterm.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
require "showterm/version"

Gem::Specification.new do |s|
s.name = "showterm"
s.version = "0.4.0"
s.version = Showterm::Version::String
s.platform = Gem::Platform::RUBY
s.author = "Conrad Irwin"
s.email = "[email protected]"
Expand Down