172 lines
4.7 KiB
C#
172 lines
4.7 KiB
C#
using System.Text;
|
|
using System.Text;
|
|
|
|
namespace PainoBar_Helper
|
|
{
|
|
public partial class StationForm : Form
|
|
{
|
|
private readonly PianobarManager _pianobarManager;
|
|
private readonly StringBuilder _outputBuffer = new StringBuilder();
|
|
|
|
public StationForm(PianobarManager pianobarManager)
|
|
{
|
|
InitializeComponent();
|
|
|
|
_pianobarManager = pianobarManager;
|
|
|
|
// Subscribe to output BEFORE controls are initialized
|
|
_pianobarManager.OutputReceived += PianobarManager_TerminalOutputReceived;
|
|
|
|
// Handle form load to display buffered output
|
|
this.Load += StationForm_Load;
|
|
|
|
InitializeControls();
|
|
}
|
|
|
|
private void StationForm_Load(object? sender, EventArgs e)
|
|
{
|
|
// Display any buffered output when form loads
|
|
if (terminal_box != null && _outputBuffer.Length > 0)
|
|
{
|
|
terminal_box.Text = _outputBuffer.ToString();
|
|
terminal_box.SelectionStart = terminal_box.Text.Length;
|
|
terminal_box.ScrollToCaret();
|
|
}
|
|
}
|
|
|
|
#region Initialization
|
|
|
|
private void InitializeControls()
|
|
{
|
|
// Wire up terminal output and command input
|
|
if (terminal_box != null)
|
|
{
|
|
terminal_box.ReadOnly = true;
|
|
terminal_box.Font = new System.Drawing.Font("Consolas", 9F);
|
|
terminal_box.WordWrap = false;
|
|
}
|
|
|
|
if (submit_button != null)
|
|
{
|
|
submit_button.Click += submit_button_Click;
|
|
}
|
|
|
|
if (command_input_box != null)
|
|
{
|
|
command_input_box.KeyDown += command_input_box_KeyDown;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Terminal Output
|
|
|
|
private void PianobarManager_TerminalOutputReceived(object? sender, string e)
|
|
{
|
|
if (string.IsNullOrEmpty(e))
|
|
return;
|
|
|
|
string lineWithNewline = e + "\n";
|
|
|
|
// Buffer output even before form is shown
|
|
_outputBuffer.Append(lineWithNewline);
|
|
|
|
// Thread-safe UI update
|
|
if (terminal_box != null)
|
|
{
|
|
if (terminal_box.InvokeRequired)
|
|
{
|
|
terminal_box.Invoke(() =>
|
|
{
|
|
AppendTerminalText(lineWithNewline);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
AppendTerminalText(lineWithNewline);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AppendTerminalText(string text)
|
|
{
|
|
if (terminal_box != null && !terminal_box.IsDisposed)
|
|
{
|
|
terminal_box.AppendText(text);
|
|
terminal_box.SelectionStart = terminal_box.Text.Length;
|
|
terminal_box.ScrollToCaret();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Command Input
|
|
|
|
public void submit_button_Click(object? sender, EventArgs e)
|
|
{
|
|
SubmitCommand();
|
|
}
|
|
|
|
private void command_input_box_KeyDown(object? sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
e.SuppressKeyPress = true;
|
|
SubmitCommand();
|
|
}
|
|
}
|
|
|
|
private void SubmitCommand()
|
|
{
|
|
if (command_input_box == null || string.IsNullOrWhiteSpace(command_input_box.Text))
|
|
return;
|
|
|
|
string command = command_input_box.Text.Trim();
|
|
|
|
try
|
|
{
|
|
// Echo the command to the terminal (simulating user input display)
|
|
AppendTerminalText($"> {command}\n");
|
|
|
|
// Send command to Pianobar with newline and flush
|
|
_pianobarManager.SendCommand(command);
|
|
|
|
command_input_box.Clear();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Failed to send command:\n{ex.Message}", "Command Error",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Close
|
|
|
|
public void btnCancelStation_Click(object? sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
public void btnSelectStation_Click(object? sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Cleanup
|
|
|
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
|
{
|
|
_pianobarManager.OutputReceived -= PianobarManager_TerminalOutputReceived;
|
|
base.OnFormClosing(e);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|