From anonymous, 2 Months ago, written in Ruby.
This paste will hop the perch in 1 Second.
Embed
  1. #!/usr/bin/env ruby
  2. puts ""
  3. # puts "(press ^C to abort)"
  4.  
  5. paste = (!STDIN.tty? && !STDIN.closed? || ARGF.filename != ?-) ? ARGF.read : (require "clipboard"; Clipboard.paste)
  6. # https://stackoverflow.com/a/68673623/322020
  7.  
  8. $stdin = open "/dev/tty"
  9. puts "paste size: #{paste.size}"
  10. require_relative "../lib/paster"
  11. paster = Paster paste
  12. puts "preview: #{inspected = paste[0,30].inspect; inspected.size <= 30 ? inspected : inspected[0,27] + "..."}"
  13. puts "detected language: #{paster.lang || "unknown"}"   # change 'detected' to 'assumed' if we make it guess by file extension
  14. puts ""
  15.  
  16. require "tty-prompt"  # if we delay this the rescue matcher may say 'uninitialized constant TTY'
  17. begin  # TODO: we overwrite this identifier, why not?
  18.   options = [
  19.     [
  20.       {
  21.         "burn after reading": ["burn"],
  22.         "5 minutes": [5],
  23.         "1 hour": [60, 3600],
  24.         "1 day": [1440, 86400],
  25.         "3 days": [nil, 259200],
  26.         "1 week": [10080],
  27.         "1 month": [40320],
  28.         "3 months": [nil, 7776000],
  29.         "1 year": [483840],
  30.         "virtually forever": [0, -1],
  31.       },
  32.       :expire,
  33.       "expiration",
  34.     ],
  35.     [
  36.       {
  37.         "unlisted": true,
  38.         "public": false,
  39.       },
  40.       :unlist,
  41.       "visibility",
  42.     ],
  43.   ]
  44.   apply_variants = lambda do |paster, variants, option_name, _|
  45.     variants.map do |variant_name, value|
  46.       [variant_name, paster.dup.tap{ |_| _[option_name] = value }]
  47.     end
  48.   end
  49.  
  50.   names, paster = options.reduce [[[], paster]] do |pasters, option|
  51.     pasters.flat_map do |names, paster|
  52.       apply_variants.call(paster, *option).map do |variant_name, paster|
  53.         [[*names, variant_name], paster]
  54.       end
  55.     end
  56.   end.max_by{ |_, paster| paster.services.size }
  57.   abort "no pastebin for such size" if paster.services.size.zero?  # otherwise tty-prompt fails with an unhandled exception, lol
  58.  
  59.   loop do
  60.     option, index = TTY::Prompt.new.select "change current options if needed:", [
  61.       *options.map.with_index{ |option, i| {name: "#{option[2]}: #{names[i]}", value: [option, i]} },
  62.       {name: "proceed", value: nil},
  63.     ], default: "proceed",
  64.       filter: true, cycle: true, per_page: 100
  65.     break unless option
  66.     paster, names[index] = TTY::Prompt.new.select "#{option[2]}:",
  67.       apply_variants.call(paster, *option).map{ |name, paster|
  68.         size = paster.services.size
  69.         size.zero? ?
  70.           {name: "#{name}",           value: [paster, name], disabled: "(no pastebins)"} :
  71.           {name: "#{name} (#{size})", value: [paster, name]}
  72.       },
  73.       default: option[0].keys.index(names[index]) + 1,
  74.       filter: true, cycle: true, per_page: 100
  75.   end
  76. rescue TTY::Reader::InputInterrupt
  77.   # we don't use TTY::Prompt.new(interrupt: :exit) because it does not finish the last printed line
  78.   abort "\n\n(interrupted by SIGINT)"
  79. end
  80. if ENV.include? "DRYRUN"
  81.   require "pp"
  82.   pp paster
  83. else
  84.   paster.paste
  85. end
  86.  
  87. puts ""
  88.