06-23-2023, 04:53 PM
(This post was last modified: 06-23-2023, 05:01 PM by burque505.
Edit Reason: Add reference to TerminalGuiDesigner
)
Sometimes a terminal GUI is useful. I picked Terminal.Gui to experiment with, probably just because it has the most stars on Github - it's not very hard to pick up.
The first example is cribbed from the Terminal.Gui docs pages, with the addition of a 'TextView', or multiline text entry. The console checkbox in Properties has to be checked for this to run.
The second example is a 'Wizard'.
Regards,
burque505
EDIT: It seems there's also a drag-and-drop designer for Terminal.GUI. It's in alpha according to the Github page and I haven't tried it, but it certainly looks interesting.
The first example is cribbed from the Terminal.Gui docs pages, with the addition of a 'TextView', or multiline text entry. The console checkbox in Properties has to be checked for this to run.
/*/ console true; nuget Terminal\Terminal.GUI; /*/
// A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
// For LA, make sure console checkbox in Properties is checked.
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
/// <summary>
/// Example from github page, https://github.com/gui-cs/Terminal.Gui
/// with multiline TextView added
/// </summary>
public class ExampleWindow : Window {
/// <summary>
/// User name
/// </summary>
public TextField usernameText;
/// <summary>
/// Example terminal window
/// </summary>
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(passwordText) + 1,
// center the login button horizontally
X = Pos.Center(),
IsDefault = true,
};
var commentBoxLabel = new Label() {
Text = "Multiline Comment Box",
X = Pos.Center(),
Y = Pos.Bottom(btnLogin) + 1
};
var commentBox = new TextView() {
X = Pos.Left(passwordLabel) + 2,
Y = Pos.Bottom(commentBoxLabel) + 1,
Width = Dim.Fill(2),
Height = 400,
WordWrap = 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, commentBoxLabel, commentBox);
}
}
The second example is a 'Wizard'.
/*/ console true; nuget Terminal\Terminal.GUI; /*/
// A simple Terminal.Gui example of Wizard in C# - using C# 9.0 Top-level statements
// From github page with minor modification.
// For LA, make sure console checkbox in Properties is checked.
using Terminal.Gui;
using NStack;
Application.Init();
var wizard = new Wizard ($"Setup Wizard");
// Add 1st step
var firstStep = new Wizard.WizardStep ("End User License Agreement");
wizard.AddStep(firstStep);
firstStep.NextButtonText = "Accept!";
firstStep.HelpText = @"This is the End User License Agreement.
You must accept this and offer up your first-born offspring
in order to continue. You must also pay monthly through the nose.
No exceptions!";
var firstLbl = new Label("Important! Click 'Accept' only after reading License Agreement!") { AutoSize = true };
firstStep.Add(firstLbl);
// Add 2nd step
var secondStep = new Wizard.WizardStep ("Second Step");
wizard.AddStep(secondStep);
secondStep.HelpText = @"Enter your full name on one line.
(The entry box may appear oversize on your terminal.)";
var lbl = new Label ("Name:") { AutoSize = true };
secondStep.Add(lbl);
var name = new TextField () { X = Pos.Right (lbl) + 1, Width = Dim.Fill () - 1 };
secondStep.Add(name);
wizard.Finished += (args) =>
{
MessageBox.Query("Wizard", $"Finished. The Name entered is '{name.Text}'", "Ok");
Application.RequestStop();
};
Application.Top.Add (wizard);
Application.Run ();
Application.Shutdown ();
Regards,
burque505
EDIT: It seems there's also a drag-and-drop designer for Terminal.GUI. It's in alpha according to the Github page and I haven't tried it, but it certainly looks interesting.