Skip to content

Commit

Permalink
add --all flag and add new alias for brightness cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
neroist committed Mar 13, 2024
1 parent b2647f9 commit b0e7277
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 22 deletions.
6 changes: 5 additions & 1 deletion src/commands/brightness.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import puppy

import ../common

proc brightness*(device: int = 0; brightness = -1; output: bool = on): int =
proc brightness*(device = 0, brightness: int = -1; output = on, all: bool = false): int =
## Set device brightness

if not isSetup(output) or not checkDevices(device, output = output): return

if all:
for i in 0..<numDevices-1:
discard brightness(i, brightness, output)

let
apiKey = readFile(keyFile)
Expand Down
8 changes: 6 additions & 2 deletions src/commands/color.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import puppy

import ../common

proc color*(device: int = 0; color: string = ""; output: bool = on): string =
proc color*(device: int = 0; color: string = ""; output = on, all: bool = false): string =
## Set device color with an HTML/hex color code.
## NOTE: when called with no parameters, the device's current color will be #000000 if:
##
Expand All @@ -19,7 +19,11 @@ proc color*(device: int = 0; color: string = ""; output: bool = on): string =
## 3. A scene is playing on the device.

if not isSetup(output) or not checkDevices(device, output = output): return


if all:
for i in 0..<numDevices-1:
discard color(i, color, output)

let
apiKey = readFile(keyFile)
devices = parseJson readFile(devicesFile)
Expand Down
6 changes: 5 additions & 1 deletion src/commands/color_temp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ func kelvinToRgb*(temp: int): tuple[r, g, b: range[0..255]] =
else:
result.b = int (138.5177312231 * ln(temp - 10) - 305.0447927307).clamp(0.0, 255.0)

proc colorTemp*(device: int = 0; output: bool = on; temperature: int = -1): int =
proc colorTemp*(device = 0, temperature: int = -1; output = on, all: bool = false): int =
## Set device color temperature in kelvin

if not isSetup(output) or not checkDevices(device, output = output): return

if all:
for i in 0..<numDevices-1:
discard colorTemp(i, temperature, output)

let
apiKey = readFile(keyFile)
Expand Down
6 changes: 5 additions & 1 deletion src/commands/picker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import tinydialogs
import ../common
import ./color

proc picker*(device: int = 0; setProperty: bool = true; output: bool = on) =
proc picker*(device: int = 0; setProperty = true, output: bool = on, all: bool = false) =
## Pick a color through a GUI (your OS's default color picker dialog)

if all:
for i in 0..<numDevices:
picker(i, setProperty, output)

