1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-10 11:47:35 +00:00

LibGUI+TextEditor: Add the calculation of selected words

This moves the calculation of selected words that was originally
in the TextEditor application to TextEditor in LibGUI.
This allows all applications with text editors to get
this number without having to calculating it themselves.
This commit is contained in:
ry-sev 2021-05-25 20:58:55 -04:00 committed by Linus Groh
parent 074813e441
commit 8146543a43
3 changed files with 25 additions and 13 deletions

View file

@ -765,20 +765,8 @@ void MainWidget::update_statusbar()
builder.appendff("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column());
if (m_editor->has_selection()) {
int word_count = 0;
bool in_word = false;
String selected_text = m_editor->selected_text();
for (char c : selected_text) {
if (in_word && isspace(c)) {
in_word = false;
word_count++;
continue;
}
if (!in_word && !isspace(c))
in_word = true;
}
if (in_word)
word_count++;
auto word_count = m_editor->number_of_selected_words();
builder.appendff(" Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
}
m_statusbar->set_text(builder.to_string());