Refactor logic for building ansi escape sequences.