0 votes
by (140 points)
edited

I did not find the method to search for text in the terminal ssh you have a solution

Thanks

2 Answers

0 votes
by (70.2k points)
edited

There is no such a method, but you can simply do it yourself like this:

bool FindText(VirtualTerminal terminal, string text, out int row, out int column)
{
    TerminalScreen s = terminal.Screen;

    // get all lines of the screen
    string[] lines = s.GetRegionText(0, 0, s.Columns, s.Rows);

    // iterate through all lines to find the text
    for (int r = 0; r < lines.Length; r++)
    {
        // check whether the text is on the current line
        int c = lines[r].IndexOf(text);
        if (c >= 0)
        {
            row = r;
            column = c;
            return true;
        }
    }

    // the text not found
    row = 0;
    column = 0;
    return false;
}

It can be used like this:

int row, column;
string text = "login:";
if (FindText(terminal, text, out row, out column))
    Console.WriteLine("Text '{0}' found at: {1}x{2}.", text, column, row);
else
    Console.WriteLine("Text '{0}' not found.", text);

If you would like to search the text using a Regex or using case insensitive search, adjust the int c = lines[r].IndexOf(text) line appropriately.

0 votes
by (15.2k points)
edited

Here is an alternative solution for the method Lukas posted, but this goes through the terminal history, not only the terminal current screen.

public bool FindText(string text, int startRow, int startColumn)
{
    // Get screen with history
    MemoryStream ms = new MemoryStream();
    terminal.Save(ms, TerminalCaptureFormat.Text, TerminalCaptureOptions.SaveHistory);
    string data = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);

    // get lines
    string[] lines = data.Split('\n');

    // iterate through the lines
    for (int r = startRow; r < lines.Length - 1; r++)
    {
        int c;
        if (r == startRow)
            c = lines[r].IndexOf(text, startColumn);
        else
            c = lines[r].IndexOf(text);

        if (c >= 0)
        {
            int rowOffset = r - (lines.Length - 1 - terminal.Screen.Rows);
            terminal.SetSelection(c, rowOffset, c + text.Length - 1, rowOffset);

            terminal.Scroll(terminal.HistoryMaxLength);
            terminal.Scroll(-(r - 2));
            _textFindColumn = c + 1;
            _textFindRow = r;
            return true;
        }
    }
    _textFindColumn = 0;
    _textFindRow = 0;
    return false;
}
...