let
pickedColor = colorChooser(
"Pick a color",
Expand Down
6 changes: 5 additions & 1 deletion src/commands/rgb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import puppy

import ../common

proc rgb*(rgb: seq[int] = @[-1, -1, -1]; device: int = 0; output: bool = on): tuple[r, g, b: int] =
proc rgb*(rgb: seq[int] = @[-1, -1, -1]; device: int = 0; output = on, all: bool = false): tuple[r, g, b: int] =
## Same as command `color` but uses rgb instead of HTML codes, although it doesn't support random colors.
##
## NOTE: when called with no parameters, the device's current color will be rgb(0, 0, 0) if:
Expand All @@ -18,6 +18,10 @@ proc rgb*(rgb: seq[int] = @[-1, -1, -1]; device: int = 0; output: bool = on): tu

if not isSetup(output) or not checkDevices(device, output=output): return

if all:
for i in 0..<numDevices:
discard rgb(rgb, i, output)

var rgb = rgb

if len(rgb) != 3:
Expand Down
6 changes: 5 additions & 1 deletion src/commands/state.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import std/json
import ./color_temp
import ../common

proc state*(device: int = 0) =
proc state*(device: int = 0; all: bool = false) =
## Output state of device

if not isSetup(true) or not checkDevices(device, output=true): return

if all:
for i in 0..<numDevices-1:
state(i)

var
colorJson = %* {"r": 0, "g": 0, "b": 0}
colorTem = 0
Expand Down
10 changes: 7 additions & 3 deletions src/commands/turn.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ func toggle*(str: string): string =
elif str == "off": "on"
else: str

proc turn*(device: int = 0; state: string = ""; toggle: bool = false, output: bool = on): string =
proc turn*(device: int = 0; state: string = ""; toggle = false, output = on, all: bool = false): string =
## Turn device on or off

if not isSetup(output) or (not checkDevices(device, output = output)): return

let apiKey = readFile(keyFile)
if all:
for i in 0..<numDevices-1:
discard turn(i, state, toggle, output)

let
apiKey = readFile(keyFile)

let
resp = parseJson readFile(devicesFile)
(deviceAddr, model) = getDeviceInfo(resp, device)

Expand Down
3 changes: 3 additions & 0 deletions src/common/vars.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const
OutputHelp* = "Whether or not the command will produce output."
## "Output" option help text.

AllHelp* = "If enabled, the command will run on all devices. Ignores `device` option."
## "All" option help text.

NotSetupErrorMsg* = "Nova is not setup properly. Use the command `nova setup` to setup Nova."
## Error message when Nova is not setup properly.

Expand Down
41 changes: 29 additions & 12 deletions src/nova.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ dispatchMulti(
"toggle": "Whether or not to toggle the power state of the device (if its on turn it off and " &
"vice-versa). This flag takes precedence over the `state` option.",
"device": $DeviceHelp,
"output": $OutputHelp
"output": $OutputHelp,
"all": $AllHelp
},
noAutoEcho = true
],
Expand All @@ -56,10 +57,17 @@ dispatchMulti(
"brightness": "The brightness you want to set on the device. Supports values 1-100 only. " &
"If left blank, the command will print the current brightness of the device.",
"device": $DeviceHelp,
"output": $OutputHelp
"output": $OutputHelp,
"all": $AllHelp
},
noAutoEcho = true
],
[
brightness.brightness,
cmdName = "b",
doc = "Alias for 'brighness' command",
noAutoEcho = true
],
[
color.color,
help = {
Expand All @@ -68,7 +76,8 @@ dispatchMulti(
"If left blank, will return the current color of the device. " &
"If `color` is \"rand\" or \"random\" a random color will be displayed on the device",
"device": $DeviceHelp,
"output": $OutputHelp
"output": $OutputHelp,
"all": $AllHelp
},
noAutoEcho = true
],
Expand All @@ -79,19 +88,26 @@ dispatchMulti(
"temperature": "The color temperature you want to set on the device. " &
"Has to be in the valid range your Govee device supports.",
"device": $DeviceHelp,
"output": $OutputHelp
"output": $OutputHelp,
"all": $AllHelp
},
noAutoEcho = true
],
[
state.state,
help = {"device": $DeviceHelp}
help = {
"device": $DeviceHelp,
"all": $AllHelp
}
],
[
state.state,
cmdName = "device",
doc = "Alias for state",
help = {"device": $DeviceHelp}
doc = "Alias for 'state' command",
help = {
"device": $DeviceHelp,
"all": $AllHelp
}
],
[
rgb.rgb,
Expand All @@ -100,17 +116,18 @@ dispatchMulti(
"rgb": "The color you want to set on the device in an RGB format. " &
"Has to be 3 numbers seperated by a space. " &
"If left blank, the command will print the current color in an RGB function.",
"output": $OutputHelp
"output": $OutputHelp,
"all": $AllHelp
},
noAutoEcho = true,
cmdName = "rgb"
noAutoEcho = true
],
[
picker.picker,
help = {
"device": $DeviceHelp,
"output": $OutputHelp,
"set_property": "Whether or not to set `device`'s color to the color chosen."
"set_property": "Whether or not to set `device`'s color to the color chosen.",
"all": $AllHelp
}
],
[
Expand All @@ -129,7 +146,7 @@ dispatchMulti(
license,
help = {
"browser": "Whether or not to open the license in the default " &
"browser, or to just print the license text to the terminal"
"browser, or to just print the license text to the terminal"
}
],
[docs]
Expand Down

0 comments on commit b0e7277

Please sign in to comment.