Files
Terminal.Gui/docfx/docs/getting-started.md
2023-12-03 12:16:46 -07:00

111 lines
3.0 KiB
Markdown

# Getting Started
Paste these commands into your favorite terminal on Windows, Mac, or Linux. This will install the [Terminal.Gui.Templates](https://github.com/gui-cs/Terminal.Gui.templates), create a new "Hello World" TUI app, and run it.
(Press `CTRL-Q` to exit the app)
```ps1
dotnet new --install Terminal.Gui.templates
dotnet new tui -n myproj
cd myproj
dotnet run
```
## Adding Terminal.Gui to a Project
To install Terminal.Gui from [Nuget](https://www.nuget.org/packages/Terminal.Gui) into a .NET Core project, use the `dotnet` CLI tool with this command.
```ps1
dotnet add package Terminal.Gui
```
## Using the Templates
Use the [Terminal.Gui.Templates](https://github.com/gui-cs/Terminal.Gui.templates).
## Sample Usage in C#
The following example shows a basic Terminal.Gui application in C#:
```csharp
// A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
using Terminal.Gui;
Application.Run<ExampleWindow> ();
System.Console.WriteLine ($"Username: {((ExampleWindow)Application.Top).usernameText.Text}");
// Before the application exits, reset Terminal.Gui for clean shutdown
Application.Shutdown ();
// Defines a top-level window with border and title
public class ExampleWindow : Window {
public TextField usernameText;
public ExampleWindow ()
{
Title = "Example App (Ctrl+Q to quit)";
// Create input components and labels
var usernameLabel = new Label () {
Text = "Username:"
};
usernameText = new TextField ("") {
// Position text field adjacent to the label
X = Pos.Right (usernameLabel) + 1,
// Fill remaining horizontal space
Width = Dim.Fill (),
};
var passwordLabel = new Label () {
Text = "Password:",
X = Pos.Left (usernameLabel),
Y = Pos.Bottom (usernameLabel) + 1
};
var passwordText = new TextField ("") {
Secret = true,
// align with the text box above
X = Pos.Left (usernameText),
Y = Pos.Top (passwordLabel),
Width = Dim.Fill (),
};
// Create login button
var btnLogin = new Button () {
Text = "Login",
Y = Pos.Bottom(passwordLabel) + 1,
// center the login button horizontally
X = Pos.Center (),
IsDefault = true,
};
// When login button is clicked display a message popup
btnLogin.Clicked += () => {
if (usernameText.Text == "admin" && passwordText.Text == "password") {
MessageBox.Query ("Logging In", "Login Successful", "Ok");
Application.RequestStop ();
} else {
MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
}
};
// Add the views to the Window
Add (usernameLabel, usernameText, passwordLabel, passwordText, btnLogin);
}
}
```
When run the application looks as follows:
![Simple Usage app](../images/Example.png)
## Building the Library and Running the Examples
* Windows, Mac, and Linux - Build and run using the .NET SDK command line tools (`dotnet build` in the root directory). Run `UICatalog` with `dotnet run --project UICatalog`.
* Windows - Open `Terminal.sln` with Visual Studio 202x.