Skip to content

Instantly share code, notes, and snippets.

@lukesutton
Created December 12, 2016 05:45

Revisions

  1. lukesutton created this gist Dec 12, 2016.
    46 changes: 46 additions & 0 deletions file-navigation.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    struct File {}

    struct Landscape {
    let parents: FileList
    let children: FileList
    }

    struct FileList {
    fileprivate let all: Array<File>
    fileprivate let before: ArraySlice<File>
    fileprivate let after: ArraySlice<File>

    var previous: FileList {
    guard !before.isEmpty else { return self }
    return shift(by: before.count - 1)
    }

    var next: FileList {
    guard !after.isEmpty else { return self }
    return shift(by: before.count + 1)
    }

    var current: File? {
    return before.last
    }

    var isEmpty: Bool {
    return all.isEmpty
    }

    fileprivate func shift(by prefixLength: Int) -> FileList {
    return FileList(
    all: all,
    before: all.prefix(prefixLength),
    after: all.suffix(all.count - prefixLength)
    )
    }
    }

    extension FileList {
    fileprivate init(_ elements: File...) {
    self.all = elements
    self.before = elements.prefix(1)
    self.after = elements.suffix(elements.count - 1)
    }
    }