//

//  /Users/coti/Documents/lib/swift/DIADir_macos.swift

//

//  Created by coti on 19/02/2016.

//

//  Copyright © 2016 coti. All rights reserved.

//

//   Modification history

//

//   21/02/2016, 18:18

//

// Basically rewritten on 4. 9. 2017, modernized really.

// Makes use of forEach() and filter(), for a much shorter code

/*

 

 The following Strings and URLs try to make it easier to access standard directories.

 Other methods can be used to produce them

 

 These values are easy to use in other classes.

 

 competencias_swift

 

 */

import Foundation


public class DIADir_macos {

  

  static let fm = FileManager.default

  //

  // Unix paths

  //

  public static let pathToHomeDirectory = NSHomeDirectory()

  public static let pathToDesktop = pathToHomeDirectory + "/Desktop"

  public static let pathToDocuments = pathToHomeDirectory + "/Documents"

  public static let pathToTmp = NSTemporaryDirectory()

  

  //

  // URLs

  //

  

  public static let urlToHomeDirectory = URL(string: "file://" + pathToHomeDirectory)

  public static let urlToDesktopDirectory = URL(string: "file://" + pathToDesktop)

  public static let urlToDocumentsDirectory = URL(string: "file://" + pathToDocuments)

  public static let urlToTmpDirectory = URL(string: "file://" + pathToTmp)

  

  //

  // Useful functions

  //

  

  //

  // Simple test for hiddenness

  //

  

  public static func isHidden(path : URL) -> Bool {

    return path.lastPathComponent.hasPrefix(".")

  }

  

  //

  // Given the URL of a directory and an extension, this function produces a list

  // of URLs with that extension.

  //

  // Case is not meaningful for extensions

  //

  

  public static func findFilesIn(directoryAtURL: URL,

                                 withExtension endWith : String) -> [URL] {

    

    do {

      //

      // Get the URLs of all objects found in directoryAtURL

      //

      let contents = try fm.contentsOfDirectory(at: directoryAtURL,

                                                includingPropertiesForKeys: nil ,

                                                options: .skipsSubdirectoryDescendants)

      //

      // Keep only the ones that are not directories

      //

      let allFiles = contents.filter() { !$0.hasDirectoryPath }

      //

      // If the extension is "*", return all files with any extensions

      //

      if "*" == endWith {

        return allFiles

      }

      //

      // Otherwise keep only files with that extension

      //

      let onlyWithExtension = allFiles.filter() { endWith == $0.pathExtension }

      //

      // And return that list

      //

      return onlyWithExtension

    } catch {

      print("\nCould not list files in " + directoryAtURL.path)

    }

    //

    // If for some reason we cannot read the diretory's contents,

    // return an empty list of URLs

    //

    return [URL]()

  }

  

  //

  // Given the URL of a directory, this function produces a the list of directories

  // found in that directory

  //

  

  public static func findDirectoriesIn(directoryAtURL: URL) -> [URL] {

    

    

    do {

      let contents = try fm.contentsOfDirectory(at: directoryAtURL,

                                                includingPropertiesForKeys: nil ,

                                                options: .skipsSubdirectoryDescendants)

      //

      // Keep only the ones that ARE directories

      //

      let onlyDirectories = contents.filter() { $0.hasDirectoryPath }

      //

      // And return the list of directories

      //

      return onlyDirectories

    } catch {

      print("\nCould not list directories in \(directoryAtURL.path)")

    }

    return [URL]()

  }

  

  //

  // Given a list of URLs, this funcion shows a prettyprinted list of any items in the list

  //

  

  public static func showContentsOf(list : [URL]) -> Void {

    

    

    var isDir = false

    var isHidden = false

    

    list.forEach() {

      isDir = $0.hasDirectoryPath

      isHidden = "." == $0.path.characters.first

      let padded = $0.absoluteString.padding(toLength: 60,

                                             withPad: " ",

                                             startingAt: 0)

      print(padded

        + "is a"

        + (isHidden ? " hidden" : "")

        + (isDir ? " directory" : " file"))

    }

  } // End of showContentsOf:

  

  //

  // Given a list of URLs, this funcion returns a prettyprinted string that containsany items in the list

  //

  

  public static func prettyPrintURLsIn(list : [URL]) -> String {

    

    

    var isDir = false

    var isHidden = false

    var result = ""

    list.forEach() {

      isDir = $0.hasDirectoryPath

      isHidden = "." == $0.path.characters.first

      let padded = $0.absoluteString.padding(toLength: 60,

                                             withPad: " ",

                                             startingAt: 0)

      result = padded

        + "is a"

        + (isHidden ? " hidden" : "")

        + (isDir ? " directory" : " file")

        + "\n"

    }

    return result

  } // End of prettyPrintURLsIn

  

}