Skip to content

Commit

Permalink
Add set-solid-color command (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
medusalix authored May 4, 2020
1 parent 7f1d4b6 commit 3ce8276
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
20 changes: 20 additions & 0 deletions Sources/WallpaperCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ final class SetCommand: Command {
}
}

final class SetSolidColorCommand: Command {
let name = "set-solid-color"
let shortDescription = "Set solid color wallpaper background"
let color = Parameter()
let screen = Key<String>("--screen", description: "Values: all, main, <index> [Default: all]")

func execute() throws {
guard let color = NSColor(hexString: color.value) else {
print("Invalid `color` value", to: .standardError)
exit(1)
}

try Wallpaper.set(
color,
screen: convertStringToScreen(screen.value)
)
}
}

final class GetCommand: Command {
let name = "get"
let shortDescription = "Get current wallpaper image"
Expand Down Expand Up @@ -105,6 +124,7 @@ let cli = CLI(

cli.commands = [
SetCommand(),
SetSolidColorCommand(),
GetCommand(),
ScreensCommand()
]
Expand Down
7 changes: 7 additions & 0 deletions Sources/wallpaper/Wallpaper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public struct Wallpaper {
}
}

/// Set a solid color as wallpaper.
public static func set(_ solidColor: NSColor, screen: Screen = .all) throws {
let transparentImage = URL(fileURLWithPath: "/System/Library/PreferencePanes/DesktopScreenEffectsPref.prefPane/Contents/Resources/DesktopPictures.prefPane/Contents/Resources/Transparent.tiff")

try set(transparentImage, screen: screen, scale: .fit, fillColor: solidColor)
}

/// Names of available screens.
public static var screenNames: [String] {
NSScreen.screens.map(\.name)
Expand Down
32 changes: 27 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ Usage: wallpaper <command> [options]
Manage the desktop wallpaper
Commands:
set Set wallpaper image
get Get current wallpaper image
screens List screens
help Prints help information
version Prints the current version of this app
set Set wallpaper image
set-solid-color Set solid color wallpaper background
get Get current wallpaper image
screens List screens
help Prints help information
version Prints the current version of this app
```

```
Expand Down Expand Up @@ -67,12 +68,30 @@ Options:
-h, --help Show help information
```

```
$ wallpaper set-solid-color --help
Usage: wallpaper set-solid-color <color> [options]
Set solid color wallpaper background
Options:
--screen <value> Values: all, main, <index> [Default: all]
-h, --help Show help information
```

##### Set

```
$ wallpaper set unicorn.jpg
```

##### Set solid color

```
$ wallpaper set-solid-color 0000ff
```

##### Get

```
Expand All @@ -96,9 +115,12 @@ Swift Package Manager:
import Wallpaper

let imageURL = URL(fileURLWithPath: "<path>")
let solidColor = NSColor.blue

try! Wallpaper.set(imageURL, screen: .main, scale: .fill, fillColor: NSColor.blue)

try! Wallpaper.set(solidColor, screen: .main)

print(try! Wallpaper.get(screen: .main))
```

Expand Down

0 comments on commit 3ce8276

Please sign in to comment.