def to_string(pkt):
    """Pretty-prints a packet."""
    name = pkt._name
    detail = ''

    if name == 'AppleMIDIExchangePacket':
        detail = '[command={} ssrc={} name={}]'.format(
            pkt.command.decode('utf-8'), pkt.ssrc, pkt.name
        )
    elif name == 'MIDIPacket':
        items = []
        for entry in pkt.command.midi_list:
            command = entry.command
            if command in ('note_on', 'note_off'):
                items.append('{} {} {}'.format(command, entry.params.key, entry.params.velocity))
            elif command == 'control_mode_change':
                items.append(
                    '{} {} {}'.format(command, entry.params.controller, entry.params.value)
                )
            else:
                items.append(command)
        detail = ' '.join(('[{}]'.format(i) for i in items))

    return '{} {}'.format(name, detail)