code: def apply_backspaces_and_linefeeds(text):
      
      orig_lines = text.split('\n')
      orig_lines_len = len(orig_lines)
      new_lines = []
      for orig_line_idx, orig_line in enumerate(orig_lines):
          chars, cursor = [], 0
          orig_line_len = len(orig_line)
          for orig_char_idx, orig_char in enumerate(orig_line):
              if orig_char == '\r' and (orig_char_idx != orig_line_len - 1 or
                                        orig_line_idx != orig_lines_len - 1):
                  cursor = 0
              elif orig_char == '\b':
                  cursor = max(0, cursor - 1)
              else:
                  if (orig_char == '\r' and
                          orig_char_idx == orig_line_len - 1 and
                          orig_line_idx == orig_lines_len - 1):
                      cursor = len(chars)
                  if cursor == len(chars):
                      chars.append(orig_char)
                  else:
                      chars[cursor] = orig_char
                  cursor += 1
          new_lines.append(''.join(chars))
      return '\n'.join(new_lines)
docs: Interpret backspaces and linefeeds in text like a terminal would.
  
      Interpret text like a terminal by removing backspace and linefeed
      characters and applying them line by line.
  
      If final line ends with a carriage it keeps it to be concatenable with next
      output chunk.