Skip to content

Instantly share code, notes, and snippets.

@lukesutton
Created December 8, 2016 00:01

Revisions

  1. lukesutton created this gist Dec 8, 2016.
    52 changes: 52 additions & 0 deletions git-status.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    struct StatusSummary {
    let items: [Item]

    enum Staging {
    case staged
    case unstaged
    case unknown
    }

    enum Status {
    case modified
    case untracked
    case deleted
    case renamed
    case unknown
    }

    struct Item {
    let staging: Staging
    let status: Status
    let filename: String
    }
    }

    func parse(item: String) -> StatusSummary.Item {
    let chars = item.characters
    let status = String(chars.prefix(3))
    let file = String(chars.suffix(chars.count - 3))

    switch(status) {
    case " M ":
    return StatusSummary.Item(
    staging: .unstaged,
    status: .modified,
    filename: file
    )
    case "M ":
    return StatusSummary.Item(
    staging: .staged,
    status: .modified,
    filename: file
    )
    default:
    return StatusSummary.Item(
    staging: .unknown,
    status: .unknown,
    filename: file
    )
    }
    }

    print(parse(item: " M derp/what-even.rb"))