2017-10-14 16:23:41 +01:00
using System ;
using System.Windows.Forms ;
2017-10-14 18:42:11 +01:00
using ShiftOS.Engine ;
2017-10-15 20:25:37 +01:00
using ShiftOS.Main.Terminal ;
2017-11-21 17:47:37 -06:00
using System.Linq ;
using System.Collections.Generic ;
2017-11-23 11:20:53 -06:00
using System.Drawing.Text ;
2017-10-14 16:23:41 +01:00
namespace ShiftOS.Main.ShiftOS.Apps
{
public partial class Terminal : UserControl
{
2017-10-15 20:25:37 +01:00
public int TerminalID = TerminalBackend . trmTopID + + ; // Used so that we can have multiple instances of the terminal whilst the command begin run knowing what terminal to send the text to - very complicated ;)
2017-10-14 16:23:41 +01:00
public string defaulttextBefore = "user> " ;
2017-11-22 11:53:01 -06:00
public string defaulttextResult = "[user@shiftos ~]$ " ; // NOT YET IMPLEMENTED!!!
2017-10-15 20:25:37 +01:00
public bool DoClear = false ;
public bool RunningCommand = false ;
public bool WaitingResponse = false ;
public string InputReturnText = "" ;
2017-11-22 20:06:58 -06:00
public Stack < string > c = TerminalBackend . commandBuffer ;
2017-11-23 11:20:53 -06:00
private PrivateFontCollection fontCollection = new PrivateFontCollection ( ) ;
2017-10-14 16:23:41 +01:00
2017-11-05 18:47:46 -05:00
// The below variables makes the terminal... a terminal!
string OldText = "" ;
2017-10-14 16:23:41 +01:00
2017-11-05 18:47:46 -05:00
int TrackingPosition ;
2017-10-14 16:23:41 +01:00
2017-11-05 18:47:46 -05:00
public Terminal ( )
{
InitializeComponent ( ) ;
2017-10-14 16:23:41 +01:00
2017-11-05 18:47:46 -05:00
termmain . ContextMenuStrip = new ContextMenuStrip ( ) ; // Disables the right click of a richtextbox!
2017-10-14 16:23:41 +01:00
2017-11-18 16:46:45 +00:00
TerminalBackend . trm . Add ( this ) ;
2017-11-23 11:20:53 -06:00
fontCollection . AddFontFile ( SaveSystem . fontDir + "\\termFont.ttf" ) ;
termmain . Font = new System . Drawing . Font ( fontCollection . Families [ 0 ] , 12F , System . Drawing . FontStyle . Regular ) ;
2017-11-18 16:46:45 +00:00
}
void Print ( )
{
termmain . AppendText ( $"\n {defaulttextResult}" ) ;
TrackingPosition = termmain . Text . Length ;
}
void Print ( string text )
2017-11-05 18:47:46 -05:00
{
2017-11-18 16:46:45 +00:00
termmain . AppendText ( $"\n {text} \n {defaulttextResult}" ) ;
2017-11-05 18:47:46 -05:00
TrackingPosition = termmain . Text . Length ;
2017-10-14 16:23:41 +01:00
}
2017-11-18 16:46:45 +00:00
//if (e.Control && e.KeyCode == Keys.V)
// {
// //if (Clipboard.ContainsText())
// // termmain.Paste(DataFormats.GetFormat(DataFormats.Text));
// e.Handled = true;
// } else if (e.KeyCode == Keys.Enter) {
// RunningCommand = true;
// TerminalBackend.RunCommand(termmain.Text.Substring(TrackingPosition, termmain.Text.Length - TrackingPosition), TerminalID); // The most horrific line in the entire application!
// RunningCommand = false;
// termmain.AppendText($"\n {defaulttextResult}");
// TrackingPosition = termmain.Text.Length;
// e.Handled = true;
// }
//}
2017-10-14 16:23:41 +01:00
private void termmain_TextChanged ( object sender , EventArgs e )
{
2017-10-15 20:25:37 +01:00
if ( ! RunningCommand )
2017-10-14 16:23:41 +01:00
{
2017-10-15 20:25:37 +01:00
if ( termmain . SelectionStart < TrackingPosition )
2017-10-14 16:23:41 +01:00
{
2017-10-15 20:25:37 +01:00
if ( ! DoClear ) // If it's not clearing the terminal
{
termmain . Text = OldText ;
termmain . Select ( termmain . Text . Length , 0 ) ;
}
}
else
{
OldText = termmain . Text ;
2017-10-14 16:23:41 +01:00
}
}
}
private void termmain_SelectionChanged ( object sender , EventArgs e )
{
2017-10-15 20:25:37 +01:00
if ( ! RunningCommand )
2017-10-14 16:23:41 +01:00
{
2017-10-15 20:25:37 +01:00
if ( termmain . SelectionStart < TrackingPosition )
{
termmain . Text = OldText ;
termmain . Select ( termmain . Text . Length , 0 ) ;
}
2017-10-14 16:23:41 +01:00
}
}
private void Terminal_Load ( object sender , EventArgs e )
{
2017-10-15 20:25:37 +01:00
termmain . Text = $"\n {defaulttextResult}" ;
TrackingPosition = termmain . Text . Length ;
2017-11-18 16:48:19 +00:00
termmain . Select ( termmain . TextLength , 1 ) ;
2017-10-14 16:23:41 +01:00
}
2017-10-15 20:25:37 +01:00
public void Input ( string request )
2017-10-14 16:23:41 +01:00
{
2017-10-15 20:25:37 +01:00
InputReturnText = "" ;
RunningCommand = false ;
2017-10-14 16:23:41 +01:00
2017-10-15 20:25:37 +01:00
termmain . AppendText ( $"\n {request} " ) ;
TrackingPosition = termmain . Text . Length ;
}
public void Clear ( )
{
DoClear = true ;
2017-11-23 18:28:51 +00:00
OldText = "" ;
termmain . Text = "" ;
2017-10-15 20:25:37 +01:00
TrackingPosition = termmain . Text . Length ;
DoClear = false ;
2017-10-14 16:23:41 +01:00
}
2017-11-18 16:46:45 +00:00
2017-11-05 18:47:46 -05:00
void termmain_KeyDown ( object sender , KeyEventArgs e )
{
// The below code disables the ability to paste anything other then text...
2017-10-14 16:23:41 +01:00
2017-11-05 18:47:46 -05:00
if ( e . Control & & e . KeyCode = = Keys . V )
{
//if (Clipboard.ContainsText())
// termmain.Paste(DataFormats.GetFormat(DataFormats.Text));
e . Handled = true ;
}
else if ( e . KeyCode = = Keys . Enter )
{
2017-11-18 16:46:45 +00:00
TerminalBackend . RunCommand ( termmain . Text . Substring ( TrackingPosition , termmain . Text . Length - TrackingPosition ) , TerminalID ) ; // The most horrific line in the entire application!
Print ( ) ;
2017-11-05 18:47:46 -05:00
e . Handled = true ;
}
2017-11-21 17:47:37 -06:00
else if ( e . KeyCode = = Keys . Up )
{
if ( c . Count = = 0 ) return ;
2017-11-22 20:06:58 -06:00
termmain . AppendText ( c . Pop ( ) ) ;
2017-11-21 17:47:37 -06:00
}
}
2017-11-05 18:47:46 -05:00
}
2017-10-14 16:23:41 +01:00
}