Skip to content

Commit

Permalink
Merge pull request #131 from michaelb/dev
Browse files Browse the repository at this point in the history
v1.1.1
  • Loading branch information
michaelb authored Dec 19, 2021
2 parents 68ebb6c + 6044f27 commit 4ab5996
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.1.1
- Fix terminal display issues
- Configurable display options

## v1.1.0
- TerminalWithCode display option (courtesy of @control13)
- Fix default interpreter issue
Expand Down
22 changes: 11 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sniprun"
version = "1.1.0"
version = "1.1.1"
authors = ["michaelb <[email protected]>"]
edition = "2018"

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ require'sniprun'.setup({
-- "Api" --# return output to a programming interface
},
display_options = {
terminal_width = 45, --# change the terminal display option width
notification_timeout = 5 --# timeout for nvim_notify output
},
--# You can use the same keys to customize whether a sniprun producing
--# no output should display nothing or '(no output)'
show_no_output = {
Expand Down
2 changes: 1 addition & 1 deletion doc/Prolog_gnu.md → doc/Prolog_original.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ you can set it with the following key:
```
require'sniprun'.setup({
interpreter_options = {
Prolog_gnu = { interpreter = "swipl" }
Prolog_original = { interpreter = "swipl" }
}
}
})
Expand Down
9 changes: 7 additions & 2 deletions lua/sniprun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ M.config_values = {
-- "NvimNotify"
},

display_options = {
terminal_width = 45, -- change the terminal display option width
notification_timeout = 5 -- timeout for nvim_notify output
},

show_no_output = {
"Classic",
"TempFloatingWindow", -- implies LongTempFloatingWindow, which is not a correct key here
"TempFloatingWindow", -- implies LongTempFloatingWindow, which is not a correct key here
},

inline_messages = 0,
Expand Down Expand Up @@ -109,7 +114,7 @@ end

function M.setup_highlights()
local colors_table = M.config_values["snipruncolors"]
if M.custom_highlight then
if M.custom_highlight then
vim.cmd('augroup snip_highlights')
vim.cmd('autocmd!')
for group, styles in pairs(colors_table) do
Expand Down
6 changes: 4 additions & 2 deletions lua/sniprun/display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ end

function M.term_open()
if M.term.opened ~= 0 then return end
vim.cmd(':rightb45vsplit')
local open_term_cmd = ':rightb'.. require('sniprun').config_values.display_options.terminal_width .. 'vsplit'
vim.cmd(open_term_cmd)
local buf = vim.api.nvim_create_buf(false,true)
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(win,buf)
local chan = vim.api.nvim_open_term(buf, {})
vim.cmd("set scrollback=1")
vim.cmd('setlocal nonu')

vim.cmd("wincmd p")
M.term.opened = 1
Expand Down Expand Up @@ -122,7 +124,7 @@ function M.display_nvim_notify(message, ok)

local title = ok and "Sniprun: Ok" or "Sniprun: Error"
local notif_style = ok and "info" or "error"
require("notify")(message, notif_style, {title=title})
require("notify")(message, notif_style, {title=title, timeout=require('sniprun').config_values.display_options.notification_timeout})
end

function M.display_extmark(ns,line, message, highlight)
Expand Down
7 changes: 4 additions & 3 deletions ressources/CONTRIBUTING_REPL.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ To avoid confusion, we'll call the language interpreter 'interpreter', and snipr
- you make use of a named pipe (fifo) and pipe what sniprun says into it. the pipe is connected to a live, running, interpreter for your language. Its output is written to a file and sniprun waits for landmarks (start, end) to be printed.


I strongly advise the latter methodology, which has several advantages that I won't discuss here, but can be harder to implement if your language's interpreter has weird stdin/stdou/stderr behavior. Like non-disablable prompts printed to stdout.
I strongly advise the latter methodology, which has several advantages that I won't discuss here, but can be harder to implement if your language's interpreter has weird stdin/stdout/stderr behavior. Like non-disablable prompts printed to stdout.


## How to implement a pipe-based repl-capable runner
## How to implement a pipe-based repl-capable runner

The best example I'm going to discuss is Python3\_fifo, even if it's a bit bloated from python-specific things.

Expand Down Expand Up @@ -119,4 +119,5 @@ fn new_with_level(...)
....
```

- disable prompts for your interpreter. They'll pollute stdout
- disable prompts for your interpreter. They'll pollute stdout. For example, in python, you'll have to set `sys.ps1` and `sys.ps2` to `""`.

8 changes: 8 additions & 0 deletions ressources/display_notify.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ require'sniprun'.setup({
})
EOF
```

The notification timeout can be changed with this configuration option:

```
display_options = {
notification_timeout = 5 -- timeout for nvim_notify output
},
```
7 changes: 6 additions & 1 deletion ressources/display_terminal.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ require'sniprun'.setup({
})
EOF
```

You can change the width of the terminal by using the display option in the configuration:
```
display_options = {
terminal_width = 45, -- change the terminal display option width
},
```


![](visual_assets/terminal.png)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#[derive(Clone)]
#[allow(non_camel_case_types)]
pub struct Prolog_gnu {
pub struct Prolog_original {
support_level: SupportLevel,
data: DataHolder,
code: String,
prolog_work_dir: String,
main_file_path: String,
interpreter: String,
}
impl ReplLikeInterpreter for Prolog_gnu {}
impl Interpreter for Prolog_gnu {
fn new_with_level(data: DataHolder, level: SupportLevel) -> Box<Prolog_gnu> {
let bwd = data.work_dir.clone() + "/prolog-gnu";
impl ReplLikeInterpreter for Prolog_original {}
impl Interpreter for Prolog_original {
fn new_with_level(data: DataHolder, level: SupportLevel) -> Box<Prolog_original> {
let bwd = data.work_dir.clone() + "/prolog-original";
let mut builder = DirBuilder::new();
builder.recursive(true);
builder
.create(&bwd)
.expect("Could not create directory for prolog-gnu");
.expect("Could not create directory for prolog-original");
let mfp = bwd.clone() + "/main.pl";
Box::new(Prolog_gnu {
Box::new(Prolog_original {
data,
support_level: level,
code: String::from(""),
Expand All @@ -28,7 +28,7 @@ impl Interpreter for Prolog_gnu {
})
}
fn get_name() -> String {
String::from("Prolog_gnu")
String::from("Prolog_original")
}
fn get_supported_languages() -> Vec<String> {
vec![String::from("Prolog"), String::from("prolog")]
Expand Down Expand Up @@ -86,9 +86,9 @@ impl Interpreter for Prolog_gnu {
}
fn build(&mut self) -> Result<(), SniprunError> {
let mut _file =
File::create(&self.main_file_path).expect("Failed to create file for prolog-gnu");
File::create(&self.main_file_path).expect("Failed to create file for prolog-original");

write(&self.main_file_path, &self.code).expect("Unable to write to file for prolog-gnu");
write(&self.main_file_path, &self.code).expect("Unable to write to file for prolog-original");
Ok(())
}
fn execute(&mut self) -> Result<String, SniprunError> {
Expand All @@ -108,7 +108,7 @@ impl Interpreter for Prolog_gnu {
.output()
.expect("Unable to start process");
}
info!("yay from gnu Prolog interpreter");
info!("yay from Prolog interpreter");
if output.status.success() {
Ok(String::from_utf8(output.stdout).unwrap())
} else {
Expand All @@ -119,14 +119,14 @@ impl Interpreter for Prolog_gnu {
}
}
#[cfg(test)]
mod test_prolog_gnu {
mod test_prolog_original {
use super::*;

// #[test]
fn simple_print() {
let mut data = DataHolder::new();
data.current_bloc = String::from(":- write(ok), halt.");
let mut interpreter = Prolog_gnu::new(data);
let mut interpreter = Prolog_original::new(data);
let res = interpreter.run();

// should panic if not an Ok()
Expand Down
14 changes: 9 additions & 5 deletions src/interpreters/Python3_fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ impl Python3_fifo {

let already_imported: String = self.read_previous_code();
if !already_imported.contains(line) {
self.imports = self.imports.clone() + "\n" + line;
self.save_code(already_imported + "\n" + line);
let line = unindent(&line);
self.imports = self.imports.clone() + "\n" + &line;
self.save_code(already_imported + "\n" + &line);
}
}
}
Expand Down Expand Up @@ -313,7 +314,9 @@ impl Interpreter for Python3_fifo {

self.code = source_venv
+ &self.imports.clone()
+ &unindent(&format!("{}{}", "\n", self.code.as_str()));
+ "\n## Imports above, code below, a #\\n# marker is very important to separate the try/catch bloc from the code ##Here it is: #\n#"
+ &unindent(&format!("{}{}", "\n\n", self.code.as_str()));
info!("source code::::: {}", self.code);
Ok(())
}
fn build(&mut self) -> Result<(), SniprunError> {
Expand Down Expand Up @@ -414,9 +417,10 @@ impl ReplLikeInterpreter for Python3_fifo {
.lines()
.filter(|l| !l.trim().is_empty())
.collect::<Vec<&str>>()
.join("\n");
.join("\n")
.replace("#\n#","\n");

let all_code = self.imports.clone() + "\n" + &self.code + "\n\n";
let all_code = String::from("\n") + &self.code + "\n\n";
self.code = String::from("\nimport sys\n\n")
+ &start_mark
+ &start_mark_err
Expand Down
3 changes: 2 additions & 1 deletion src/interpreters/Python3_jupyter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl Python3_jupyter {
&& self.module_used(line, &self.code)
{
// embed in try catch blocs in case uneeded module is unavailable
self.imports = self.imports.clone() + "\n" + line;
let line = unindent(&line);
self.imports = self.imports.clone() + "\n" + &line;
}
}
info!("import founds : {:?}", self.imports);
Expand Down
4 changes: 3 additions & 1 deletion src/interpreters/Python3_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl Python3_original {
&& self.module_used(line, &self.code)
{
// embed in try catch blocs in case uneeded module is unavailable
self.imports = self.imports.clone() + "\n" + line;
let line = unindent(&line);
self.imports = self.imports.clone() + "\n" + &line;
}
}
info!("import founds : {:?}", self.imports);
Expand Down Expand Up @@ -227,6 +228,7 @@ impl Interpreter for Python3_original {

self.code = source_venv
+ &self.imports.clone()
+ "\n"
+ &unindent(&format!("{}{}", "\n", self.code.as_str()));
Ok(())
}
Expand Down

0 comments on commit 4ab5996

Please sign in to comment.