Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
wychlw committed Oct 5, 2024
1 parent b711e2f commit 9f2e3ea
Show file tree
Hide file tree
Showing 11 changed files with 569 additions and 17 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

build:
maturin develop
# cargo build

ui: build
cargo test --test ui_test

.PHONY: build ui
12 changes: 10 additions & 2 deletions src/exec/cli_api.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
//! The API for the CLI test.
use std::error::Error;

use crate::cli::tty::WrapperTty;

/// The API for the CLI test.
pub trait CliTestApi: WrapperTty {
///
/// Run a script in the terminal, wait for the script to finish(or timeout), and return the output.
///
/// You may found this func includes assert_script_run and script_output
fn script_run(&mut self, script: &str, timeout: u32) -> Result<String, Box<dyn Error>>;

/// Run a script in the terminal which is a background command
fn background_script_run(&mut self, script: &str) -> Result<(), Box<dyn Error>>;

/// Write someting to the terminal.
fn writeln(&mut self, script: &str) -> Result<(), Box<dyn Error>>;

/// Wait for the terminal to output the expected string. Output the terminal output when the expected string is found.
fn wait_serial(&mut self, expected: &str, timeout: u32) -> Result<String, Box<dyn Error>>;
}

pub trait SudoCliTestApi: CliTestApi {
///
/// Just like script_run, but with sudo.
///
/// You may found this func includes assert_script_sudo and script_output
fn script_sudo(&mut self, script: &str, timeout: u32) -> Result<String, Box<dyn Error>>;
Expand Down
2 changes: 2 additions & 0 deletions src/exec/cli_exec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! The implementation of the CLI tester. Look at [`CliTestApi`] for more information.
use std::{
error::Error,
thread::sleep,
Expand Down
2 changes: 1 addition & 1 deletion src/exec/gui_exec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Executor gor GUI
//! Executor for GUI. Look at [`GuiTestApi`] for more details.
//!
use std::{error::Error, time::Instant};
Expand Down
7 changes: 7 additions & 0 deletions src/exec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//! How to execute some operations for the CLI and GUI.
//!
//! This part contains the interactive API for the CLI and GUI. You definitely
//! could use the CLI API and GUI API directly, but you really want to see a bunch of
//! blocking IO and u8 slices?
//! So these wrappers are here to provide high-level API for the CLI and GUI.
pub mod cli_api;
pub mod cli_exec;
pub mod gui_api;
Expand Down
4 changes: 4 additions & 0 deletions src/pythonapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use tee::Tee;
use shell_like::PyTty;
use util::{get_log_level, run_ui, set_log_level};

use crate::ui::register_ui;

#[pymodule]
#[pyo3(name = "tester")]
fn tester(m: &Bound<'_, PyModule>) -> PyResult<()> {
Expand All @@ -52,5 +54,7 @@ fn tester(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(warn, m)?)?;
m.add_function(wrap_pyfunction!(err, m)?)?;

register_ui(m)?;

Ok(())
}
1 change: 1 addition & 0 deletions src/ui/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl Default for CodeEditor {
Self {
code: "\
from tester import *
from tester.ui import *
print('Hello, world!')
".to_string(),
Expand Down
34 changes: 21 additions & 13 deletions src/ui/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Main UI render for the APP
use std::error::Error;
use std::{
error::Error, thread::sleep, time::Duration,
};

use eframe::{
egui::{Context, Id, SidePanel, Ui, ViewportBuilder},
Expand Down Expand Up @@ -35,11 +37,11 @@ impl AppUi {
}
}

struct SubWindowHolder {
window: Box<dyn SubWindow>,
id: Id,
title: String,
open: bool,
pub struct SubWindowHolder {
pub window: Box<dyn SubWindow>,
pub id: Id,
pub title: String,
pub open: bool,
}

struct MyApp {
Expand Down Expand Up @@ -75,17 +77,23 @@ impl MyApp {
let id = Id::new(self.sub_window_idx);
info!("Try create sub window: {}", title);
self.sub_window_idx += 1;
self.sub_windows.push(SubWindowHolder {
sleep(Duration::from_millis(5));
let sub_windows = &mut self.sub_windows;
sub_windows.push(SubWindowHolder {
window: creator.open(),
id,
title,
open: true,
});
}
}
self.sub_windows.retain(|w| w.open);
for w in &mut self.sub_windows {
w.window.show(ctx, &w.title, &w.id, &mut w.open);
{
sleep(Duration::from_millis(5));
let sub_windows = &mut self.sub_windows;
sub_windows.retain(|w| w.open);
for w in sub_windows.iter_mut() {
w.window.show(ctx, &w.title, &w.id, &mut w.open);
}
}
});
}
Expand Down Expand Up @@ -120,15 +128,15 @@ pub trait SubWindowCreator {
}

/// Snippet to register a sub window
///
///
/// # Arguments
/// $name: The struct name of the sub window
/// $window_name: The name of the window, will become the title of the window
///
///
/// # Example
/// `impl_sub_window!(TestUiStruct, "TestUiName");`
/// where TestUiStruct implements SubWindow trait and Default trait
///
///
/// # Notice
/// If you found rust-analyzer gives "invalid metavariable expression", this is a nightly feature, you can ignore it. It will work.
/// The problem is on `${concat()}` macro. Just suppress it.
Expand Down
15 changes: 14 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ pub mod main;
pub mod code_editor;
pub mod pyenv;
pub mod terminal;
pub mod cli_hooker;
pub mod cli_hooker;
pub mod ui_cli_exec;
pub mod util;

use pyo3::{types::{PyModule, PyModuleMethods}, Bound, PyResult};
use ui_cli_exec::UiExec;

pub fn register_ui(parent_module: &Bound<'_, PyModule>) -> PyResult<()> {
let m = PyModule::new_bound(parent_module.py(), "ui")?;
m.add_class::<UiExec>()?;

parent_module.add_submodule(&m)?;
Ok(())
}
Loading

0 comments on commit 9f2e3ea

Please sign in to comment.