Class: Sheet
- Inherits:
-
Object
- Object
- Sheet
- Defined in:
- lib/sheet.rb,
lib/sheet/copy.rb,
lib/sheet/list.rb,
lib/sheet/open.rb,
lib/sheet/write.rb
Defined Under Namespace
Classes: Copy, List, Open, Write
Constant Summary collapse
- SHEETS_DIR =
'~/.sheets/'.freeze
Class Method Summary collapse
-
.command_available?(cmd) ⇒ Boolean
Utility to check wherever a command is available in the user system.
-
.copy_command ⇒ Object
Returns the copy to clipboard command or nil if no command is found.
-
.display(message) ⇒ Object
Utility to write to standard output.
-
.editor ⇒ String
Used to check the preferred editor for the user.
-
.exec(cmd, replace_current_process = false) ⇒ Object
Utility to execute system commands.
-
.open_command ⇒ Object
If we’re using mac, we should use open to open urls.
-
.sheet_exists?(name) ⇒ true
Used to check if a sheet exists.
-
.sheet_path(name) ⇒ String
Returns the path of a sheet, doesn’t check if the file exists.
-
.sheets_dir ⇒ Object
Where the sheets directory is (absolute path).
-
.sheets_directory_exists? ⇒ Boolean
Returns true if ~/.sheets exists.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Sheet
constructor
Creates a new instance of Sheet, usually followed by a call to #process.
-
#process ⇒ Object
Where the dispatching really happens.
Constructor Details
Class Method Details
.command_available?(cmd) ⇒ Boolean
Utility to check wherever a command is available in the user system
83 84 85 |
# 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 ⇒ Object
Returns the copy to clipboard command or nil if no command is found
77 78 79 |
# File 'lib/sheet.rb', line 77 def copy_command ['pbcopy', 'xclip'].find { |cmd| command_available?(cmd) } end |
.display(message) ⇒ Object
Utility to write to standard output
15 16 17 |
# File 'lib/sheet.rb', line 15 def display() puts end |
.editor ⇒ String
Used to check the preferred editor for the user
52 53 54 55 56 57 58 |
# 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) ⇒ Object
Utility to execute system commands
20 21 22 23 24 25 26 |
# 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 |
.open_command ⇒ Object
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
63 64 65 66 67 68 69 70 71 72 73 |
# 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) ⇒ true
Used to check if a sheet exists
46 47 48 |
# File 'lib/sheet.rb', line 46 def sheet_exists?(name) name && File.exists?(sheet_path(name)) end |
.sheet_path(name) ⇒ String
Returns the path of a sheet, doesn’t check if the file exists
31 32 33 |
# File 'lib/sheet.rb', line 31 def sheet_path(name) File.join(sheets_dir, name) end |
.sheets_dir ⇒ Object
Where the sheets directory is (absolute path)
This defaults to ~/.sheets, but can be overridden by the SHEETS_DIR env var
39 40 41 |
# File 'lib/sheet.rb', line 39 def sheets_dir File.(ENV['SHEETS_DIR'] || SHEETS_DIR) end |
.sheets_directory_exists? ⇒ Boolean
Returns true if ~/.sheets exists
88 89 90 |
# File 'lib/sheet.rb', line 88 def sheets_directory_exists? File.directory?(Sheet.sheets_dir) end |
Instance Method Details
#process ⇒ Object
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
103 104 105 106 107 108 109 110 111 112 113 |
# 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 |