LoLei / razer-cli

CLI for configuring Razer devices
GNU General Public License v3.0
89 stars 11 forks source link

[patch] Revise --dpi print & --poll print options #44

Closed GM-Script-Writer-62850 closed 3 years ago

GM-Script-Writer-62850 commented 3 years ago

Before:

$ ~/.local/bin/razer-cli --dpi print --poll print
1200
1000
$ ~/.local/bin/razer-cli --poll print --dpi print
1200
1000
$ ~/.local/bin/razer-cli --poll print
1000
$ ~/.local/bin/razer-cli --dpi print
1200

After:

$ ~/.local/bin/razer-cli --dpi print --poll print
dpi: 1200
poll_rate: 1000
$ ~/.local/bin/razer-cli --poll print --dpi print
dpi: 1200
poll_rate: 1000
$ ~/.local/bin/razer-cli --poll print
1000
$ ~/.local/bin/razer-cli --dpi print
1200

Revised Functions:

def set_dpi(device_manager):
    # Iterate over each device and set DPI
    for device in device_manager.devices:
        # If -d argument is set, only set those devices
        if (args.device and device.name in args.device) or (not args.device):
            if (device.type != "mouse"):
                if args.verbose:
                    print("Device {} is not a mouse".format(device.name))
            elif args.dpi == "print":
                dpi = str(device.dpi)[1:-1].split(', ')
                if args.poll == "print":
                    if dpi[0] == dpi[1]:
                        print('dpi:',dpi[0])
                    else:
                        print('dpi:',dpi[0], dpi[1])
                else:
                    if dpi[0] == dpi[1]:
                        print(dpi[0])
                    else:
                        print(dpi[0], dpi[1])
            else:
                if args.verbose:
                    print("Setting DPI of device {} to {}".format(device.name,
                                                                  args.dpi))

                # Save used settings for this device to a file
                util.write_settings_to_file(device, dpi=args.dpi)

                # Actually set DPI
                args.dpi = args.dpi.split(',')
                if len(args.dpi) == 1:
                    args.dpi.append(int(args.dpi[0]))
                device.dpi = (int(args.dpi[0]), int(args.dpi[1]))

def set_poll_rate(device_manager):
    # Iterate over each device and set Polling Rate
    for device in device_manager.devices:
        # If -d argument is set, only set those devices
        if (args.device and device.name in args.device) or (not args.device):
            if device.has("poll_rate"):
                if args.poll == "print":
                    if args.dpi == "print":
                        print('poll_rate:',device.poll_rate)
                    else:
                        print(device.poll_rate)
                else:
                    if args.verbose:
                        print(
                            "Setting polling rate of device {} to {}".format(
                                device.name,
                                args.poll))

                    # Actually set Polling Rate
                    device.poll_rate = int(args.poll)
            else:
                print("Device does not support setting the polling rate")
LoLei commented 3 years ago

I would suggest that you open pull requests yourself if you continue to add changes, it's much more convenient for both of us!

But I appreciate your effort nevertheless.