Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
oschrenk committed Dec 5, 2023
0 parents commit 5d647b8
Show file tree
Hide file tree
Showing 15 changed files with 698 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Swift
on: [push]
jobs:
build:
name: Swift ${{ matrix.swift }} on ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest]
swift: ["5.9"]
runs-on: ${{ matrix.os }}
steps:
- uses: swift-actions/setup-swift@v1.25.0
with:
swift-version: ${{ matrix.swift }}
- uses: actions/checkout@v4
- name: Build
run: swift build
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
1 change: 1 addition & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--indent 2
15 changes: 15 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
included:
- Package.swift
- Sources/Swift
strict: true
opt_in_rules:
- indentation_width
line_length: 100
indentation_width:
indentation_width: 2
identifier_name:
excluded: # excluded via string array
- id
- to
trailing_comma:
mandatory_comma: true
40 changes: 40 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# DOCS

> Night Shift automatically shifts the colours of your display to the warmer end of the colour spectrum after dark. This may help you get a better night's sleep.
Under "System Settings > Display > Night Shift" you find the UI controls.

It supports three schedules

1. Off
2. Sunset to Sunrise
3. Custom

When "Off" (`mode: 0`), you can turn it on for a single day toggling "Turn on until "tomorrow"

When "Sunset to Sunrise" (`mode: 1`), the schedule is automatic, and you can toggle it active until the next "sunrise"

When "Custom" (`mode: 2`), you can set a daily schedule, and additionally toggle it active until "tomorrow"

In all three modes, you can select a colour temperature, on a sliding scale between "Less Warm" and "More Warm"

## Technical

The status object

```
var status: Status = Status()
client.getBlueLightStatus(&status)
```

```
// seems to control the temporary setting until tomorrow/sunrise
enabled: true|false
available: true|false
// I guesss this has to do with location based information
sunSchedulePermitted: true|false
disableFlags: 0
mode: 0|1|2
schedule(fromTime(hour,minute),toTime(hour,minute))
```
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
}
],
"version" : 2
}
29 changes: 29 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "nightshift",
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", exact: "1.2.3"),
],
targets: [
.target(
name: "DisplayClient",
path: "Sources/ObjC/DisplayClient",
linkerSettings: [
.unsafeFlags(["-F/System/Library/PrivateFrameworks"]),
.linkedFramework("CoreBrightness"),
]
),
.executableTarget(
name: "nightshift",
dependencies: [
.byName(name: "DisplayClient"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
path: "Sources/Swift"
),
]
)
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# README

A macOS tool to read and control the [Night Shift](https://support.apple.com/en-us/102191) feature.

## Usage

Print out current settings

```
nightshift
Schedule: sunrise
Enabled: true
Temperature: 0.49
```

Control modes

```
nightshift schedule
nightshift schedule off
nightshift schedule custom --from 11:00 --to 23:00 -t 0.3
nightshift schedule sunrise --temperature 0.3
```

Control details

```
nightshift enable
nightshift disable
nightshift temperature
nightshift temperature 0.33
```

## Installation

**Via Github**

```https://www.youtube.com/watch?v=kWbALI5Ik0A
git clone git@github.com:oschrenk/nightshift.swift.git
cd nightshift.swift
# installs to $HOME/.local/bin/nightshift
task install
```

## Development

- `task build` **Build**
- `task run` **Run**
- `task lint` **Lint**

## Release

```
task release
# produces artifact in ./.build/release/nightshift
```
34 changes: 34 additions & 0 deletions Sources/ObjC/DisplayClient/include/DisplayClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#import <Foundation/Foundation.h>

// Partial header for CBBlueLightClient in private CoreBrightness API
@interface CBBlueLightClient : NSObject

typedef struct {
int hour;
int minute;
} Time;

typedef struct {
Time fromTime;
Time toTime;
} ScheduleObjC;

typedef struct {
BOOL active;
BOOL enabled;
BOOL sunSchedulePermitted;
int mode;
ScheduleObjC schedule;
unsigned long long disableFlags;
BOOL available;
} Status;

- (BOOL)setStrength:(float)strength commit:(BOOL)commit;
- (BOOL)setEnabled:(BOOL)enabled;
- (BOOL)setMode:(int)mode;
- (BOOL)setSchedule:(ScheduleObjC *)schedule;
- (BOOL)getStrength:(float *)strength;
- (BOOL)getBlueLightStatus:(Status *)status;
- (void)setStatusNotificationBlock:(void (^)(void))block;
+ (BOOL)supportsBlueLightReduction;
@end
14 changes: 14 additions & 0 deletions Sources/Swift/AppError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

enum AppError: Error {
case decodingError(String)
}

extension AppError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .decodingError(comment):
return NSLocalizedString("Application Error.", comment: comment)
}
}
}
Loading

0 comments on commit 5d647b8

Please sign in to comment.