# -*- coding: utf-8 -*-
__author__ = 'nickl-'

from aero.__version__ import __version__
from .base import BaseAdapter


class Brew(BaseAdapter):
    """
    Homebrew adapter.
    """
    def search(self, query):
        response = self.command(['search', query])[0]
        if 'No formula found' not in response and 'Error:' not in response:
            return dict([(
                self.package_name(line),
                '\n'.join(map(
                    lambda k: k[0] if len(k) < 2 else k[0] + ': ' + k[1],
                    self.search_info(line)
                ))
            ) for line in response.splitlines() if line])
        return {}

    def search_info(self, query):
        info = self.info(query)
        return filter(
            None,
            [
                info[0],
                info[1] if len(info) > 1 else None,
                info[2] if len(info) > 2 else None,
            ]
        )

    def info(self, query):
        if '/' in query:
            self.command(['tap', '/'.join(query.split('/')[:-1])])
        response = self.command(['info', query])[0]
        if 'Error:' not in response:
            response = response.replace(query + ': ', 'version: ')
            return [line.split(': ', 1) for line in response.splitlines() if 'homebrew' not in line]
        return [['No info available']]

    def install(self, query):
        self.shell(['install', query])
        return {}

@@ -423,156 +423,47 @@
     
-'%5Cn'.join(map(%0A                    lambda k: k%5B0%5D if len(k) %3C 2 else k%5B0%5D + ': ' + k%5B1%5D,%0A                    self.search_info(line)%0A                
+self.search_info(self.package_name(line
 ))%0A 
@@ -584,238 +584,403 @@
     
-info = self.
+response = self._execute_command('aero', %5B'
 info
-(
+', 
 query
-)%0A        return filter(%0A            None,%0A            %5B%0A                info%5B0%5D,%0A                info%5B1%5D if len(info) %3E 1 else None,%0A                info%5B2%5D if len(info) %3E 2 else None,%0A            %5D%0A        
+%5D, False)%5B0%5D%0A        from re import split%0A        lines = response.splitlines()%0A        idx = lines.index('       ________________________________________    __________________________________________________  ')%0A        return '%5Cn'.join(%5B''.join(split('%5Cx1b.*?m', l)).replace(' : ', '').strip() for l in response.splitlines()%5Bidx+1:idx+4%5D%5D
 )%0A%0A 

Use aero info instead for caching info