From 9f91f886890219d0b0468c33207ef3b3ceca203b Mon Sep 17 00:00:00 2001 From: Louis-Marie Baer Date: Sat, 23 Mar 2024 16:41:29 +0100 Subject: [PATCH] feat: optmize console output to render only what is visible egui TextEdit::multiligne will render everything. Use show_rows insteead of show_viewport for AreaScroll to fix this. --- TODO_XMRvsBeast.md | 6 +- src/app/panels/middle/p2pool/mod.rs | 85 +++++++++++++++-------------- src/app/panels/middle/xmrig.rs | 70 +++++++++++------------- src/app/panels/middle/xvb.rs | 22 ++++++-- src/utils/regex.rs | 6 ++ 5 files changed, 102 insertions(+), 87 deletions(-) diff --git a/TODO_XMRvsBeast.md b/TODO_XMRvsBeast.md index be1f903..0731fb1 100644 --- a/TODO_XMRvsBeast.md +++ b/TODO_XMRvsBeast.md @@ -45,13 +45,17 @@ - [x] output log to console in XvB tab - [x] fix some overlapping from upstream - [ ] edit metadata of project - - [ ] adapt README for XvB + - [ ] adapt README for XvB + - [ ] beta release + - [ ] stable release + - [ ] video tutorial to set up XvB Tab - [ ] adapt doc for new code - [ ] cargo package metadata - [ ] pgp signatures - [x] fix clippy - [ ] optimizations - [x] benchmarks table render only what is visible + - [x] console output render only what is visible - [ ] migrate to hyper stable - [ ] use tor socks proxy instead of creating one - [ ] better organize new code diff --git a/src/app/panels/middle/p2pool/mod.rs b/src/app/panels/middle/p2pool/mod.rs index 801d140..7a34d2a 100644 --- a/src/app/panels/middle/p2pool/mod.rs +++ b/src/app/panels/middle/p2pool/mod.rs @@ -1,6 +1,7 @@ use crate::disk::node::Node; use crate::disk::state::{P2pool, State}; use crate::helper::p2pool::PubP2poolApi; +use crate::regex::num_lines; // Gupax - GUI Uniting P2Pool And XMRig // // Copyright (c) 2022-2023 hinto-janai @@ -18,7 +19,11 @@ use crate::helper::p2pool::PubP2poolApi; // You should have received a copy of the GNU General Public License // along with this program. If not, see . use crate::{components::node::*, constants::*, helper::*, macros::*, utils::regex::Regexes}; -use egui::{vec2, Color32, Label, RichText, TextEdit, TextStyle::*, Vec2}; +use egui::{ + vec2, Color32, Label, RichText, TextEdit, + TextStyle::{self, *}, + Vec2, +}; use log::*; use std::sync::{Arc, Mutex}; @@ -45,49 +50,45 @@ impl P2pool { let width = size.x; let text_edit = size.y / 25.0; //---------------------------------------------------------------------------------------------------- [Simple] Console - debug!("P2Pool Tab | Rendering [Console]"); + // debug!("P2Pool Tab | Rendering [Console]"); ui.group(|ui| { - if self.simple { - // height of console = height - address - simple(dropmenu, select_bar, buttons, warning) - let height = (size.y * 0.38) - SPACE; - let width = size.x - SPACE; - egui::Frame::none().fill(DARK_GRAY).show(ui, |ui| { - ui.style_mut().override_text_style = Some(Name("MonospaceSmall".into())); - egui::ScrollArea::vertical() - .stick_to_bottom(true) - .max_width(width) - .max_height(height) - .auto_shrink([false; 2]) - .show_viewport(ui, |ui, _| { - ui.add_sized( - [width, height], - TextEdit::multiline(&mut lock!(api).output.as_str()), - ); - }); - }); - //---------------------------------------------------------------------------------------------------- [Advanced] Console + let text = &lock!(api).output; + let nb_lines = num_lines(text); + let (height, width) = if self.simple { + ((size.y * 0.38) - SPACE, size.x - SPACE) } else { - // fix for advanced submenu overlap on bottom when small window - let height = if size.y < 600.0 { - size.y * 0.22 - SPACE - } else { - size.y * 0.36 - SPACE - }; - let width = width - SPACE; - egui::Frame::none().fill(DARK_GRAY).show(ui, |ui| { - ui.style_mut().override_text_style = Some(Name("MonospaceSmall".into())); - egui::ScrollArea::vertical() - .stick_to_bottom(true) - .max_width(width) - .max_height(height) - .auto_shrink([false; 2]) - .show_viewport(ui, |ui, _| { - ui.add_sized( - [width, height], - TextEdit::multiline(&mut lock!(api).output.as_str()), - ); - }); - }); + ( + if size.y < 600.0 { + size.y * 0.22 - SPACE + } else { + size.y * 0.36 - SPACE + }, + width - SPACE, + ) + }; + egui::Frame::none().fill(DARK_GRAY).show(ui, |ui| { + ui.style_mut().override_text_style = Some(Name("MonospaceSmall".into())); + egui::ScrollArea::vertical() + .stick_to_bottom(true) + .max_width(width) + .max_height(height) + .auto_shrink([false; 2]) + // .show_viewport(ui, |ui, _| { + .show_rows( + ui, + ui.text_style_height(&TextStyle::Name("MonospaceSmall".into())), + nb_lines, + |ui, row_range| { + for i in row_range { + if let Some(line) = text.lines().nth(i) { + ui.label(line); + } + } + }, + ); + }); + if !self.simple { + //---------------------------------------------------------------------------------------------------- [Advanced] Console ui.separator(); let response = ui .add_sized( diff --git a/src/app/panels/middle/xmrig.rs b/src/app/panels/middle/xmrig.rs index 6d5c4e1..00e1356 100644 --- a/src/app/panels/middle/xmrig.rs +++ b/src/app/panels/middle/xmrig.rs @@ -19,12 +19,13 @@ use crate::disk::pool::Pool; use crate::disk::state::Xmrig; use crate::helper::xmrig::PubXmrigApi; use crate::helper::Process; -use crate::regex::REGEXES; +use crate::regex::{num_lines, REGEXES}; use crate::utils::regex::Regexes; use crate::{constants::*, macros::*}; use egui::{ vec2, Button, Checkbox, ComboBox, Label, RichText, SelectableLabel, Slider, TextEdit, - TextStyle::*, Vec2, + TextStyle::{self, *}, + Vec2, }; use log::*; @@ -47,43 +48,36 @@ impl Xmrig { //---------------------------------------------------------------------------------------------------- [Simple] Console debug!("XMRig Tab | Rendering [Console]"); ui.group(|ui| { - if self.simple { - let height = size.y / 1.5; - let width = size.x - SPACE; - let size = vec2(width, height); - egui::Frame::none().fill(DARK_GRAY).show(ui, |ui| { - ui.style_mut().override_text_style = Some(Name("MonospaceSmall".into())); - egui::ScrollArea::vertical() - .stick_to_bottom(true) - .max_width(width) - .max_height(height) - .auto_shrink([false; 2]) - .show_viewport(ui, |ui, _| { - ui.add_sized( - size, - TextEdit::multiline(&mut lock!(api).output.as_str()), - ); - }); - }); - //---------------------------------------------------------------------------------------------------- [Advanced] Console + let text = &lock!(api).output; + let nb_lines = num_lines(text); + let (height, width) = if self.simple { + (size.y / 1.5, size.x - SPACE) } else { - let height = size.y / 2.8; - let width = size.x - SPACE; - let size = vec2(width, height); - egui::Frame::none().fill(DARK_GRAY).show(ui, |ui| { - ui.style_mut().override_text_style = Some(Name("MonospaceSmall".into())); - egui::ScrollArea::vertical() - .stick_to_bottom(true) - .max_width(width) - .max_height(height) - .auto_shrink([false; 2]) - .show_viewport(ui, |ui, _| { - ui.add_sized( - size, - TextEdit::multiline(&mut lock!(api).output.as_str()), - ); - }); - }); + (size.y / 2.8, size.x - SPACE) + }; + egui::Frame::none().fill(DARK_GRAY).show(ui, |ui| { + ui.style_mut().override_text_style = Some(Name("MonospaceSmall".into())); + egui::ScrollArea::vertical() + .stick_to_bottom(true) + .max_width(width) + .max_height(height) + .auto_shrink([false; 2]) + // .show_viewport(ui, |ui, _| { + .show_rows( + ui, + ui.text_style_height(&TextStyle::Name("MonospaceSmall".into())), + nb_lines, + |ui, row_range| { + for i in row_range { + if let Some(line) = text.lines().nth(i) { + ui.label(line); + } + } + }, + ); + }); + //---------------------------------------------------------------------------------------------------- [Advanced] Console + if !self.simple { ui.separator(); let response = ui .add_sized( diff --git a/src/app/panels/middle/xvb.rs b/src/app/panels/middle/xvb.rs index 1eb11b8..6092774 100644 --- a/src/app/panels/middle/xvb.rs +++ b/src/app/panels/middle/xvb.rs @@ -6,6 +6,7 @@ use log::debug; use readable::num::Float; use crate::helper::xvb::PubXvbApi; +use crate::regex::num_lines; use crate::utils::constants::{ GREEN, LIGHT_GRAY, ORANGE, RED, XVB_DONATED_1H_FIELD, XVB_DONATED_24H_FIELD, XVB_FAILURE_FIELD, XVB_HELP, XVB_HERO_SELECT, XVB_ROUND_TYPE_FIELD, XVB_TOKEN_FIELD, XVB_TOKEN_LEN, XVB_URL_RULES, @@ -47,6 +48,8 @@ impl crate::disk::state::Xvb { // console output for log debug!("XvB Tab | Rendering [Console]"); ui.group(|ui| { + let text = &lock!(api).output; + let nb_lines = num_lines(text); let height = size.y / 2.8; let width = size.x - (space_h / 2.0); egui::Frame::none().fill(DARK_GRAY).show(ui, |ui| { @@ -56,12 +59,19 @@ impl crate::disk::state::Xvb { .max_width(width) .max_height(height) .auto_shrink([false; 2]) - .show_viewport(ui, |ui, _| { - ui.add_sized( - [width, height], - TextEdit::multiline(&mut lock!(api).output.as_str()), - ); - }); + // .show_viewport(ui, |ui, _| { + .show_rows( + ui, + ui.text_style_height(&TextStyle::Name("MonospaceSmall".into())), + nb_lines, + |ui, row_range| { + for i in row_range { + if let Some(line) = text.lines().nth(i) { + ui.label(line); + } + } + }, + ); }); }); // input token diff --git a/src/utils/regex.rs b/src/utils/regex.rs index 9dca61a..4b26dc0 100644 --- a/src/utils/regex.rs +++ b/src/utils/regex.rs @@ -123,6 +123,12 @@ impl XmrigRegex { } } } + +// count the lines without consuming. +pub fn num_lines(s: &str) -> usize { + static LINE_BREAKS: Lazy = Lazy::new(|| Regex::new(r"\r?\n").unwrap()); + LINE_BREAKS.captures_iter(s).count() + 1 +} //---------------------------------------------------------------------------------------------------- TEST #[cfg(test)] mod test {