Ejemplo n.º 1
0
def progressclear():
    ANSI.clear(1)
Ejemplo n.º 2
0
def find(prefs=None):
    # Setup
    # ===================================
    pbuddy = '/usr/libexec/PlistBuddy'
    if prefs is None:
        prefs = '/Library/Preferences/com.apple.windowserver.plist'

    if not os.path.isfile(pbuddy):
        print(ANSI.color('Error: could not locate PlistBuddy', 'red'))
        exit(1)
    if not os.path.isfile(prefs):
        print(ANSI.color('Error: could not locate display preferences file', 'red'))
        exit(1)

    def test(command):
        try:
            subprocess.check_call(
                command,
                stdout=DEVNULL,
                stderr=DEVNULL,
                shell=True
            )
        except subprocess.CalledProcessError:
            return False

        return True
    # ===================================

    total_arrangements = 0
    pbuddy_print = lambda n: 'print :DisplayAnyUserSets:%d:' % n
    # Determine number of arrangements
    while test('"%s" -c "%s" "%s"' % (pbuddy, pbuddy_print(total_arrangements), prefs)):
        total_arrangements += 1

    print('Find your display setup:')
    found = False
    for arrangement_index in range(total_arrangements):
        arrangement = []

        print_left = pbuddy_print(arrangement_index)
        display = 0
        while test('"%s" -c "%s%d" "%s"' % (pbuddy, print_left, display, prefs)):
            display_attr = lambda attr: int(
                subprocess.check_output(
                    '"%s" -c "%s%d:%s" "%s"' % (pbuddy, print_left, display, attr, prefs),
                    universal_newlines=True,
                    shell=True
                ).rstrip()
            )

            mirrored = display_attr('Mirrored') == 1
            prefix = '' if mirrored else 'Unmirrored'

            width   = display_attr(prefix + 'Width')
            height  = display_attr(prefix + 'Height')
            originX = display_attr(prefix + 'OriginX')
            originY = display_attr(prefix + 'OriginY')

            arrangement.append(Display(width, height, originX, originY, mirrored))

            display += 1

        # Normalize before printing or saving
        arrangement = normalize(arrangement)

        print()
        num_lines = print_arrangement(arrangement)
        print()
        num_lines += 2 # Two print() 's

        prog = '(%d/%d)' % (arrangement_index + 1, total_arrangements)
        prog = ANSI.color(prog, 'cyan')
        read = input('%s Is this your arrangement? [y/N] ' % prog)
        num_lines += 2 # One for printing the string and one for when you hit enter
        if read and read in 'yY':
            found = True
            fname = DATA_FILE
            try:
                with open(fname, 'wb') as file:
                    pickle.dump(arrangement, file, protocol=2)
                    print(ANSI.color('Successfully recorded!', 'green'))
            except Exception:
                print(ANSI.color('Error storing arrangement data in "%s"' % fname, 'red'))
            break
        else:
            ANSI.clear(num_lines)

    if not found:
        print(ANSI.color('No arrangement chosen. Nothing recorded.', 'red'))

    return found