From c6dd8e88d3ff60b9292e20d33cae499aa5a21046 Mon Sep 17 00:00:00 2001 From: deadbone Date: Mon, 16 Jun 2025 19:05:56 +0200 Subject: [PATCH] Add Swift playground for chmod calculator --- ChmodHelper.playground/Contents.swift | 65 ++++++++++++++++++++ ChmodHelper.playground/contents.xcplayground | 4 ++ ChmodHelper.playground/timeline.xctimeline | 3 + README.md | 4 ++ 4 files changed, 76 insertions(+) create mode 100644 ChmodHelper.playground/Contents.swift create mode 100644 ChmodHelper.playground/contents.xcplayground create mode 100644 ChmodHelper.playground/timeline.xctimeline diff --git a/ChmodHelper.playground/Contents.swift b/ChmodHelper.playground/Contents.swift new file mode 100644 index 0000000..6ec8a7a --- /dev/null +++ b/ChmodHelper.playground/Contents.swift @@ -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 { + Binding( + get: { self.wrappedValue.read }, + set: { self.wrappedValue.read = $0 } + ) + } + var write: Binding { + Binding( + get: { self.wrappedValue.write }, + set: { self.wrappedValue.write = $0 } + ) + } + var execute: Binding { + Binding( + 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) -> some View { + VStack(alignment: .leading) { + Toggle("Read", isOn: group.read) + Toggle("Write", isOn: group.write) + Toggle("Execute", isOn: group.execute) + } + } +} + +PlaygroundPage.current.setLiveView(ContentView()) diff --git a/ChmodHelper.playground/contents.xcplayground b/ChmodHelper.playground/contents.xcplayground new file mode 100644 index 0000000..fc5b4ab --- /dev/null +++ b/ChmodHelper.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + diff --git a/ChmodHelper.playground/timeline.xctimeline b/ChmodHelper.playground/timeline.xctimeline new file mode 100644 index 0000000..cd91565 --- /dev/null +++ b/ChmodHelper.playground/timeline.xctimeline @@ -0,0 +1,3 @@ + + + diff --git a/README.md b/README.md index 3b11e9e..7c8d34e 100644 --- a/README.md +++ b/README.md @@ -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. +