Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zao111222333 committed Aug 13, 2024
1 parent 2edf1eb commit d87f196
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 18 deletions.
37 changes: 22 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2021"
thiserror = "1.0"
pyo3 = { version = "0.22.0", features = ["experimental-inspect"] }
clap = { version = "4.5.15", features = ["derive", "color"] }
rustyline = { version = "14.0.0", features = ["custom-bindings", "derive"]}
rustyline = { git = "https://github.com/zao111222333/rustyline.git", features = ["custom-bindings", "derive"]}
# rustyline = { path = "../rustyline", features = ["custom-bindings", "derive"]}
# rustyline = { version = "14.0.0", features = ["custom-bindings", "derive"]}
25 changes: 24 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
py, ExecMode, PROMPT1, PROMPT1_ERR, PROMPT1_OK, PROMPT2, PROMPT2_ERR, PROMPT2_OK,
TERMINATE_N,
PROMPT2_OK_NEWLINE, TERMINATE_N,
};
use pyo3::{
types::{PyAnyMethods, PyModule},
Expand Down Expand Up @@ -96,6 +96,17 @@ impl MyHelper {
}

impl Highlighter for MyHelper {
fn continuation_prompt<'p, 'b>(
&self,
_prompt: &'p str,
default: bool,
) -> Option<Cow<'b, str>> {
if default {
Some(Borrowed(PROMPT2_OK))
} else {
Some(Borrowed(PROMPT2))
}
}
fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
&'s self,
prompt: &'p str,
Expand Down Expand Up @@ -130,6 +141,10 @@ fn run_shell() -> Result<(), ExecErr> {
KeyEvent(KeyCode::BackTab, Modifiers::NONE),
EventHandler::Simple(Cmd::Dedent(Movement::BackwardChar(4))),
);
rl.bind_sequence(
KeyEvent(KeyCode::Char('s'), Modifiers::CTRL),
EventHandler::Simple(Cmd::Newline),
);
let mut code = String::new();
let mut prompt = PROMPT1;
let mut terminate_count: u8 = 0;
Expand Down Expand Up @@ -270,6 +285,14 @@ mod test {
exec_file(&PathBuf::from("tests/test1.py"), vec![]).expect("msg");
}
#[test]
fn test_shell() {
use super::*;
use py::foo;
pyo3::append_to_inittab!(foo);
pyo3::prepare_freethreaded_python();
run_shell().expect("msg");
}
#[test]
fn test_pyo3() {
use super::*;
pyo3::prepare_freethreaded_python();
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const PROMPT2: &str = " .... > ";
const PROMPT1_OK: &str = "\x1b[1;94mpyapp\x1b[1;32m > \x1b[m";
const PROMPT1_ERR: &str = "\x1b[1;94mpyapp\x1b[1;91m > \x1b[m";
const PROMPT2_OK: &str = "\x1b[1;94m ....\x1b[1;32m > \x1b[m";
const PROMPT2_OK_NEWLINE: &str = "\n\x1b[1;94m ....\x1b[1;32m > \x1b[m";
const PROMPT2_ERR: &str = "\x1b[1;94m ....\x1b[1;91m > \x1b[m";
const TERMINATE_N: u8 = 2;

Expand Down
5 changes: 5 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
cargo build --release
for file in tests/*.py; do
./package/pyapp $file
done
2 changes: 1 addition & 1 deletion tests/test1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
print(1)
print(2)
print(3)
print('\n')
if True:
print(1)
print(2)
foo.exit(1)
print(3)
2 changes: 2 additions & 0 deletions tests/test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import foo, sys
print(sys.argv)

0 comments on commit d87f196

Please sign in to comment.