






import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


__UpperCamelCase   :   List[Any]      =    False
try:
						__UpperCamelCase   :   int      =    _is_package_available("""google.colab""")
except ModuleNotFoundError:
						pass


@input.register
class 							__UpperCamelCase     :







	def __init__(	self   :				Dict							,					_lowerCAmelCase   :				str = None							,					_lowerCAmelCase   :				list = []      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	0
		__lowercase   							=	choices
		__lowercase   							=	prompt
		if sys.platform == "win32":
			__lowercase   							=	"""*"""
		else:
			__lowercase   							=	"""➔ """







	def 			_a (	self   :				List[str]							,					_lowerCAmelCase   :				Dict							,					_lowerCAmelCase   :				str = ""      )		->			str:
		"""simple docstring"""







		if sys.platform != "win32":
			writeColor(self.choices[index]							,					32							,					_lowerCAmelCase      )
		else:
			forceWrite(self.choices[index]							,					_lowerCAmelCase      )







	def 			_a (	self   :				Dict							,					_lowerCAmelCase   :				int      )		->			Any:
		"""simple docstring"""







		if index == self.position:
			forceWrite(F' {self.arrow_char} '      )
			self.write_choice(_lowerCAmelCase      )
		else:
			forceWrite(F'    {self.choices[index]}'      )
		reset_cursor()







	def 			_a (	self   :				Optional[Any]							,					_lowerCAmelCase   :				Direction							,					_lowerCAmelCase   :				int = 1      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	self.position
		if direction == Direction.DOWN:
			if self.position + 1 >= len(self.choices      ):
				return
			self.position += num_spaces
		else:
			if self.position - 1 < 0:
				return
			self.position -= num_spaces
		clear_line()
		self.print_choice(_lowerCAmelCase      )
		move_cursor(_lowerCAmelCase							,					direction.name      )
		self.print_choice(self.position      )







	@input.mark(KEYMAP["""up"""]      )
	def 			_a (	self   :				Any      )		->			Dict:
		"""simple docstring"""







		self.move_direction(Direction.UP      )







	@input.mark(KEYMAP["""down"""]      )
	def 			_a (	self   :				Optional[Any]      )		->			Optional[Any]:
		"""simple docstring"""







		self.move_direction(Direction.DOWN      )







	@input.mark(KEYMAP["""newline"""]      )
	def 			_a (	self   :				Tuple      )		->			Dict:
		"""simple docstring"""







		move_cursor(len(self.choices      ) - self.position							,					"""DOWN"""      )
		return self.position







	@input.mark(KEYMAP["""interrupt"""]      )
	def 			_a (	self   :				Optional[Any]      )		->			Union[str, Any]:
		"""simple docstring"""







		move_cursor(len(self.choices      ) - self.position							,					"""DOWN"""      )
		raise KeyboardInterrupt







	@input.mark_multiple(*[KEYMAP[str(_lowerCAmelCase      )] for number in range(10      )]      )
	def 			_a (	self   :				Union[str, Any]      )		->			int:
		"""simple docstring"""







		__lowercase   							=	int(chr(self.current_selection      )      )
		__lowercase   							=	index - self.position
		if index == self.position:
			return
		if index < len(self.choices      ):
			if self.position > index:
				self.move_direction(Direction.UP							,					-movement      )
			elif self.position < index:
				self.move_direction(Direction.DOWN							,					_lowerCAmelCase      )
			else:
				return
		else:
			return







	def 			_a (	self   :				Optional[int]							,					_lowerCAmelCase   :				int = 0      )		->			Union[str, Any]:
		"""simple docstring"""







		if self.prompt:
			linebreak()
			forceWrite(self.prompt							,					"""\n"""      )
			if in_colab:
				forceWrite("""Please input a choice index (starting from 0), and press enter"""							,					"""\n"""      )
			else:
				forceWrite("""Please select a choice using the arrow or number keys, and selecting with enter"""							,					"""\n"""      )
		__lowercase   							=	default_choice
		for i in range(len(self.choices      )      ):
			self.print_choice(_lowerCAmelCase      )
			forceWrite("""\n"""      )
		move_cursor(len(self.choices      ) - self.position							,					"""UP"""      )
		with cursor.hide():
			while True:
				if in_colab:
					try:
						__lowercase   							=	int(builtins.input()      )
					except ValueError:
						__lowercase   							=	default_choice
				else:
					__lowercase   							=	self.handle_input()
				if choice is not None:
					reset_cursor()
					for _ in range(len(self.choices      ) + 1      ):
						move_cursor(1							,					"""UP"""      )
						clear_line()
					self.write_choice(_lowerCAmelCase							,					"""\n"""      )
					return choice








"""simple docstring"""






import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin



class 					__UpperCAmelCase							(						unittest.TestCase						,  _UpperCamelCase  ):
				def       UpperCAmelCase      (		self    :			Dict    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Any		 =			load_tool("text-classification"    )
							self.tool.setup()
							a__  :	List[Any]		 =			load_tool("text-classification"	, remote=a_    )
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	Optional[Any]:



							'''simple docstring'''



							a__  :	str		 =			self.tool("That's quite cool"	, ["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	List[str]		 =			self.remote_tool("That's quite cool"	, ["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	Any:



							'''simple docstring'''



							a__  :	List[str]		 =			self.tool(text="That's quite cool"	, labels=["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )






				def       UpperCAmelCase      (		self    :			List[Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Tuple		 =			self.remote_tool(text="That's quite cool"	, labels=["positive", "negative"]    )
							self.assertEqual(a_	, "positive"    )