class Hackpad::Cli::Client

Attributes

config[R]

Public Class Methods

new(options, input = STDIN, output = STDOUT) click to toggle source
# File lib/hackpad/cli/client.rb, line 16
def initialize(options, input = STDIN, output = STDOUT)
  @output = output
  @input = input
  @config = Config.new options, @input, @output
  if options[:plain] == true || @config.use_colors == false
    Paint.mode = 0
  end
  @workspace = Workspace.new({ basedir: File.join(@config.basedir, @config.workspace), name: @config.workspace }, @input, @output)
  Store.prepare @config, @workspace
  Api.prepare @workspace
end

Public Instance Methods

add() click to toggle source
# File lib/hackpad/cli/client.rb, line 28
def add
  @workspace.clone.create
end
default() click to toggle source
# File lib/hackpad/cli/client.rb, line 41
def default
  @config.change_default
end
getnew() click to toggle source
# File lib/hackpad/cli/client.rb, line 65
def getnew
  @output.puts 'New pads:'
  padlist = Padlist.get_new
  if padlist.count == 0
    @output.puts 'There is no new pad.'
  else
    @output.puts padlist.map { |pad|
      padline pad
    }
  end
end
info(id) click to toggle source
# File lib/hackpad/cli/client.rb, line 77
def info(id)
  pad = Pad.new id
  pad.load 'txt'
  table 'Id', Paint[id, :bold]
  table 'Title', Paint[pad.title, :yellow]
  table 'URI', "#{@workspace.site}/#{id}"
  table 'Chars', "#{pad.chars}"
  table 'Lines', "#{pad.lines}"
  table 'Guest Policy', "#{pad.guest_policy}"
  table 'Moderated', "#{pad.moderated}"
  table 'Cached', "#{pad.cached_at || 'unknown'}"
end
list() click to toggle source
# File lib/hackpad/cli/client.rb, line 59
def list
  @output.puts Padlist.get_list(@config.refresh).map { |pad|
    padline pad
  }
end
show(id, format) click to toggle source
# File lib/hackpad/cli/client.rb, line 90
def show(id, format)
  pad = Pad.new id
  pad.load format
  @output.puts pad.content
end
stats() click to toggle source
# File lib/hackpad/cli/client.rb, line 45
def stats
  table 'Site', Paint[@workspace.site, :blue]
  table 'Cached Pads', Store.count_pads
  table 'Last Refresh', Store.last_refresh || 'not refreshed yet'
end
workspaces() click to toggle source
# File lib/hackpad/cli/client.rb, line 32
def workspaces
  @config.workspaces.each do |s|
    if s.name == @config.workspace
      s.name = "> #{s.name}"
    end
    table s.name, s.site
  end
end

Private Instance Methods

extract(s) click to toggle source
# File lib/hackpad/cli/client.rb, line 106
def extract(s)
  unescape(s).gsub(/<b class="hit">([^<]*)<\/b>/) { Paint[Regexp.last_match[1], :cyan, :bold] }
end
id_or_url(id) click to toggle source
# File lib/hackpad/cli/client.rb, line 114
def id_or_url(id)
  "#{(@workspace.site + '/') if @config.urls}#{Paint[id, :bold]}"
end
padline(pad) click to toggle source
# File lib/hackpad/cli/client.rb, line 98
def padline(pad)
  "#{(@workspace.site + '/') if @config.urls}#{pad.id} - #{pad.title}"
end
table(key, value) click to toggle source
# File lib/hackpad/cli/client.rb, line 110
def table(key, value)
  @output.printf "%-20s %s\n", key, value
end
unescape(s) click to toggle source
# File lib/hackpad/cli/client.rb, line 102
def unescape(s)
  CGI.unescapeHTML s
end