Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active June 17, 2016 04:14
Show Gist options
  • Save thomasjslone/e142f077f0dc434867f762aa7194f952 to your computer and use it in GitHub Desktop.
Save thomasjslone/e142f077f0dc434867f762aa7194f952 to your computer and use it in GitHub Desktop.
Navigator based on dnav.rb app i built months ago, this one can access directories on local host,and urls, its pretty lacking, oh and incomplete, IM NOT DONE YET so dont try to run it or anything, this is just a progress update
##########################################################################################################
## ##
## App Name: Dnav ##
## App Version: 1.0.3 ##
## App Manufacturer: Thomas Technical ##
## Release Date: 2016-5-20 ##
## ##
## Description: ##
## ##
## Navigator can brows directories on the local host or remote hosts on the web ##
## ##
## Features: ##
## ##
## ##
##########################################################################################################
$dnav_release_date = "2016-5-26"
$dnav_version = "1.0.3"
begin
require 'rbconfig'
require 'open-uri'
require 'mechanize'
rescue
end
class Navigator
def initialize(dir)
if dir.to_s == nil
@curdir = Dir.getwd.to_s.split("/")[0].to_s + "/"
elsif File.directory?(dir)
@curdir = dir.to_s
else
@curdir = "[INVALID]"
end
@supported_os = ["mingw32", "mingw64", "other"]
begin
@host_os = RbConfig::CONFIG["host_os"].to_s
rescue
@host_os = "UNDETERMINABLE"
end
@elevated = false
@clipboard = []
@del_code = "0000"
@del_dir = ""
$obj = ""
@curobjs = []
@history = []
@hist_time = []
@html = Mechanize.new
end
def startup_splash
return curdir_summary.to_s
end
def curdir_summary
if File.directory?(@curdir.to_s)
return directory_summary.to_s
elsif File.exist?(@curdir.to_s)
return local_file_summary.to_s
elsif @curdir[0..6].to_s == "http://" or @curdir[0..7].to_s == "https://"
return web_page_summary.to_s
end
end
def directory_summary
str = "\nLocation: " + @curdir.to_s
fc = 0
sc = 0
Dir.foreach(@curdir.to_s) do |o|
if o.to_s != "." and o.to_s != ".."
if File.file?(@curdir.to_s + "/" + o.to_s)
fc += 1
elsif File.directory?(@curdir.to_s + "/" + o.to_s)
sc += 1
end
end
end
str << "\nFiles: " + fc.to_s + " , Folders: " + sc.to_s
if fc.to_i == 0 and sc.to_i == 0
str << "\nDirectory is empty.\n"
else
str << "\nContents:\n\n"
end
@le = []
Dir.foreach(@curdir.to_s) do |o|
if o.to_s != "." and o.to_s != ".."
if File.file?(@curdir.to_s + "/" + o.to_s)
@le << "File - "
elsif File.directory?(@curdir.to_s + "/" + o.to_s)
@le << "Directory - "
end
end
end
@ri = []
Dir.foreach(@curdir.to_s) do |o|
if o.to_s != "." and o.to_s != ".."
if File.file?(@curdir.to_s + "/" + o.to_s)
@ri << o.to_s
elsif File.directory?(@curdir.to_s + "/" + o.to_s)
@ri << o.to_s
end
end
end
i = 0
st = ""
@le.each do |l|
sp = ""
t = 15 - l.to_s.length.to_i
t.times { sp << " " }
p = @curdir.to_s + "/" + @ri[i].to_s
st << l.to_s + sp.to_s + @ri[i].to_s + "\n"
i += 1
end
s = ""
i = 0
objs = []
Dir.foreach(@curdir.to_s) do |d|
if d.to_s != "." and d.to_s != ".."
objs << d.to_s
end
end
st.to_s.split("\n").each do |l|
spacer = ""
p = @curdir.to_s + "/" + objs[i.to_i].to_s
if File.directory?(p)
d = map_directory(@curdir.to_s + "/" + @ri[i].to_s)
r = " Files: " + d[1].length.to_s + " , Folders: " + d[0].length.to_s
#r = " Total Size: " + format_size(directory_size(p.to_s).to_s).to_s + " bytes , Files: " + d[1].length.to_s + " , Folders: " + d[0].length.to_s
elsif File.file?(p)
r = " Size: " + format_size(File.size(p.to_s).to_s).to_s
end
t = 78 - l.to_s.length.to_i
t.times { spacer << " "}
s << l.to_s + spacer.to_s + r.to_s + "\n"
i += 1
end
str << s.to_s
str << "\nEnter a file, folder or command."
@curobjs = Dir.entries(@curdir.to_s)
return str.to_s
end
def navigate_to(str)
if File.directory?(str)
@curdir = str.to_s
return true
elsif File.exist?(str)
@curdir = str.to_s
return true
elsif str[0..6].to_s == "http://" or str[0..7].to_s == "https://"
puts "you entered a website"
#begin
file = open(str.to_s)
file.close
@curdir = str.to_s
@history << str.to_s
return true
#rescue
return false
#end
else
return false
end
end
def get_dir_info
str = "\n\nDirectory Information on Location: " + @curdir.to_s
map = map_directory(@curdir.to_s)
str << "\nFiles: " + map[1].length.to_s + " , Folders: " + map[0].length.to_s
str << "\nTotal Size: " + directory_size(@curdir.to_s).to_s + "\n"
return str.to_s
end
def local_file_summary
fn = @curdir.to_s.split("/")[-1].to_s
str = "File Location: " + @curdir.to_s
str << "\nFile Name: " + fn.to_s
str << "\nFormat: " + fn.split(".")[1].to_s.upcase
str << "\nSize: " + format_size(File.size(@curdir.to_s).to_s).to_s
return str.to_s
end
def web_page_summary
file = open(@curdir.to_s)
page = file.read.to_s
file.close
str = "Website Location: " + @curdir.to_s
str << "\nLinks:"
page = @html.get(@curdir.to_s)
@curobjs = []
@currefs = []
page.links.each do |link|
@curobjs << link.text.to_s
@currefs << link.href.to_s
end
s = []
@curobjs.each do |link|
s << link.to_s + " @ "
end
s = cons_list_spacer(10, s, @currefs)
str < "\n" + s.to_s + "\n"
return "\n" + str.to_s + "\n"
end
def get_file_text
begin
f = File.open(@curdir.to_s,"rb")
s = f.read.to_s
f.close
s = "\n\n######### File Begin: " + @curdir.to_s + "##########\n" + s.to_s + "\n######### File End: " + @curdir.to_s + "############\n\n"
return s.to_s
rescue
return "File Inaccesable."
end
end
def map_directory(dir) ## can be string or array of strings
files = []
subdirectories = []
remaining_directories = [dir.to_s]
until remaining_directories.length.to_i == 0
cd = remaining_directories[0]
begin
Dir.foreach(cd) do |d|
if d.to_s != "." and d.to_s != ".."
if File.file?(cd.to_s + "/" + d.to_s)
files << cd.to_s + "/" + d.to_s
elsif File.directory?(cd.to_s + "/" + d.to_s)
subdirectories << cd.to_s + "/" + d.to_s
remaining_directories << cd.to_s + "/" + d.to_s
end
end
end
rescue
## subdirectory unaccessable, cant be includded with the rest of the mapping, is included as an unmapped directory which will appear empty
end
remaining_directories.delete_at(0)
end
return [subdirectories,files] ##this array of two arrays will be considered a mapping
end
def directory_size(dir)
files = []
subdirectories = []
remaining_directories = [dir.to_s]
until remaining_directories.length.to_i == 0
cd = remaining_directories[0]
begin
Dir.foreach(cd) do |d|
if d.to_s != "." and d.to_s != ".."
if File.file?(cd.to_s + "/" + d.to_s)
files << cd.to_s + "/" + d.to_s
elsif File.directory?(cd.to_s + "/" + d.to_s)
remaining_directories << cd.to_s + "/" + d.to_s
end
end
end
rescue
##access denied error will go on silently in the background as it is skipped from the measurement
end
remaining_directories.delete_at(0)
end
size = 0
files.each do |f|
size += File.size(f).to_i
end
return size.to_i
end
def format_size(n)
size = n.to_i
units = ''
if n.to_i < 1024
units = "B "
elsif n.to_i > 1023 and n.to_i < 1048576 ## in kb range
size = size.to_f / 1024
units = "KB"
elsif n.to_i > 1048575 and n.to_i < 1073741824 ## in mb range
2.times do
size = size.to_f / 1024
end
units = "MB"
elsif n.to_i > 1073741823 and n.to_i < 1099511627776 ## in gb range
3.times do
size = size.to_f / 1024
end
units = "GB"
elsif n.to_i > 1099511627775 and n.to_i < 1125899906842624 ## in tb range
4.times do
size =size.to_f / 1024
end
units = "TB"
elsif n.to_i > 1125899906842623 and n.to_i < 1152921504606846976 ## in pb range
5.times do
size = size.to_f / 1024
end
units = "PB"
elsif n.to_i > 1152921504606846975
size = size
units = "B"
end
str = ''
s = size.to_s
n = 0
@decimal = 0
if s.to_s.include?(".")
s = s.split(".")[0].to_i
@decimal = s.to_s.split(".")[1].to_s
else
s = s.to_i
@decimal = "NONE"
end
s = s.to_s.split("").reverse
i=0
s.each do |nc|
if i == 2
i=0
str << nc.to_s + ","
else
str << nc.to_s
i+=1
end
end
if str.to_s[-1].to_s == ","
str = str.reverse.to_s.split("")[1..-1].join("").to_s
else
str = str.reverse.to_s
end
if size.to_s.include?(".")
str = str.to_s + "." + size.to_s.split(".")[-1].to_s[0..1].to_s + " " + units.to_s
else
str = str.to_s + " " + units.to_s
end
return str
end
def cons_list_spacer(spaces,left,right) # (integer,array,array) makes a printable multi line list of the given labels and data w/ spacing
width = spaces .to_i+ 5
str = ''
i = 0
left.each do |l|
spacer = ""
t = width - l.length.to_i
t.to_i.times { spacer << " " }
str << l.to_s + spacer.to_s + right[i].to_s + "\n"
i += 1
end
puts str.to_s
end
def shell_in(inp)
if File.directory?(@curdir.to_s)
objs = @curobjs
if objs.include?(inp.to_s)
##user entered either a file or subdirectory that is in the current directory
@curdir = @curdir.to_s + "/" + inp.to_s
return curdir_summary.to_s
elsif inp.to_s == "size"
puts "Please wait while measuring directory contents..."
m = map_directory(@curdir.to_s)
str = "\nFiles: " + m[1].length.to_s + ", Folders: " + m[0].length.to_s
str << "\n" + @curdir.to_s + " Total Size: " + format_size(directory_size(@curdir.to_s).to_s).to_s
return str.to_s
elsif inp[0..3].to_s == "nav:"
if navigate_to(inp[4..-1].to_s) != false
return curdir_summary.to_s
else
return "Error: Invalid Location: " + inp[4..-1].to_s
end
elsif inp[0..15].to_s == "delete directory"
if @del_code.to_s != "0000" and @curdir.to_s == @del_dir.to_s
if @del_code.to_s == inp[17..20].to_s
begin
map = map_directory(@curdir.to_s)
map[1].each do |f|
File.delete(f)
end
map[0].each do |d|
Dir.delete(d)
end
Dir.delete(@del_dir.to_s)
@del_code = "0000"
d = @del_dir.to_s
@del_dir = ""
@curdir = @curdir.to_s.split("/")[0..-2].join("/").to_s + "/"
return "Directory was deleted: " + d.to_s + curdir_summary.to_s
rescue
@del_code = "0000"
return "Error : Permission/Access Issues"
end
else
@del_dir = ""
@del_code = "0000"
return "Permission Denied : Invalid Delete Code"
end
elsif @del_code.to_s == "0000"
@del_code = rand(10).to_s + rand(10).to_s + rand(10).to_s + rand(10).to_s
@del_dir = @curdir.to_s
return "Delete Code: " + @del_code.to_s
end
elsif inp[0..6].to_s == "delete:"
if File.file?(@curdir.to_s + "/" + inp[7..-1].to_s)
##begin
File.delete(@curdir.to_s + "/" + inp[7..-1].to_s)
return "\nFile was deleted: " + @curdir.to_s + inp.to_s[7..-1].to_s + "\n" + curdir_summary.to_s
##rescue
#return "ERROR : Unable to delete file."
##end
elsif File.directory?(@curdir.to_s + "/" + inp[7..-1].to_s)
map = map_directory(inp[7..-1].to_s)
if map[0].length == 0 and map[1].length == 00
Dir.delete(inp[7..-1].to_s)
return "\nDirectory was deleted: " + @curdir.to_s + "/" + inp.to_s + "\n" + curdir_summary.to_s
else
return "Can not delete a folder this way unless it is empty.\n Navigate to it to and use 'delete directory'"
end
else
return "No such file or folder: " + inp[7..-1].to_s
end
elsif inp[0..5].to_s == "clone:"
if File.directory?(@curdir.to_s + "/" + inp[6..-1].to_s)
begin
cd = @curdir.to_s + "/" + inp[6..-1].to_s
map = map_directory(cd.to_s)
nfiles = []
ndirs = []
map[1].each do |fp|
nfiles << fp.to_s.split(@curdir.to_s + "/" + inp[6..-1].to_s)[-1].to_s
end
map[0].each do |dp|
ndirs << dp.to_s.split(@curdir.to_s + "/" + inp[6..-1].to_s)[-1]
end
if Dir.entries(@curdir).include?(inp[6..-1].to_s)
dn = inp[6..-1].to_s + " - Copy"
elsif Dir.entries(@curdir).include?(inp[6..-1].to_s + " - Copy")
dn = inp[6..-1].to_s + " - Copy " + rand(10).to_S
end
np = @curdir.to_s + "/" + dn.to_s
Dir.mkdir(np.to_s)
ndirs.each do |nd|
Dir.mkdir(@curdir.to_s + "/" + dn.to_s + "/" + nd.to_s)
end
i = 0
begin
mf = []
nfiles.each do |nfp|
begin
file = File.open(map[1][i].to_s,"rb")
dat = file.read.to_s
file.close
file = File.open(@curdir.to_s + "/" + dn.to_s + "/" + nfp.to_s,"wb")
file.write(dat.to_s)
file.close
i += 1
rescue
mf << map[1][i].to_s
end
end
return "Directory was cloned: " + @curdir.to_s + "/" + inp.to_s + curdir_summary.to_s
rescue
return "ERROR: Directory not sucessfully cloned because files missing: " + mf.join(", ").to_s
end
return "Directory was cloned: " + @curdir.to_s + "/" + inp.to_s + curdir_summary.to_s
rescue
return "Error : Permission/Access Issue."
end
elsif File.file?(@curdir.to_s + "/" + inp[6..-1].to_s)
file = File.open(@curdir.to_s + "/" + inp.to_s[6..-1].to_s,"rb")
data = file.read.to_s
file.close
if inp[6..-1].to_s.include?(".")
n = inp[6..-1].to_s.split(".")[0].to_s
e = inp[6..-1].to_s.split(".")[1].to_s
nname = n.to_s + " - Copy." + e.to_s
else
nname = inp[6..-1].to_s + " - Copy "
end
begin
file = File.open(@curdir.to_s + "/" + nname.to_s,"wb")
file.write(data.to_s)
file.close
return "File was copied: " + @curdir.to_s + "/" + nname.to_s + "\n" + curdir_summary.to_s
rescue
return "Error : Permission/Access Issues"
end
else
return "No such file or directory: " + inp[6..-1].to_s
end
elsif inp.to_s == "home"
@curdir = @curdir.to_s.split("/")[0].to_s + "/"
return curdir_summary.to_s
elsif inp.to_s == "commands"
str = "\n\nDirectory Command List:"
str << "\n - commands View command list anywhere to see what you can do. "
str << "\n - nav: Change your location to a directory you've entered. "
str << "\n - back Go to the parent directory of the current directory. "
str << "\n - home Go to the drive that the current directory resides on. "
str << "\n - refresh Reload location to check for changes made by other programs. "
str << "\n - size Measure the total size of current location. "
str << "\n - info Get file/folder information for directory. "
str << "\n - delete: Delete a file or folder. "
str << "\n - delete directory Delete this directory. "
str << "\n - new file: Create a new file in the current directory. "
str << "\n - new folder: Create a new folder in the current directory. "
str << "\n - clone: Create a copy of a file or folder in current directory. "
str << "\n - search: Search for a term in current directory folder and file names."
str << "\n - back Go back to parent directory of file.\n"
str << "\nYou can enter a file or folder name to navigate to it. \nThere are slightly different commands for files."
return str.to_s
elsif inp.to_s == "info"
return get_dir_info.to_s
elsif inp.to_s == "refresh"
return curdir_summary.to_s
elsif inp[0..8].to_s == "new file:"
if @host_os.to_s == "mingw32" or @host_os.to_s == "mingw64" ## use windows file creation app
@file = @curdir.to_s + "/" + inp[9..-1].to_s
@line = 0
@text = []
@lines = []
@writing = true
while @writing
str = ''
if @line.to_s.length == 1
str << "00" + @line.to_s
elsif @line.to_s.length == 2
str << "0" + @line.to_s
else
str << @line.to_s
end
str << "|"
print str.to_s
line = gets.chomp.to_s
if line.to_s.include?("{{") and line.to_s.include?("}}") and line.to_s[-2..-1].to_s == "}}" ##command present at end of line
cmd = line.split("{{")[-1].to_s
cmd = cmd[0..-3].to_s
line = line.split("{{")[0].to_s
if cmd.to_s == "end" ## stop writing text
@writing = false
if @lines.include?(@line.to_s) and line.to_s != ""
@text[@lines.index(@line.to_s).to_i] = line.to_s
elsif @lines.include?(@line.to_s.to_s) == false and line.to_s != ""
@text << line.to_s
@lines << @line.to_s
end
elsif cmd[0..4].to_s == "line:"
if @lines.include?(@line.to_s) and line.to_s != ""
@text[@lines.index(@line.to_s).to_i] = line.to_s
elsif @lines.include?(@line.to_s.to_s) == false and line.to_s != ""
@text << line.to_s
@lines << @line.to_s
end
@line += 1
t = cmd[5..-1].to_s !~ /\D/
if t
@line = cmd[5..-1].to_i
end
next
else
##invalid command, ignore
end
else
if @lines.include?(@line.to_s) and line.to_s != ""
@text[@lines.index(@line.to_s).to_i] = line.to_s
elsif @lines.include?(@line.to_s.to_s) == false and line.to_s
@text << line.to_s
@lines << @line.to_s
end
@line += 1
end
end
begin
file = File.open(@file.to_s,"w")
body = @text.join("\n").to_s
file.write(body.to_s)
file.close
return "File created and written: " + @curdir.to_s + "/" + inp.to_s[9..-1].to_s + "\n" + curdir_summary.to_s
rescue
"Error: File access issue."
end
else
## non native os method
begin
File.new(@curdir.to_s + inp[9..-1].to_s,"w")
return "File was created: " + @curdir.to_s + "/" + inp[9..-1].to_s + "\n" + curdir_summary
rescue
return "Unable to create file due to access iussues."
end
end
elsif inp[0..10].to_s == "new folder:"
begin
puts "ABOUT TO CREATE DIR:" + @curdir.to_s + "/" + inp[11..-1].to_s
Dir.mkdir(@curdir.to_s + "/" + inp[11..-1].to_s)
return "Directory was created: " + @curdir.to_s + "/" + inp[11..-1].to_s + "\n" + curdir_summary.to_s
rescue
return "Unable to create directory: " + @curdir.to_s + "/" + inp.to_s[11..-1].to_s
end
elsif inp.to_s == "wexp"
system("explorer ." )
return "A Windows Explorer call was made, you window should have spawned already."
elsif inp.to_s == "back"
if @curdir.split("/").length > 1
if @curdir.to_s.split("/").length == 2
@curdir = @curdir.to_s.split("/")[0..-2].join("/").to_s + "/"
return curdir_summary.to_s
else
@curdir = @curdir.to_s.split("/")[0..-2].join("/").to_s
return curdir_summary.to_s
end
else
return "You are in the " + @curdir.to_s + " directory, you can not go back any further."
end
elsif inp[0..6].to_s == "search:" #### search directory for files and folders matching terms
if inp.length > 7
if inp[7..-1].to_s.include?(",")
terms = inp[7..-1].to_s.split(",").to_a
else
terms = [inp[7..-1].to_s]
end
t = terms ; terms = []
t.each do |te|
terms << te.to_s.downcase
end
map = map_directory(@curdir.to_s)
dirs = map[0]
files = map[1]
dmatches = []
dmt =[]
fmatches = []
fmt = []
dirs.each do |d| ## check directory paths
terms.each do |t|
if d.to_s.downcase.include?(t.to_s.downcase)
dmatches << d.to_s
dmt << t.to_s
end
end
end
files.each do |d| ## check file paths
terms.each do |t|
if d.to_s.downcase.split("/")[-1].to_s.include?(t.to_s.downcase)
fmatches << d.to_s
fmt << t.to_s
end
end
end
tm = dmatches.length.to_i + fmatches.length.to_i
str = ""
if tm.to_i < 1
str = str + "\nThere were no matches"
return str.to_s
else
str = "Matches: " + tm.to_s + "\n"
if dmt.length.to_i > 0
str << "\n" + cons_list_spacer(7,dmt,dmatches).to_s + "\n\n\n"
end
if fmt.length.to_i > 0
str = + cons_list_spacer(7,fmt,fmatches).to_s + "\n\n\n"
end
return str.to_s
end
else
return "No search term in input."
end
else
return inp.to_s + " is not a file, folder or command. Enter 'commands' to see directory commands list."
end
elsif File.file?(@curdir.to_s)
if inp.to_s == "view text"
return get_file_text.to_s
elsif inp.to_s == "info"
return local_file_summary.to_s
elsif inp.to_s == "view bytes"
b = []
f = File.open(@curdir.to_s,"rb")
f.each_byte do |by|
b << by.to_s
end
f.close
b = "file Bytes: " + @curdir.to_s + "\n" + b.join("").to_s + "\n###End of file: " + @curdir.to_s
return b.to_s
elsif inp.to_s == "back"
if @curdir.to_s.split("/").length == 2
@curdir = @curdir.to_s.split("/")[0..-2].join("/").to_s + "/"
else
@curdir = @curdir.to_s.split("/")[0..-2].join("/").to_s
end
return curdir_summary.to_s
elsif inp.to_s == "open" ################## ASK HOST TO EXECUTE FILE #############
dir = @curdir.to_s.split("/")[0..-2].join("\\").to_s + "\\"
obj = @curdir.to_s.split("/")[-1].to_s
begin
system("CHDIR " + dir.to_s)
if obj[-4..-1].to_s == ".lnk"
system("explorer " + obj.to_s)
elsif obj[-3..-1].to_s == ".rb"
system("irb -r " + @curdir.to_s)
else
system(obj.to_s)
end
return obj.to_s + " was executed by the host system. Feedback: "
rescue
return "Execution failed: " + @curdir.to_s
end
elsif inp[0..7].to_s == "copy to:" ################ make a copy of this elsewhere ##################
if File.directory?(inp[8..-1].to_s)
if @curdir.to_s == inp[8..-1].to_s
n = @curdir.to_s.split("/")[-1].to_s
if n.include?(".")
e = n.split(".")[-1].to_s
n = n.to_s + " - Copy." + e.to_s
else
n = n.to_s + " - Copy"
end
else
n = @curdir.to_s.split("/")[-1].to_s
end
ndir = inp[8..-1].to_s + "/" + n.to_s
dat = ''
begin
file = File.open(@curdir.to_s,"rb")
dat = file.read.to_s
file.close
begin
file = File.open(ndir.to_s, "wb")
file.write(dat.to_s)
file.close
return "File was copied to: " + ndir.to_s
rescue
return "Error : Permission/Access Issue, can not access directory: " + ndir.to_s.split("/")[0..-2].join("/").tos_
end
rescue
return "ERROR : Permission/Access Issue, can not make a copy of this file: " + @curdir.to_s
end
else
return "No such directory: " + inp[8..-1].to_s
end
elsif inp.to_s == "clone" ################ make a copy of this nearby with slightly different name ######
if File.file?(@curdir.to_s)
n = @curdir.to_s.split("/")[-1].to_s
if Dir.entries(@curdir.to_s.split("/")[0..-2].join("/").to_s).include?(n.to_s + " - Copy")
if n.include?(".")
e = n.split(".")[-1].to_s
n = n.to_s + " - Copy" + rand(10).to_s + "." + e.to_s
else
n = n.to_s + " - Copy" + rand(10).to_s
end
else
if n.include?(".")
e = n.split(".")[-1].to_s
n = n.to_s + " - Copy" + "." + e.to_s
else
n = n.to_s + " - Copy"
end
end
np = @curdir.to_s.split("/")[0..-2].join("/").to_s + "/" + n.to_s
begin
file = File.open(@curdir.to_s,"rb")
dat = file.read.to_s
file.close
begin
file = File.open(np.to_s,"wb")
file.write(dat.to_s)
file.close
return "File was copied: " + np.to_s
rescue
return "RARE ERROR : Unable to write to this directory: " + @curdir.to_s
end
rescue
return "ERROR : Permission/Access Issue, File inaccessable: " + @curdir.to_s
end
else
return "RARE ERROR : @CURDIR seems to not be a file!"
end
elsif inp[0..5].to_s == "delete" ################ make a copy of this nearby with slightly different name ######
if @del_code.to_s != "0000"
if @del_dir.to_s == @curdir.to_s
if @del_code.to_s == inp[7..-1].to_s
begin
res = @curdir.to_s.split("/")[0..-2].join("/").to_s
File.delete(@curdir.to_s)
@curdir = res.to_s
d = @del_dir.to_s
@del_dir = ""
@del_code = "0000"
return "File was deleted: " + d.to_s + curdir_summary.to_s
rescue
d = @del_dir.to_s
@del_dir = ""
@del_code = "0000"
return "ERROR : Permission/Access Issue : Unable to delete file: " + d.to_s
end
else
@del_dir = ""
@del_code = "0000"
return "Permission Denied : Invalide Delete Code"
end
else
d = @del_dir.to_s
@del_dir = ""
c = @del_code.to_s
@del_code = "0000"
return "RARE ERROR : You changed directories after obtaining this delete code: " + c.to_s + " Intended Delete: " + d.to_s
end
else
@del_code = rand(10).to_s + rand(10).to_s + rand(10).to_s + rand(10).to_s
@del_dir = @curdir.to_s
return "Delete Code: " + @del_code.to_s
end
elsif inp[0..6].to_s == "search:" ################ make a copy of this nearby with slightly different name ######
#begin
if inp[7..-1].to_s.include?(",")
terms = inp[7..-1].to_s.split(",").to_a
else
terms = [inp[7..-1].to_s]
end
file = File.open(@curdir.to_s,"rb")
lines = file.read.to_s.split("\n")
file.close
matched_lines = []
matched_terms = []
line_occurence_pos = []
i = 0
lines.each do |l|
terms.each do |t|
if l.to_s.downcase.include?(t.to_s)
matched_lines << i.to_s
matched_terms << t.to_s
line_occurence_pos << l.to_s.index(t.to_s).to_s
end
end
i += 1
end
tt = matched_terms.length.to_s
label1 = []
i = 0
matched_lines.each do |l|
if matched_lines[i].to_s.length == 1
mll = "00" + matched_lines[i].to_s
elsif matched_lines[i].to_s.length == 2
mll = "0" + matched_lines[i].to_s
elsif matched_lines[i].to_s.length == 3
mll = matched_lines[i].to_s
end
label1 << "Line" + mll.to_s + " Pos: " + line_occurence_pos[i].to_s + " : "
i += 1
end
str = "Occurences of terms in file: " + tt.to_s
s = cons_list_spacer(7,label1,matched_terms).to_s
str << "\n" + s.to_s + "\n"
return str.to_s
#rescue
# return "ERROR : Permission/Access Issue, Unable to read file."
#end
elsif inp[0..3].to_s == "nav:"
if File.directory?(inp[4..-1].to_s)
begin
Dir.foreach(inp[4..-1].to_s) do |d|
end
accessable = true
rescue
accessable = false
end
if accessable
@curdir = inp[4..-1].to_s
return curdir_summary.to_s
else
return "ERROR : Permission/Access Issue"
end
else
return "No such directory: " + inp[4..-1].to_s
end
elsif inp.to_s == "commands"
str = "\n\nFile Command List:"
str << "\n - info View file information. (Windows Only)"
str << "\n - view text Read file as text on screen."
str << "\n - view bytes Read file bytes on screen."
str << "\n - execute Attempt to execute file using a system call."
str << "\n - delete Delete the current file."
str << "\n - copy to: Make a copy of this file elsewhere."
str << "\n - clone Make a copy of this file in the same directory as this file."
str << "\n - search file: Search the files data for one or more terms."
str << "\n - nav: Change your location to a directory you've entered."
str << "\n - back Go back to parent directory of file.\n"
return str.to_s
else
return "Invalid file command: " + inp.to_s + " Enter 'commands' for commands list."
end
elsif @curdir[0..6].to_s == "http://" or @curdir[0..7].to_s == "https://" ## curdir is a remote file
if @curobjs.include?(inp)
begin
navigate_to(@currefs[@curobjs.index(inp).to_i])
return web_page_summary.to_s
rescue
return "Error : Could not acces link " + inp.to_s + " @ " + @currefs[@curobjs.index(inp).to_i].to_s
end
else
if inp.to_s == "refresh"
return web_page_summary.to_s
elsif inp.to_s == "commands"
str = "\n\nFile Command List:"
str << "\n - commands View this list."
str << "\n - refresh Reload webpage "
str << "\n - save Save webpagge as html file. "
str << "\n - save: Save link or image as file. "
str << "\n - back Go to previous webpage. "
str << "\n - nav: Go to url or link. "
str << "\n - history View website history. "
str << "\n - page info View information about page. "
str << "\n - exit Go back to parent directory of file.\n"
return str.to_s
elsif inp.to_s == "save"
wait = true
canceled = false
print "Enter location to save " + @page_title.to_s + ":"
while wait
inp = gets.chomp.to_s
if File.directory?(inp)
wait = false
@inp = inp
elsif inp.to_s == "cancel"
wait = false
canceled = true
else
print "Please enter a valid directory to save " + @page_title.to_s + " to:"
end
end
if canceled == false
file = open(@curdir.to_s)
page = file.read.to_s
file.close
name = @inp.to_s + "/" + @page_title.to_s + ".html"
file = File.open(name.to_s,"wb")
file.write(page.to_s)
file.close
return @page_title.to_s + " was sasved to: " + name.to_s
else
return "Canceled saving webpage."
end
elsif inp.to_s == "back"
if @history.length > 0
loc = @history[-1].to_s
@history.delete_at(-1)
navigate_to(loc.to_s)
return web_page_summary.to_s
else
return "Can not navigate backwards any further."
end
elsif inp[0..3].to_s == "nav:"
if navigate_to(inp[4..-1].to_s) != false
return web_page_summary.to_s
else
return "Invalid location entered: " + inp[4..-1].to_s
end
elsif inp.to_s == "history"
str = "Web History: \n - " + @history.join("\n - ") + "\n"
return str.to_s
elsif inp.to_s == "page info"
return webpage_info.to_s
elsif inp.to_s == "exit"
exit
else
return "Invalid Input: " + inp.to_s
end
end
end
end
end
class NShell
def initialize
@nav = Navigator.new(Dir.getwd.to_s)
@run = false
start
end
def start
puts @nav.startup_splash
@run = true
main_loop
exit
end
def stop
@run = false
end
def main_loop
while @run
$obj = @dnav.instance_variable_get("@curdir").to_s
print "<" + $obj.to_s + ": "
inp = gets.chomp.to_s
if inp.to_s == "exit"
@run = false
elsif inp.to_s == "about"
puts "App Name : Navigator"
puts "Manufacturer : ThomasTech"
puts "Version : " + $nav_version.to_s
puts "Release Date : " + $nav_release_date.to_s
puts ""
puts "Navigator for Windows"
puts "Allows you to navigate your file host file system and remote hosts on the web."
else
puts ">" + $obj.to_s + ": " + @nav.shell_in(inp).to_s
end
end
end
end
NShell.new()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment