'Loop, reading from <chan>.<attr>(), writing to <stream> and buffering to <capture>.
Will raise `~fabric.exceptions.CommandTimeout` if network timeouts
continue to be seen past the defined ``self.timeout`` threshold.
(Timeouts before then are considered part of normal short-timeout fast
network reading; see Fabric issue #733 for background.)'

def loop(self):

    initial_prefix_printed = False
    seen_cr = False
    line = []
    if (not env.output_prefix):
        self.prefix = ''
    start = time.time()
    while True:
        try:
            bytelist = self.read_func(self.read_size)
        except socket.timeout:
            elapsed = (time.time() - start)
            if ((self.timeout is not None) and (elapsed > self.timeout)):
                raise CommandTimeout(timeout=self.timeout)
            continue
        if (bytelist == ''):
            if (self.linewise and line):
                self._flush(self.prefix)
                self._flush(''.join(line))
            break
        if (self.capture is None):
            self._flush(bytelist)
        else:
            if self.printing:
                printable_bytes = bytelist
                if (printable_bytes[(-1)] == '\r'):
                    seen_cr = True
                if ((printable_bytes[0] == '\n') and seen_cr):
                    printable_bytes = printable_bytes[1:]
                    seen_cr = False
                while (_has_newline(printable_bytes) and (printable_bytes != '')):
                    cr = re.search('(\r\n|\r|\n)', printable_bytes)
                    if (cr is None):
                        break
                    end_of_line = printable_bytes[:cr.start(0)]
                    printable_bytes = printable_bytes[cr.end(0):]
                    if (not initial_prefix_printed):
                        self._flush(self.prefix)
                    if _has_newline(end_of_line):
                        end_of_line = ''
                    if self.linewise:
                        self._flush(((''.join(line) + end_of_line) + '\n'))
                        line = []
                    else:
                        self._flush((end_of_line + '\n'))
                    initial_prefix_printed = False
                if self.linewise:
                    line += [printable_bytes]
                else:
                    if (not initial_prefix_printed):
                        self._flush(self.prefix)
                        initial_prefix_printed = True
                    self._flush(printable_bytes)
            read_lines = re.split('(\\r|\\n|\\r\\n)', bytelist)
            for fragment in read_lines:
                self.capture += fragment
                (expected, response) = self._get_prompt_response()
                if expected:
                    del self.capture[((-1) * len(expected)):]
                    self.chan.sendall((str(response) + '\n'))
                else:
                    prompt = _endswith(self.capture, env.sudo_prompt)
                    try_again = (_endswith(self.capture, (env.again_prompt + '\n')) or _endswith(self.capture, (env.again_prompt + '\r\n')))
                    if prompt:
                        self.prompt()
                    elif try_again:
                        self.try_again()
    if (self.prefix and (''.join(self.write_buffer) == self.prefix)):
        self._flush('\n')
