<commit_before>/*!
 * jqTerminal
 * Copyright 2013, 2014 <NAME>
 * Released under the BSD-2-Clause license
 * http://opensource.org/licenses/BSD-2-Clause
 */
 
var Terminal = function (user, prompt,container) {
		
	// Private variables.
	var _hostAndPrompt = '';
	var _user = "user";
	var _containerId = '';
	var _promptSymbol = '';
	var _eraseLimit;
	var _self;
	var _filesInDirectory = [{name:"hello.txt",mimeType:"text/plain",content:"¡Hola mundo!\nHello world!\nHallo welt!",permissions:"rw-r--r--",type:1,owner:_user,size:1,date:"18 Oct 16:45"}];
	var _consoleCommands = [];
	var _environmentVariables = [];
	var _commandQueue = [];
	var _currentQueuePosition = 0;
	
	// Gets the command that was typed into the console.
	function getCommandInput()
	{
		var lastLine = $(".console").val().split('\n')[$(".console").val().split('\n').length-1];
		var lastLineSplit = lastLine.split(_promptSymbol);
		var command = "";
		
		for(i =1; i < lastLineSplit.length; i ++)
		{
			command = command + " " + (i >= 2? _promptSymbol : "") + lastLineSplit[i]; 
		}
				
		return command;
	}
	
	// Inserts a new line in the console. 
	function _insertNewLine()
	{
		 $(".console").val($(".console").val() + '\n' + _hostAndPrompt);
		  // Set the limit for erasing the conosole to the beginning of the newline.
		 _eraseLimit = $(".console").val().length;
	}

	// Clear the console.
	function _clearConsole()
	{
		$(".console").val("" + _hostAndPrompt);
		_eraseLimit = _hostAndPrompt.length;
	}
	
	// Writes the given text on the console.
	function _write(text)
	{
	  $(".console").val($(".console").val() + text);
	}
	
	// Executes a given command. 
	function executeCommand(command,arguments)
	{
		var commandPos = $.inArray(command, $.map(_consoleCommands,function(item,index){ return item.name;}));
		if( commandPos != -1)
		{
			_consoleCommands[commandPos].run(arguments,_self);
		}
		else
		{
			 $(".console").val($(".console").val() + "\n");
			_write("bash: " + command + ": command not found");
		}
	}
	
	// Handles the click event. 
	function handleClick(event)
	{
		if($(".console")[0].selectionStart  <= _eraseLimit || $(".console")[0].selectionEnd <= _eraseLimit)
		{
			$(".console")[0].selectionStart = _eraseLimit;
			$(".console")[0].selectionEnd = _eraseLimit;
		}
	}
	
	/*
	Handles the keypress events to prevent the user to erase the command prompt.
	Relevant keys/events: 
	
	8  - Backspace.
	13 - Enter.
	36 - Init key.
	37 - Arrow back.
	38 - Arrow up.
	39 - Arrow forward (this case is not being handled).
	40 - Arrow down.
	46 - Supress key. 
	
	*/
	function handleKeydown(event)
	{		

		console.log(event.which);
			
		switch(event.which)
		{							
			case 13:
				 handleEnterKey(event);
				break;
			case 36:
				handleInitKey();
			case 46:
				preventPromptErasing(true);
			case 8:	
			case 37:								
				preventPromptErasing(false);
				break;
			case 38:	
				event.preventDefault();
				getCommandFromQueue(true);
				break;
			case 40:
				getCommandFromQueue(false);
				event.preventDefault();
				break;
			default:
				// As of now, nothing is done in this case.
				break;
		}
	}
	
	// Handles the init key.								
	function handleInitKey()		
	{
	  event.preventDefault();
	  $(".console")[0].selectionStart = _eraseLimit;
	  $(".console")[0].selectionEnd = _eraseLimit;		  
	}
	
	// Handles the enter key.    
	function handleEnterKey(event)
	{
	   event.preventDefault(); // Do not insert a newline. Instead, let us handle the event.		  
	   var commandQuery = getCommandInput().trim();
	   var splitCommand;
	   
	   _commandQueue.push(commandQuery);
	   
	   if(commandQuery != "")
	   {
		  splitCommand = commandQuery.split(" ");
		  executeCommand(splitCommand[0],splitCommand);
	   } 
	   else 
	   {
		  splitCommand = [commandQuery];
	   }
	  
	  if(splitCommand[0].indexOf("clear") == -1)
	  {
		 _insertNewLine();
	  }
	  
	  return false;
	}

	// Prevents the user from deleting the command prompt.
	function preventPromptErasing(deleteKeyPressed)
	{				  
		if($(".console")[0].selectionStart  < _eraseLimit || $(".console")[0].selectionEnd < _eraseLimit || (!deleteKeyPressed && $(".console")[0].selectionStart  == _eraseLimit || $(".console")[0].selectionEnd == _eraseLimit))
		{
			event.preventDefault();
			$(".console")[0].selectionStart = _eraseLimit;
			$(".console")[0].selectionEnd = _eraseLimit;
		}
	}
	
	// Replaces whatever is on the console at the time of being called with the previous or next command.
	function getCommandFromQueue(arrowUp)
	{	
		// Queue commands 
		console.log(_commandQueue);
	
		if(_commandQueue.length == 0)
		{
			return;
		}
		
		if(_currentQueuePosition == 0 && arrowUp)
		{
		}
	}
	
	// Initialize the console and all of its relevant variables.
	function doInitialize()
	{
		// Initialize variables
		_promptSymbol = prompt;
		_hostAndPrompt = user + prompt;		
		_containerId = container;
		_self = this;
		var con = $("<textarea class=\"console\" spellcheck=\"false\">");
		con.val(_hostAndPrompt);
		_eraseLimit = _hostAndPrompt.length;
		con.keydown(function( event ) { handleKeydown(event);});
		
		// Initialize the conosle and place it inside its container.<commit_msg>Changes to the command queue feature.<commit_after>/*!
 * jqTerminal
 * Copyright 2013, 2014 <NAME>
 * Released under the BSD-2-Clause license
 * http://opensource.org/licenses/BSD-2-Clause
 */
 
var Terminal = function (user, prompt,container) {
		
	// Private variables.
	var _hostAndPrompt = '';
	var _user = "user";
	var _containerId = '';
	var _promptSymbol = '';
	var _eraseLimit;
	var _self;
	var _filesInDirectory = [{name:"hello.txt",mimeType:"text/plain",content:"¡Hola mundo!\nHello world!\nHallo welt!",permissions:"rw-r--r--",type:1,owner:_user,size:1,date:"18 Oct 16:45"}];
	var _consoleCommands = [];
	var _environmentVariables = [];
	var _commandQueue = [];
	var _currentCommandQueuePosition = -1;
	
	// Gets the command that was typed into the console.
	function getCommandInput()
	{
		var lastLine = $(".console").val().split('\n')[$(".console").val().split('\n').length-1];
		var lastLineSplit = lastLine.split(_promptSymbol);
		var command = "";
		
		for(i =1; i < lastLineSplit.length; i ++)
		{
			command = command + " " + (i >= 2? _promptSymbol : "") + lastLineSplit[i]; 
		}
				
		return command;
	}
	
	// Replaces the current command of the console with the specified string. 
	function setCommandInput(command)
	{
	   var newValue = $(".console").val().slice(0, _eraseLimit) + command;
	   $(".console").val(newValue);
	}
	
	// Inserts a new line in the console. 
	function _insertNewLine()
	{
		 $(".console").val($(".console").val() + '\n' + _hostAndPrompt);
		  // Set the limit for erasing the conosole to the beginning of the newline.
		 _eraseLimit = $(".console").val().length;
	}

	// Clear the console.
	function _clearConsole()
	{
		$(".console").val("" + _hostAndPrompt);
		_eraseLimit = _hostAndPrompt.length;
	}
	
	// Writes the given text on the console.
	function _write(text)
	{
	  $(".console").val($(".console").val() + text);
	}
	
	// Executes a given command. 
	function executeCommand(command,arguments)
	{
		var commandPos = $.inArray(command, $.map(_consoleCommands,function(item,index){ return item.name;}));
		if( commandPos != -1)
		{
			_consoleCommands[commandPos].run(arguments,_self);
		}
		else
		{
			 $(".console").val($(".console").val() + "\n");
			_write("bash: " + command + ": command not found");
		}
	}
	
	// Handles the click event. 
	function handleClick(event)
	{
		if($(".console")[0].selectionStart  <= _eraseLimit || $(".console")[0].selectionEnd <= _eraseLimit)
		{
			$(".console")[0].selectionStart = _eraseLimit;
			$(".console")[0].selectionEnd = _eraseLimit;
		}
	}
	
	/*
	Handles the keypress events to prevent the user to erase the command prompt.
	Relevant keys/events: 
	
	8  - Backspace.
	13 - Enter.
	36 - Init key.
	37 - Arrow back.
	38 - Arrow up.
	39 - Arrow forward (this case is not being handled).
	40 - Arrow down.
	46 - Supress key. 
	
	*/
	function handleKeydown(event)
	{		

		console.log(event.which);
			
		switch(event.which)
		{							
			case 13:
				 handleEnterKey(event);
				break;
			case 36:
				handleInitKey();
			case 46:
				preventPromptErasing(true);
			case 8:	
			case 37:								
				preventPromptErasing(false);
				break;
			case 38:	
				event.preventDefault();
				getCommandFromQueue(true);
				break;
			case 40:
				getCommandFromQueue(false);
				event.preventDefault();
				break;
			default:
				// As of now, nothing is done in this case.
				break;
		}
	}
	
	// Handles the init key.								
	function handleInitKey()		
	{
	  event.preventDefault();
	  $(".console")[0].selectionStart = _eraseLimit;
	  $(".console")[0].selectionEnd = _eraseLimit;		  
	}
	
	// Handles the enter key.    
	function handleEnterKey(event)
	{
	   event.preventDefault(); // Do not insert a newline. Instead, let us handle the event.		  
	   var commandQuery = getCommandInput().trim();
	   var splitCommand;
	   
	   _commandQueue.push(commandQuery);
	   _currentCommandQueuePosition += 1;
	   
	   if(commandQuery != "")
	   {
		  splitCommand = commandQuery.split(" ");
		  executeCommand(splitCommand[0],splitCommand);
	   } 
	   else 
	   {
		  splitCommand = [commandQuery];
	   }
	  
	  if(splitCommand[0].indexOf("clear") == -1)
	  {
		 _insertNewLine();
	  }
	  
	  return false;
	}

	// Prevents the user from deleting the command prompt.
	function preventPromptErasing(deleteKeyPressed)
	{				  
		if($(".console")[0].selectionStart  < _eraseLimit || $(".console")[0].selectionEnd < _eraseLimit || (!deleteKeyPressed && $(".console")[0].selectionStart  == _eraseLimit || $(".console")[0].selectionEnd == _eraseLimit))
		{
			event.preventDefault();
			$(".console")[0].selectionStart = _eraseLimit;
			$(".console")[0].selectionEnd = _eraseLimit;
		}
	}
	
	// Replaces whatever is on the console at the time of being called with the previous or next command.
	/*
		TODO's (in order of priority):
		
		- Make it work.
		- Change this method's name (what kind of name is this?)
		- Refactor and make the code less horrible. 
	*/
	function getCommandFromQueue(arrowUp)
	{			
		var queueLength = _commandQueue.length;
	
		// If there are no commands in the queue we don't do anything. 
		if(queueLength == 0)
		{
			return;
		}
				
		if(arrowUp)
		{
			_currentCommandQueuePosition = (_currentCommandQueuePosition == 0 ? 0 : (_currentCommandQueuePosition == queueLength - 1 ? _currentCommandQueuePosition : (_currentCommandQueuePosition - 1)));
		}
		else
		{
			_currentCommandQueuePosition = (_currentCommandQueuePosition == queueLength - 1 ? queueLength - 1 : _currentCommandQueuePosition + 1);
		}	
		
		console.log(_currentCommandQueuePosition);
		setCommandInput(_commandQueue[_currentCommandQueuePosition]);
	}
	
	// Initialize the console and all of its relevant variables.
	function doInitialize()
	{
		// Initialize variables
		_promptSymbol = prompt;
		_hostAndPrompt = user + prompt;		
		_containerId = container;
		_self = this;
		var con = $("<textarea class=\"console\" spellcheck=\"false\">");
		con.val(_hostAndPrompt);
		_eraseLimit = _hostAndPrompt.length;
		con.keydown(function( event ) { handleKeydown(event);});
		
		// Initialize the conosle and place it inside its container.
{'fraction_non_alphanumeric': 0.14737681673165545, 'fraction_numerical': 0.012761432116270826, 'mean_word_length': 3.2811077389984824, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '458882', 'n_tokens_mistral': 4200, 'n_tokens_neox': 3633, 'n_words': 865}