Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions ChmodHelper.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import SwiftUI
import PlaygroundSupport

struct PermissionGroup {
var read: Bool = false
var write: Bool = false
var execute: Bool = false

var value: Int {
(read ? 4 : 0) + (write ? 2 : 0) + (execute ? 1 : 0)
}
}

extension Binding where Value == PermissionGroup {
var read: Binding<Bool> {
Binding<Bool>(
get: { self.wrappedValue.read },
set: { self.wrappedValue.read = $0 }
)
}
var write: Binding<Bool> {
Binding<Bool>(
get: { self.wrappedValue.write },
set: { self.wrappedValue.write = $0 }
)
}
var execute: Binding<Bool> {
Binding<Bool>(
get: { self.wrappedValue.execute },
set: { self.wrappedValue.execute = $0 }
)
}
}

struct ContentView: View {
@State private var owner = PermissionGroup()
@State private var group = PermissionGroup()
@State private var others = PermissionGroup()

var body: some View {
NavigationView {
Form {
Section(header: Text("Owner")) { toggles(for: $owner) }
Section(header: Text("Group")) { toggles(for: $group) }
Section(header: Text("Others")) { toggles(for: $others) }
Section(header: Text("Octal")) {
Text("\(owner.value)\(group.value)\(others.value)")
.font(.largeTitle)
.frame(maxWidth: .infinity, alignment: .center)
}
}
.navigationBarTitle("Chmod Helper")
}
}

func toggles(for group: Binding<PermissionGroup>) -> some View {
VStack(alignment: .leading) {
Toggle("Read", isOn: group.read)
Toggle("Write", isOn: group.write)
Toggle("Execute", isOn: group.execute)
}
}
}

PlaygroundPage.current.setLiveView(ContentView())
4 changes: 4 additions & 0 deletions ChmodHelper.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>
3 changes: 3 additions & 0 deletions ChmodHelper.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline version="1.0">
</Timeline>
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ I'd love to see the sites you create using this little tool.

## License
[MIT License](LICENSE)

## Swift Playground
A Swift playground implementation of the permission calculator is provided in `ChmodHelper.playground`. Open this folder with the Swift Playgrounds app on iPad to interactively select permission bits and see the resulting octal value.