'Formats a phone number on-the-fly as each digit is entered.
If remember_position is set, remembers the position where next_char is
inserted, so that it can be retrieved later by using
get_remembered_position. The remembered position will be automatically
adjusted if additional formatting characters are later
inserted/removed in front of next_char.
Arguments:
next_char -- The most recently entered digit of a phone
number. Formatting characters are allowed, but as soon as they
are encountered this method formats the number as entered and
not "as you type" anymore. Full width digits and Arabic-indic
digits are allowed, and will be shown as they are.
remember_position -- Whether to track the position where next_char is
inserted.
Returns the partially formatted phone number.'

def input_digit(self, next_char, remember_position=False):

    self._accrued_input += next_char
    if remember_position:
        self._original_position = len(self._accrued_input)
    if (not self._is_digit_or_leading_plus_sign(next_char)):
        self._able_to_format = False
        self._input_has_formatting = True
    else:
        next_char = self._normalize_and_accrue_digits_and_plus_sign(next_char, remember_position)
    if (not self._able_to_format):
        if self._input_has_formatting:
            self._current_output = self._accrued_input
            return self._current_output
        elif self._attempt_to_extract_idd():
            if self._attempt_to_extract_ccc():
                self._current_output = self._attempt_to_choose_pattern_with_prefix_extracted()
                return self._current_output
        elif self._able_to_extract_longer_ndd():
            self._prefix_before_national_number += _SEPARATOR_BEFORE_NATIONAL_NUMBER
            self._current_output = self._attempt_to_choose_pattern_with_prefix_extracted()
            return self._current_output
        self._current_output = self._accrued_input
        return self._current_output
    len_input = len(self._accrued_input_without_formatting)
    if ((len_input >= 0) and (len_input <= 2)):
        self._current_output = self._accrued_input
        return self._current_output
    elif (len_input == 3):
        if self._attempt_to_extract_idd():
            self._is_expecting_country_calling_code = True
        else:
            self._extracted_national_prefix = self._remove_national_prefix_from_national_number()
            self._current_output = self._attempt_to_choose_formatting_pattern()
            return self._current_output
    if self._is_expecting_country_calling_code:
        if self._attempt_to_extract_ccc():
            self._is_expecting_country_calling_code = False
        self._current_output = (self._prefix_before_national_number + self._national_number)
        return self._current_output
    if (len(self._possible_formats) > 0):
        temp_national_number = self._input_digit_helper(next_char)
        formatted_number = self._attempt_to_format_accrued_digits()
        if (len(formatted_number) > 0):
            self._current_output = formatted_number
            return self._current_output
        self._narrow_down_possible_formats(self._national_number)
        if self._maybe_create_new_template():
            self._current_output = self._input_accrued_national_number()
            return self._current_output
        if self._able_to_format:
            self._current_output = self._append_national_number(temp_national_number)
            return self._current_output
        else:
            self._current_output = self._accrued_input
            return self._current_output
    else:
        self._current_output = self._attempt_to_choose_formatting_pattern()
        return self._current_output
