using System.Text; using System.Text; namespace PainoBar_Helper { public partial class StationForm : Form { public StationForm() { InitializeComponent(); // Handle form load and closing for event subscription this.Load += StationForm_Load; this.FormClosing += StationForm_FormClosing; InitializeControls(); } private void StationForm_Load(object? sender, EventArgs e) { // First, populate terminal with existing history if (terminal_box != null && Main.TerminalHistory.Count > 0) { foreach (string line in Main.TerminalHistory) { terminal_box.AppendText(line + "\n"); } terminal_box.SelectionStart = terminal_box.Text.Length; terminal_box.ScrollToCaret(); } // Second, subscribe to live output events Main.OnPianobarOutputReceived += AppendToTerminal; } private void StationForm_FormClosing(object? sender, FormClosingEventArgs e) { // Unsubscribe from the static event Main.OnPianobarOutputReceived -= AppendToTerminal; } #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 AppendToTerminal(string data) { if (string.IsNullOrEmpty(data)) return; // Thread-safe UI update if (terminal_box != null) { if (terminal_box.InvokeRequired) { terminal_box.Invoke(() => { AppendTerminalText(data + "\n"); }); } else { AppendTerminalText(data + "\n"); } } } 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 { // Send command directly to Pianobar stdin (no manual echo with ">") Main.SendStdin(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 } }