Skip to content

Commit

Permalink
Adding remote tracking status check.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjoes committed Jun 16, 2019
1 parent 44def13 commit c2a53e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"repositoryURL": "https://github.com/hanjoes/swift-git",
"state": {
"branch": null,
"revision": "fec4d4b0cfade6512045861ec47bfdc73734409c",
"version": "1.3.5"
"revision": "b3cd69a11cc9143f10b7bede5ec1943c9ae134a1",
"version": "1.4.0"
}
},
{
"package": "SwiftPawn",
"repositoryURL": "https://github.com/hanjoes/swift-pawn",
"state": {
"branch": null,
"revision": "728a0505a90da404d5b8b099e50b2d9857414edb",
"version": "1.0.2"
"revision": "a547b3f421f5a6d220e419e4b625f89ece76c39d",
"version": "1.0.3"
}
},
{
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ It provides below information:
* user login
* host ip address
* git status
* whether we are tracking ("~" means we are tracking)

## Non-blocking
The "swift_prompt_nanny" is a daemon meant for synchronizing your repository with remote. It kicks in every set amount of seconds (currently hard-coded 10s). Press "ENTER" to continue if you see a question mark blinking (shown in the demo below).
Expand Down
38 changes: 23 additions & 15 deletions Sources/SwiftPrompt/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ enum State {
var glyph: String {
switch self {
case .noRepo:
return " " + Glyphs.Cross
return Glyphs.Cross
case .unknown:
return " " + Glyphs.Question
return Glyphs.Question
case .upToDate:
return " " + Glyphs.Check
return Glyphs.Check
case .newer:
return " " + Glyphs.Up
return Glyphs.Up
case .older:
return " " + Glyphs.Cross
return Glyphs.Cross
case .updating:
return " " + Colors.Blink + Glyphs.Question
return Colors.Blink + Glyphs.Question
}
}
}
Expand All @@ -83,10 +83,10 @@ struct C {
}

func getPrompt(_ login: String, _ host: String, _ branch: String,
_ state: State, _ asterisk: String) -> String {
_ state: State, _ asterisk: String, _ link: String) -> String {
return "\(Colors.Yellow)\(login)\(Colors.Reset)"
+ "@\(Colors.Red)\(host)\(Colors.Reset)"
+ " \(state.graphicsMode)(\(asterisk)\(branch)\(Colors.Reset)"
+ " \(state.graphicsMode)(\(asterisk)\(branch)\(Colors.Reset)\(link)"
+ "\(state.glyph)\(state.graphicsMode))\(Colors.Reset)"
}

Expand Down Expand Up @@ -142,11 +142,13 @@ func main() throws {

let login = String(cString: &buffer)

var link = " "
var branch = "no repository"
let isRepo = SwiftGit.isRepo(at: cwd)
if isRepo {
if let branchName = try SwiftGit.branchName(at: cwd) {
branch = branchName
link = " "
}
}

Expand All @@ -155,21 +157,23 @@ func main() throws {

let host = try getHostName()
let asterisk = try isRepo && SwiftGit.isModified(at: cwd) ? "*" : ""
var prompt = getPrompt(login, host, branch, state, asterisk)
var prompt = getPrompt(login, host, branch, state, asterisk, link)

// assemble the prompt.
// each prompt script is executed once and
var t = Termbo(width: 100, height: 1)

if isRepo {
let remote = SwiftGit.findTrackingRemote(at: cwd, branch: branch)
link = remote != nil ? "~" : " "
var lkfd = acquiredLock(lockf: lockf)
if lkfd == -1 {
state = .updating
prompt = getPrompt(login, host, branch, state, asterisk)
prompt = getPrompt(login, host, branch, state, asterisk, link)
t.render(bitmap: [prompt], to: stdout)
_ = try SwiftPawn.execute(command: "stty",
arguments: ["stty", "-isig",
"-echo", "-icanon"])
arguments:
["stty", "-isig", "-echo", "-icanon"])
defer { _ = try! SwiftPawn.execute(command: "stty",
arguments: ["stty", "sane"]) }
while true {
Expand All @@ -183,12 +187,16 @@ func main() throws {
if lkfd == -1 {
state = .unknown
} else {
state = try getDiff(cwd, isRepo, branch)
if remote != nil {
state = try getDiff(cwd, isRepo, branch)
}
}
} else {
state = try getDiff(cwd, isRepo, branch)
if remote != nil {
state = try getDiff(cwd, isRepo, branch)
}
}
prompt = getPrompt(login, host, branch, state, asterisk)
prompt = getPrompt(login, host, branch, state, asterisk, link)

// run nanny for logistics

Expand Down

0 comments on commit c2a53e5

Please sign in to comment.