Skip to content

Commit

Permalink
Make package pure swift
Browse files Browse the repository at this point in the history
  • Loading branch information
chishui committed Oct 9, 2021
1 parent 9f5d6e9 commit c5ac048
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 921 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patreon: chishui
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
prefix ?= /usr/local
bindir = $(prefix)/bin
libdir = $(prefix)/lib

build:
swift build -c release --disable-sandbox

install: build
install -d "$(bindir)" "$(libdir)"
install ".build/release/one-wallpaper" "$(bindir)"
install_name_tool -change \
".build/x86_64-apple-macosx10.10/release/libSwiftSyntax.dylib" \
"$(bindir)/one-wallpaper"

uninstall:
rm -rf "$(bindir)/one-wallpaper"

clean:
rm -rf .build

.PHONY: build install uninstall clean
34 changes: 34 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"object": {
"pins": [
{
"package": "SQLite.swift",
"repositoryURL": "https://github.com/stephencelis/SQLite.swift",
"state": {
"branch": null,
"revision": "9af51e2edf491c0ea632e369a6566e09b65aa333",
"version": "0.13.0"
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "d2930e8fcf9c33162b9fcc1d522bc975e2d4179b",
"version": "1.0.1"
}
},
{
"package": "Wallpaper",
"repositoryURL": "https://github.com/chishui/Wallpaper",
"state": {
"branch": "main",
"revision": "ab18d7162cdfedf77f0eb19b22467cb5c91fdbfe",
"version": null
}
}
]
},
"version": 1
}
33 changes: 33 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "OneWallpaper",
platforms: [
.macOS(.v10_14)
],
products: [
.library(
name: "OneWallpaper",
targets: ["OneWallpaper"]),
.executable(name: "one-wallpaper",
targets: ["OneWallpaper"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
.package(url: "https://github.com/stephencelis/SQLite.swift", from: "0.13.0"),
.package(url: "https://github.com/chishui/Wallpaper", .branch("main"))
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "OneWallpaper",
dependencies: [
.product(name: "Wallpaper", package: "Wallpaper"),
.product(name: "ArgumentParser", package: "swift-argument-parser")
]
)
]
)
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
OneWallpaper
==============================
OneWallpaper can help you set up a continous wallpaper across your multiple monitors with single wallpaper with smooth transition.

# Install
```bash
$ brew tap chishui/OneWallpaper
$ brew install one-wallpaper
```
28 changes: 28 additions & 0 deletions Sources/OneWallpaper/PathHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// PathHelper.swift
// OneWallpaper
//
// Created by Liyun Xiu on 10/17/20.
//

import Foundation

class PathHelper {
let fileManager = FileManager.default
let extensions = ["jpg", "jpeg", "png"]

func getRandomImage(from folder: String) throws -> String {
let folderUrl = URL(string: folder)
var images: [URL] = []
let items = try fileManager.contentsOfDirectory(atPath: folder)
for item in items {
guard let file = folderUrl?.appendingPathComponent(item) else {continue}
if extensions.contains(file.pathExtension) {
images.append(file)
}
}

let index = Int.random(in: 0..<images.count)
return images[index].absoluteString
}
}
67 changes: 67 additions & 0 deletions Sources/OneWallpaper/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// main.swift
// OneWallpaper
//
// Created by Liyun Xiu on 10/4/20.
//

import Foundation
import Wallpaper
import ArgumentParser

struct OneWallpaper: ParsableCommand {
// Customize your command's help and subcommands by implementing the
// `configuration` property.
static var configuration = CommandConfiguration(
// Optional abstracts and discussions are used for help output.
abstract: "A tool to split your wallpaper and setup them across multiple monitors",

// Commands can define a version for automatic '--version' support.
version: "1.0.0"
)

@Argument(help: "Set image path or directory path to set random image from directory")
var wallpaper: String

@Flag(help: "true to set the same wallpaper in each monitor, otherwise set wallpaper across monitors to they look like one image")
var duplicate = false

mutating func run() {
let wp = Wallpaper()
var isDir : ObjCBool = false
guard FileManager.default.fileExists(atPath: wallpaper, isDirectory: &isDir) else {
print("File: \(wallpaper) does not exist!")
return
}

if isDir.boolValue {
let ph = PathHelper()
do {
wallpaper = try ph.getRandomImage(from: wallpaper)
} catch {
print(error)
return
}
}

let method = duplicate ? SetWallpaperMethod.duplicate : SetWallpaperMethod.across
let result = wp.setWallpaper(with: wallpaper, by: method)
switch result {
case .success:
print("Done!")
case .failure(let error):
switch error {
case .fileNotExist:
print("File: \(wallpaper) does not exist!")
case .saveTemporaryImageFileFail:
print("Cannot save temparory file!")
case .setWallpaperFail:
print("Set wallpaper failed!")
case .unknown:
print("Unknown error happened!")
}
}
}
}

OneWallpaper.main()
Loading

0 comments on commit c5ac048

Please sign in to comment.