class Sheet

Constants

SHEETS_DIR

Public Class Methods

command_available?(cmd) click to toggle source

Utility to check wherever a command is available in the user system

# File lib/sheet.rb, line 83
def command_available?(cmd)
  %x!bash -c "type #{cmd}" 2>/dev/null!.chomp.length > 0 rescue false
end
copy_command() click to toggle source

Returns the copy to clipboard command or nil if no command is found

# File lib/sheet.rb, line 77
def copy_command
  ['pbcopy', 'xclip'].find { |cmd| command_available?(cmd) }
end
display(message) click to toggle source

Utility to write to standard output

# File lib/sheet.rb, line 15
def display(message)
  puts message
end
editor() click to toggle source

@return [String] Used to check the preferred editor for the user

# File lib/sheet.rb, line 52
def editor
  e = exec("echo $EDITOR").chomp
  if e == ""
    e = exec("echo $VISUAL").chomp
  end
  e
end
exec(cmd, replace_current_process=false) click to toggle source

Utility to execute system commands

# File lib/sheet.rb, line 20
def exec(cmd, replace_current_process=false)
  if replace_current_process
    Kernel.exec cmd
  else
    %x!#{cmd}!
  end
end
new(*args) click to toggle source

Creates a new instance of Sheet, usually followed by a call to {#process} @param [Array] args command line options

# File lib/sheet.rb, line 96
def initialize(*args)
  @args = args.flatten
end
open_command() click to toggle source

If we’re using mac, we should use open to open urls. If we’re using linux, we can probably use xdg-open Otherwise return nil

# File lib/sheet.rb, line 63
def open_command
  if RUBY_PLATFORM =~ /darwin/
    'open'
  elsif RUBY_PLATFORM =~ /linux/ && command_available?('xdg-open')
    'xdg-open'
  elsif RUBY_PLATFORM =~ /cygwin/
    'cygstart'
  else
    nil
  end
end
sheet_exists?(name) click to toggle source

@param [String] name the sheet name @return [true] Used to check if a sheet exists

# File lib/sheet.rb, line 46
def sheet_exists?(name)
  name && File.exists?(sheet_path(name))
end
sheet_path(name) click to toggle source

@param [String] name the sheet name @return [String] Returns the path of a sheet, doesn’t check if the file exists

# File lib/sheet.rb, line 31
def sheet_path(name)
  File.join(sheets_dir, name)
end
sheets_dir() click to toggle source

Where the sheets directory is (absolute path)

This defaults to ~/.sheets, but can be overridden by the SHEETS_DIR env var

# File lib/sheet.rb, line 39
def sheets_dir
  File.expand_path(ENV['SHEETS_DIR'] || SHEETS_DIR)
end
sheets_directory_exists?() click to toggle source

Returns true if ~/.sheets exists

# File lib/sheet.rb, line 88
def sheets_directory_exists?
  File.directory?(Sheet.sheets_dir)
end

Public Instance Methods

process() click to toggle source

Where the dispatching really happens. We check to see what the user intended to do and then instantiate the proper class TODO: refactor in a switch statement

# File lib/sheet.rb, line 103
def process
  if ['new', 'edit'].include?(@args[0])
    write(@args[1])
  elsif ['ls', 'list'].include?(@args[0]) || @args.empty?
    list
  elsif ['cp', 'copy'].include?(@args[0])
    copy(@args[1])
  else
    open(@args[0])
  end
end

Private Instance Methods

copy(name) click to toggle source
# File lib/sheet.rb, line 128
def copy(name)
  Sheet::Copy.new(name).copy
end
list() click to toggle source
# File lib/sheet.rb, line 124
def list
  Sheet::List.new.list
end
open(name) click to toggle source
# File lib/sheet.rb, line 116
def open(name)
  Sheet::Open.new(name).open
end
write(name) click to toggle source
# File lib/sheet.rb, line 120
def write(name)
  Sheet::Write.new(name).write
end