lib/gloo/objs/system/file_handle.rb in gloo-0.4.0 vs lib/gloo/objs/system/file_handle.rb in gloo-0.5.0
- old
+ new
@@ -1,10 +1,11 @@
# Author:: Eric Crane (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
#
# An object that points to a file in the system.
#
+require 'tty-pager'
module Gloo
module Objs
class FileHandle < Gloo::Core::Obj
@@ -31,13 +32,49 @@
#
# Get a list of message names that this object receives.
#
def self.messages
- return super + %w[read write check_exists check_is_file check_is_dir]
+ basic = %w[read write]
+ checks = %w[check_exists check_is_file check_is_dir]
+ show = %w[show page open]
+ return super + basic + show + checks
end
+ #
+ # Open the file in the default application for the file type.
+ #
+ def msg_open
+ return unless value && File.exist?( value )
+
+ cmd = Gloo::Core::GlooSystem.open_for_platform
+ cmd_with_param = "#{cmd} \"#{value}\""
+ `#{cmd_with_param}`
+ end
+
+ #
+ # Show the contents of the file, paginated.
+ #
+ def msg_page
+ return unless value && File.file?( value )
+
+ pager = TTY::Pager.new
+ pager.page( path: value )
+ end
+
+ #
+ # Show the contents of the file.
+ #
+ def msg_show
+ return unless value && File.file?( value )
+
+ puts File.read( value )
+ end
+
+ #
+ # Read the contents of the file into the object.
+ #
def msg_read
return unless value && File.file?( value )
data = File.read( value )
if @params&.token_count&.positive?
@@ -47,10 +84,13 @@
else
$engine.heap.it.set_to data
end
end
+ #
+ # Write the given data out to the file.
+ #
def msg_write
data = ''
return unless value
if @params&.token_count&.positive?
@@ -58,23 +98,29 @@
data = expr.evaluate
end
File.write( value, data )
end
+ #
# Check to see if the file exists.
+ #
def msg_check_exists
result = File.exist? value
$engine.heap.it.set_to result
end
+ #
# Check to see if the file is a file.
+ #
def msg_check_is_file
result = File.file? value
$engine.heap.it.set_to result
end
+ #
# Check to see if the file is a directory.
+ #
def msg_check_is_dir
result = File.directory? value
$engine.heap.it.set_to result
end
@@ -102,9 +148,12 @@
MESSAGES
read <into.obj> - Read file and put data in the specified object.
If the <into.obj> is not specified, the data will be in <it>.
write <from.obj> - Write the data in the <from.object> into
the file.
+ show - Show the contents of the file.
+ page - Show the contents of the file, paginated
+ open - Open the file with the default application for the type.
check_exists - Check to see if the file exists.
<It> will be true or false.
check_is_file - Check to see if the file specified is a
regular file. <It> will be true or false.
check_is_dir - Check to see if the file specified is a