
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  dir.info.r                                                                             #
  #           Last Updated Funclist  :  08 Feb 2015,  5:14 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   dir.info           ( folder=getwd(), recursive=FALSE, showWarnings=TRUE, output=TRUE, print=NA                           #
  #                        , ..., pattern=NULL, ignore.case=FALSE, all.files=all, all=FALSE )                                  #
  #   print.dir.info     ( x, order.by="modified", decreasing=TRUE, showAllTimes=FALSE )                                       #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

# dir.info.r


dir.info <- function(folder=getwd(), recursive=FALSE, showWarnings=TRUE, output=TRUE, print=NA, ...
										, pattern=NULL, ignore.case=FALSE, all.files=all, all=FALSE) {

  if (!missing(print)) {
    warning("print has been deprecated")
    if (missing(output))
      output <- print
    else
      stop("'output' and 'print' are synonymous, with print being deprecated.\n You cannot have values for both.\n")
  }

	## Check if forgot preceding dot, '.'
	if (!file.exists(folder) && file.exists(dotfolder <- paste0(".", folder))) {
		if (showWarnings) 
			warning("\n   '",  folder, "' does not exist, but \n",  "  '", dotfolder,  "' does exist.\nUsing the latter\n")
		folder <- dotfolder
	}

	files <- dir(path=folder, recursive=recursive, all.files=all.files, pattern=pattern)
	full.files <- dir(folder, full.names=TRUE, recursive=recursive, all.files=all.files, pattern=pattern)

	info <- file.info(full.files)
	info <- data.table(file=files, rn=seq_along(files), info, path=full.files)

	NameChange <- c(mtime="modified", ctime="created", atime="accessed", uname="user", grname="group")
	setnames(info, names(NameChange), NameChange)

	class(info) <- c("dir.info", class(info))

	if (isTRUE(output))
		print(info, ...)

	return(invisible(info))
}

print.dir.info <- function(x, order.by="modified", decreasing=TRUE, showAllTimes=FALSE)  {

	if (inherits(x, "data.table"))
		x <- copy(x)

	if (!is.null(order.by)) {
		order.by <- intersect(order.by, names(x))
		if (!length(order.by)) {
			if (showWarnings)
				warning("No values of `order.by` are in `names(x)`")
		} else {
			x <- x[orderch(order.by, decreasing=decreasing)]
		}
	}

	## Grab which names to display
	nms <- names(x)

	# remove path, grname, gid, uid
	nms <- setdiff(nms, "path")
	nms <- setdiff(nms, c("uid", "gid"))

	if (!showAllTimes)
		nms <- setdiff(nms, c("created", "accessed"))

	if (all(x[["user"]]==Sys.getenv("USER")))
		nms <- setdiff(nms, "user")
	else
		# move it to the end
		nms <- c(setdiff(nms, "user"), "user")

	# remove mode & group, if all mode is the same
	if (all(x[["mode"]]==mean(x[["mode"]])))
		nms <- setdiff(nms, c("group", "mode"))
	else 
		# move it to the end
		nms <- c(setdiff(nms, c("group", "mode")), c("group", "mode"))


	if (all(!x[["isdir"]]))
		nms <- setdiff(nms, "isdir")

	x[, size := formatBytes(size)]

	class(x) <- setdiff(class(x), "dir.info")
	print(x[, nms, with=FALSE])
}

