Browse Source

Improve embed terminal

- Add character attribute support
- Add cursor key mode support
- Fix buggy set fg / bg sequences

And added a bin under tools to test arbitrary apps using the embedded
terminal:

 cargo run -p tools --bin embed -- "htop" 2> .htop.debug.log
tables
Manos Pitsidianakis 3 months ago
parent
commit
a73885acb1
  1. 5
      Cargo.lock
  2. 4
      Cargo.toml
  3. 2
      src/components/mail/compose.rs
  4. 38
      src/conf/listing.rs
  5. 5
      src/jobs.rs
  6. 80
      src/lib.rs
  7. 53
      src/main.rs
  8. 2
      src/terminal.rs
  9. 48
      src/terminal/cells.rs
  10. 4
      src/terminal/color.rs
  11. 21
      src/terminal/embed.rs
  12. 456
      src/terminal/embed/grid.rs
  13. 33
      src/terminal/position.rs
  14. 14
      tools/Cargo.toml
  15. 581
      tools/src/embed.rs

5
Cargo.lock

@ -1880,7 +1880,12 @@ dependencies = [
name = "tools"
version = "0.4.1"
dependencies = [
"crossbeam",
"meli",
"melib",
"nix",
"signal-hook",
"signal-hook-registry",
]
[[package]]

4
Cargo.toml

@ -17,6 +17,10 @@ default-run = "meli"
name = "meli"
path = "src/main.rs"
[lib]
name = "meli"
path = "src/lib.rs"
#[[bin]]
#name = "managesieve-meli"
#path = "src/managesieve.rs"

2
src/components/mail/compose.rs

@ -1414,7 +1414,7 @@ impl Component for Composer {
use std::io::Write;
if let Some(ref mut embed) = self.embed {
let mut embed_guard = embed.lock().unwrap();
if embed_guard.stdin.write_all(b).is_err() {
if embed_guard.write_all(b).is_err() {
match embed_guard.is_active() {
Ok(WaitStatus::Exited(_, exit_code)) => {
drop(embed_guard);

38
src/conf/listing.rs

@ -29,25 +29,31 @@ use melib::{MeliError, Result};
/// Tree decoration examples:
///
///```no_run
///const HAS_SIBLING: &'static str = " โ”ƒ";
///const NO_SIBLING: &'static str = " ";
///const HAS_SIBLING_LEAF: &'static str = " โ”ฃโ”";
///const NO_SIBLING_LEAF: &'static str = " โ”—โ”";
///const HAS_SIBLING: &str = " โ”ƒ";
///const NO_SIBLING: &str = " ";
///const HAS_SIBLING_LEAF: &str = " โ”ฃโ”";
///const NO_SIBLING_LEAF: &str = " โ”—โ”";
///```
///
///const HAS_SIBLING: &'static str = " |";
///const NO_SIBLING: &'static str = " ";
///const HAS_SIBLING_LEAF: &'static str = " |\\_";
///const NO_SIBLING_LEAF: &'static str = " \\_";
///```no_run
///const HAS_SIBLING: &str = " |";
///const NO_SIBLING: &str = " ";
///const HAS_SIBLING_LEAF: &str = " |\\_";
///const NO_SIBLING_LEAF: &str = " \\_";
///```
///
///const HAS_SIBLING: &'static str = " ";
///const NO_SIBLING: &'static str = " ";
///const HAS_SIBLING_LEAF: &'static str = " ";
///const NO_SIBLING_LEAF: &'static str = " ";
///```no_run
///const HAS_SIBLING: &str = " ";
///const NO_SIBLING: &str = " ";
///const HAS_SIBLING_LEAF: &str = " ";
///const NO_SIBLING_LEAF: &str = " ";
///```
///
///const HAS_SIBLING: &'static str = " โ”‚";
///const NO_SIBLING: &'static str = " ";
///const HAS_SIBLING_LEAF: &'static str = " โ”œโ”€";
///const NO_SIBLING_LEAF: &'static str = " โ•ฐโ”€";
///```no_run
///const HAS_SIBLING: &str = " โ”‚";
///const NO_SIBLING: &str = " ";
///const HAS_SIBLING_LEAF: &str = " โ”œโ”€";
///const NO_SIBLING_LEAF: &str = " โ•ฐโ”€";
///```
#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(deny_unknown_fields)]

5
src/jobs.rs

@ -20,11 +20,6 @@
*/
//! Async job executor thread pool
//!
//! ## Usage
//! ```no_run
//! let (channel, handle, job_id) = job_executor.spawn(job);
//! ```
use melib::smol;
use melib::uuid::Uuid;

80
src/lib.rs

@ -0,0 +1,80 @@
/*
* meli - lib.rs
*
* Copyright 2017-2022 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
//!
//! This crate contains the frontend stuff of the application. The application entry way on
//! `src/bin.rs` creates an event loop and passes input to a thread.
//!
//! The mail handling stuff is done in the `melib` crate which includes all backend needs. The
//! split is done to theoretically be able to create different frontends with the same innards.
//!
use std::alloc::System;
pub use std::collections::VecDeque;
pub use std::path::PathBuf;
#[macro_use]
extern crate serde_derive;
extern crate linkify;
pub use melib::uuid;
pub extern crate bitflags;
pub extern crate serde_json;
#[macro_use]
pub extern crate smallvec;
pub extern crate termion;
pub use structopt::StructOpt;
#[global_allocator]
static GLOBAL: System = System;
#[macro_use]
extern crate melib;
pub use melib::*;
#[macro_use]
pub mod types;
pub use crate::types::*;
#[macro_use]
pub mod terminal;
pub use crate::terminal::*;
#[macro_use]
pub mod command;
pub use crate::command::*;
pub mod state;
pub use crate::state::*;
pub mod components;
pub use crate::components::*;
#[macro_use]
pub mod conf;
pub use crate::conf::*;
#[cfg(feature = "sqlite3")]
pub mod sqlite3;
pub mod jobs;
pub mod mailcap;

53
src/main.rs

@ -27,58 +27,7 @@
//! split is done to theoretically be able to create different frontends with the same innards.
//!
use std::alloc::System;
use std::collections::VecDeque;
use std::path::PathBuf;
#[macro_use]
extern crate serde_derive;
extern crate linkify;
pub(crate) use melib::uuid;
extern crate bitflags;
extern crate serde_json;
#[macro_use]
extern crate smallvec;
extern crate termion;
use structopt::StructOpt;
#[global_allocator]
static GLOBAL: System = System;
#[macro_use]
extern crate melib;
use melib::*;
#[macro_use]
pub mod types;
use crate::types::*;
#[macro_use]
pub mod terminal;
use crate::terminal::*;
#[macro_use]
pub mod command;
use crate::command::*;
pub mod state;
use crate::state::*;
pub mod components;
use crate::components::*;
#[macro_use]
pub mod conf;
use crate::conf::*;
#[cfg(feature = "sqlite3")]
pub mod sqlite3;
pub mod jobs;
pub mod mailcap;
use meli::*;
use std::os::raw::c_int;
fn notify(

2
src/terminal.rs

@ -312,7 +312,7 @@ mod braille {
/// ];
///
/// for lines in BEE.chunks(12) {
/// let iter = ui::BraillePixelIter::from(lines);
/// let iter = meli::BraillePixelIter::from(lines);
/// for b in iter {
/// print!("{}", b);
/// }

48
src/terminal/cells.rs

@ -197,15 +197,6 @@ impl CellBuffer {
/// Returns a reference to the `Cell` at the given coordinates, or `None` if the index is out of
/// bounds.
///
/// # Examples
///
/// ```no_run
///
/// let mut term = Terminal::new().unwrap();
///
/// let a_cell = term.get(5, 5);
/// ```
pub fn get(&self, x: usize, y: usize) -> Option<&Cell> {
match self.pos_to_index(x, y) {
Some(i) => self.cellvec().get(i),
@ -215,15 +206,6 @@ impl CellBuffer {
/// Returns a mutable reference to the `Cell` at the given coordinates, or `None` if the index
/// is out of bounds.
///
/// # Examples
///
/// ```no_run
///
/// let mut term = Terminal::new().unwrap();
///
/// let a_mut_cell = term.get_mut(5, 5);
/// ```
pub fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut Cell> {
match self.pos_to_index(x, y) {
Some(i) => self.cellvec_mut().get_mut(i),
@ -518,6 +500,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Color, Attr, Cell};
///
/// let cell = Cell::new('x', Color::Default, Color::Green, Attr::DEFAULT);
/// assert_eq!(cell.ch(), 'x');
/// assert_eq!(cell.fg(), Color::Default);
@ -542,6 +526,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Color, Attr, Cell};
///
/// let mut cell = Cell::with_char('x');
/// assert_eq!(cell.ch(), 'x');
/// assert_eq!(cell.fg(), Color::Default);
@ -557,6 +543,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Color, Attr, Cell};
///
/// let mut cell = Cell::with_style(Color::Default, Color::Red, Attr::BOLD);
/// assert_eq!(cell.fg(), Color::Default);
/// assert_eq!(cell.bg(), Color::Red);
@ -572,6 +560,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::Cell;
///
/// let mut cell = Cell::with_char('x');
/// assert_eq!(cell.ch(), 'x');
/// ```
@ -584,6 +574,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::Cell;
///
/// let mut cell = Cell::with_char('x');
/// assert_eq!(cell.ch(), 'x');
///
@ -603,6 +595,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Color, Attr, Cell};
///
/// let mut cell = Cell::with_style(Color::Blue, Color::Default, Attr::DEFAULT);
/// assert_eq!(cell.fg(), Color::Blue);
/// ```
@ -615,6 +609,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Color, Cell};
///
/// let mut cell = Cell::default();
/// assert_eq!(cell.fg(), Color::Default);
///
@ -633,6 +629,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Attr, Color, Cell};
///
/// let mut cell = Cell::with_style(Color::Default, Color::Green, Attr::DEFAULT);
/// assert_eq!(cell.bg(), Color::Green);
/// ```
@ -645,6 +643,8 @@ impl Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Cell, Color};
///
/// let mut cell = Cell::default();
/// assert_eq!(cell.bg(), Color::Default);
///
@ -708,6 +708,8 @@ impl Default for Cell {
/// # Examples
///
/// ```no_run
/// use meli::{Color, Cell};
///
/// let mut cell = Cell::default();
/// assert_eq!(cell.ch(), ' ');
/// assert_eq!(cell.fg(), Color::Default);
@ -728,6 +730,8 @@ bitflags::bitflags! {
/// # Examples
///
/// ```no_run
/// use meli::Attr;
///
/// // Default attribute.
/// let def = Attr::DEFAULT;
///
@ -1234,6 +1238,8 @@ pub fn clear_area(grid: &mut CellBuffer, area: Area, attributes: crate::conf::Th
/// `RowIterator` can be created via the `CellBuffer::row_iter` method and can be returned by
/// `BoundsIterator` which iterates each row.
/// ```no_run
/// # let mut grid = meli::CellBuffer::new(1, 1, meli::Cell::default());
/// # let x = 0;
/// for c in grid.row_iter(
/// x..(x + 11),
/// 0,
@ -1248,9 +1254,13 @@ pub struct RowIterator {
/// `BoundsIterator` iterates each row returning a `RowIterator`.
/// ```no_run
/// # let mut grid = meli::CellBuffer::new(1, 1, meli::Cell::default());
/// # let area = ((0, 0), (1, 1));
/// /* Visit each `Cell` in `area`. */
/// for c in grid.bounds_iter(area) {
/// for row in grid.bounds_iter(area) {
/// for c in row {
/// grid[c].set_ch('w');
/// }
/// }
/// ```
pub struct BoundsIterator {

4
src/terminal/color.rs

@ -34,7 +34,9 @@ use termion::color::{AnsiValue, Rgb as TermionRgb};
///
/// # Examples
///
/// ```no_run
/// ```
/// use meli::Color;
///
/// // The default color.
/// let default = Color::Default;
///

21
src/terminal/embed.rs

@ -210,6 +210,27 @@ pub enum State {
Csi1(SmallVec<[u8; 8]>),
Csi2(SmallVec<[u8; 8]>, SmallVec<[u8; 8]>),
Csi3(SmallVec<[u8; 8]>, SmallVec<[u8; 8]>, SmallVec<[u8; 8]>),
Csi4(
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
),
Csi5(
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
),
Csi6(
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
SmallVec<[u8; 8]>,
),
CsiQ(SmallVec<[u8; 8]>),
Normal,
}

456
src/terminal/embed/grid.rs

@ -53,11 +53,14 @@ pub struct EmbedGrid {
initialized: bool,
fg_color: Color,
bg_color: Color,
attrs: Attr,
/// Store the fg/bg color when highlighting the cell where the cursor is so that it can be
/// restored afterwards
prev_fg_color: Option<Color>,
prev_bg_color: Option<Color>,
prev_attrs: Option<Attr>,
cursor_key_mode: bool, // (DECCKM)
show_cursor: bool,
origin_mode: bool,
auto_wrap_mode: bool,
@ -73,11 +76,57 @@ pub struct EmbedGrid {
#[derive(Debug)]
pub struct EmbedTerminal {
pub grid: EmbedGrid,
pub stdin: std::fs::File,
stdin: std::fs::File,
/// Pid of the embed process
pub child_pid: nix::unistd::Pid,
}
impl std::io::Write for EmbedTerminal {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
/*
Key Normal Application
-------------+----------+-------------
Cursor Up | CSI A | SS3 A
Cursor Down | CSI B | SS3 B
Cursor Right | CSI C | SS3 C
Cursor Left | CSI D | SS3 D
-------------+----------+-------------
Key Normal Application
---------+----------+-------------
Home | CSI H | SS3 H
End | CSI F | SS3 F
---------+----------+-------------
*/
if self.grid.cursor_key_mode {
match buf {
&[0x1b, 0x5b, b'A']
| &[0x1b, 0x5b, b'B']
| &[0x1b, 0x5b, b'C']
| &[0x1b, 0x5b, b'D']
| &[0x1b, 0x5b, b'H']
| &[0x1b, 0x5b, b'F'] => {
self.stdin.write_all(&[0x1b, 0x4f, buf[2]])?;
Ok(buf.len())
}
_ => {
self.stdin.write_all(buf)?;
Ok(buf.len())
}
}
} else {
self.stdin.write_all(buf)?;
Ok(buf.len())
}
}
fn flush(&mut self) -> std::io::Result<()> {
self.stdin.flush()
}
}
impl EmbedTerminal {
pub fn new(stdin: std::fs::File, child_pid: nix::unistd::Pid) -> Self {
EmbedTerminal {
@ -156,11 +205,14 @@ impl EmbedGrid {
state: State::Normal,
fg_color: Color::Default,
bg_color: Color::Default,
attrs: Attr::DEFAULT,
prev_fg_color: None,
prev_bg_color: None,
prev_attrs: None,
show_cursor: true,
auto_wrap_mode: true,
wrap_next: false,
cursor_key_mode: false,
origin_mode: false,
codepoints: CodepointBuf::None,
normal_screen,
@ -219,12 +271,15 @@ impl EmbedGrid {
ref mut state,
ref mut fg_color,
ref mut bg_color,
ref mut attrs,
ref mut prev_fg_color,
ref mut prev_bg_color,
ref mut prev_attrs,
ref mut codepoints,
ref mut show_cursor,
ref mut auto_wrap_mode,
ref mut wrap_next,
ref mut cursor_key_mode,
ref mut origin_mode,
ref mut screen_buffer,
ref mut normal_screen,
@ -475,6 +530,7 @@ impl EmbedGrid {
grid[cursor_val!()].set_ch(c);
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
grid[cursor_val!()].set_attrs(*attrs);
match wcwidth(u32::from(c)) {
Some(0) | None => {
/* Skip drawing zero width characters */
@ -489,6 +545,7 @@ impl EmbedGrid {
grid[cursor_val!()].set_empty(true);
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
grid[cursor_val!()].set_attrs(*attrs);
}
}
}
@ -505,8 +562,10 @@ impl EmbedGrid {
//debug!("{}", EscCode::from((&(*state), byte)));
*fg_color = Color::Default;
*bg_color = Color::Default;
*attrs = Attr::DEFAULT;
grid[cursor_val!()].set_fg(Color::Default);
grid[cursor_val!()].set_bg(Color::Default);
grid[cursor_val!()].set_attrs(Attr::DEFAULT);
*state = State::Normal;
}
(b'C', State::Csi) => {
@ -523,6 +582,9 @@ impl EmbedGrid {
}
(b'h', State::CsiQ(ref buf)) => {
match buf.as_slice() {
b"1" => {
*cursor_key_mode = true;
}
b"6" => {
*origin_mode = true;
}
@ -533,13 +595,17 @@ impl EmbedGrid {
*show_cursor = true;
*prev_fg_color = Some(grid[cursor_val!()].fg());
*prev_bg_color = Some(grid[cursor_val!()].bg());
*prev_attrs = Some(grid[cursor_val!()].attrs());
grid[cursor_val!()].set_fg(Color::Black);
grid[cursor_val!()].set_bg(Color::White);
grid[cursor_val!()].set_attrs(Attr::DEFAULT);
}
b"1047" | b"1049" => {
*screen_buffer = ScreenBuffer::Alternate;
}
_ => {}
_ => {
debug!("unknown csi? {:?}", String::from_utf8_lossy(buf.as_slice()));
}
}
//debug!("{}", EscCode::from((&(*state), byte)));
@ -547,6 +613,9 @@ impl EmbedGrid {
}
(b'l', State::CsiQ(ref mut buf)) => {
match buf.as_slice() {
b"1" => {
*cursor_key_mode = false;
}
b"6" => {
*origin_mode = false;
}
@ -565,11 +634,21 @@ impl EmbedGrid {
} else {
grid[cursor_val!()].set_bg(*bg_color);
}
if let Some(attrs) = prev_attrs.take() {
grid[cursor_val!()].set_attrs(attrs);
} else {
grid[cursor_val!()].set_attrs(*attrs);
}
}
b"1047" | b"1049" => {
*screen_buffer = ScreenBuffer::Normal;
}
_ => {}
_ => {
debug!(
"unknown csi? `l` {:?}",
String::from_utf8_lossy(buf.as_slice())
);
}
}
//debug!("{}", EscCode::from((&(*state), byte)));
*state = State::Normal;
@ -975,14 +1054,63 @@ impl EmbedGrid {
// Character Attributes.
match buf1.as_slice() {
b"0" => {
*fg_color = Color::Default;
*bg_color = Color::Default;
*attrs = Attr::DEFAULT;
}
b"1" => {
/* bold */
*attrs |= Attr::BOLD;
}
b"2" => {
/* faint, dim */
*attrs |= Attr::DIM;
}
b"3" => {
/* italicized */
*attrs |= Attr::ITALICS;
}
b"4" => {
/* underlined */
*attrs |= Attr::UNDERLINE;
}
b"5" => {
/* blink */
*attrs |= Attr::BLINK;
}
b"7" => {
/* Inverse */
*attrs |= Attr::REVERSE;
}
b"8" => {
/* invisible */
*attrs |= Attr::HIDDEN;
}
b"9" => { /* crossed out */ }
b"21" => { /* Doubly-underlined */ }
b"22" => {
/* Normal (neither bold nor faint), ECMA-48 3rd. */
*attrs &= !(Attr::BOLD | Attr::DIM);
}
b"1" => { /* bold */ }
b"7" | b"27" => {
/* Inverse on/off */
std::mem::swap(&mut (*fg_color), &mut (*bg_color))
b"23" => {
/* Not italicized, ECMA-48 3rd */
*attrs &= !Attr::ITALICS;
}
b"24" => {
/* Not underlined, ECMA-48 3rd. */
*attrs &= !Attr::UNDERLINE;
}
b"25" => {
/* Steady (not blinking), ECMA-48 3rd. */
*attrs &= !Attr::BLINK;
}
b"27" => {
/* Positive (not inverse), ECMA-48 3rd. */
*attrs &= !Attr::REVERSE;
}
b"28" => {
/* Visible, i.e., not hidden, ECMA-48 3rd, VT300. */
*attrs &= !Attr::HIDDEN;
}
b"29" => { /* Not crossed-out, ECMA-48 3rd. */ }
b"30" => *fg_color = Color::Black,
b"31" => *fg_color = Color::Red,
b"32" => *fg_color = Color::Green,
@ -991,8 +1119,8 @@ impl EmbedGrid {
b"35" => *fg_color = Color::Magenta,
b"36" => *fg_color = Color::Cyan,
b"37" => *fg_color = Color::White,
b"39" => *fg_color = Color::Default,
b"40" => *bg_color = Color::Black,
b"41" => *bg_color = Color::Red,
b"42" => *bg_color = Color::Green,
@ -1001,7 +1129,6 @@ impl EmbedGrid {
b"45" => *bg_color = Color::Magenta,
b"46" => *bg_color = Color::Cyan,
b"47" => *bg_color = Color::White,
b"49" => *bg_color = Color::Default,
b"90" => *fg_color = Color::Black,
@ -1021,24 +1148,79 @@ impl EmbedGrid {
b"105" => *bg_color = Color::Magenta,
b"106" => *bg_color = Color::Cyan,
b"107" => *bg_color = Color::White,
_ => {}
_ => {
debug!(
"unknown attribute Csi1 {:?} m",
String::from_utf8_lossy(buf1.as_slice())
);
}
}
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
grid[cursor_val!()].set_attrs(*attrs);
*state = State::Normal;
}
(b'm', State::Csi2(ref buf1, ref buf2)) => {
for b in &[buf1, buf2] {
match b.as_slice() {
b"0" => {
*fg_color = Color::Default;
*bg_color = Color::Default;
*attrs = Attr::DEFAULT;
}
b"1" => {
/* bold */
*attrs |= Attr::BOLD;
}
b"2" => {
/* faint, dim */
*attrs |= Attr::DIM;
}
b"3" => {
/* italicized */
*attrs |= Attr::ITALICS;
}
b"4" => {
/* underlined */
*attrs |= Attr::UNDERLINE;
}
b"1" => { /* bold */ }
b"7" | b"27" => {
/* Inverse on/off */
std::mem::swap(&mut (*fg_color), &mut (*bg_color))
b"5" => {
/* blink */
*attrs |= Attr::BLINK;
}
b"7" => {
/* Inverse */
*attrs |= Attr::REVERSE;
}
b"8" => {
/* invisible */
*attrs |= Attr::HIDDEN;
}
b"9" => { /* crossed out */ }
b"21" => { /* Doubly-underlined */ }
b"22" => {
/* Normal (neither bold nor faint), ECMA-48 3rd. */
*attrs &= !(Attr::BOLD | Attr::DIM);
}
b"23" => {
/* Not italicized, ECMA-48 3rd */
*attrs &= !Attr::ITALICS;
}
b"24" => {
/* Not underlined, ECMA-48 3rd. */
*attrs &= !Attr::UNDERLINE;
}
b"25" => {
/* Steady (not blinking), ECMA-48 3rd. */
*attrs &= !Attr::BLINK;
}
b"27" => {
/* Positive (not inverse), ECMA-48 3rd. */
*attrs &= !Attr::REVERSE;
}
b"28" => {
/* Visible, i.e., not hidden, ECMA-48 3rd, VT300. */
*attrs &= !Attr::HIDDEN;
}
b"29" => { /* Not crossed-out, ECMA-48 3rd. */ }
b"30" => *fg_color = Color::Black,
b"31" => *fg_color = Color::Red,
b"32" => *fg_color = Color::Green,
@ -1047,8 +1229,8 @@ impl EmbedGrid {
b"35" => *fg_color = Color::Magenta,
b"36" => *fg_color = Color::Cyan,
b"37" => *fg_color = Color::White,
b"39" => *fg_color = Color::Default,
b"40" => *bg_color = Color::Black,
b"41" => *bg_color = Color::Red,
b"42" => *bg_color = Color::Green,
@ -1057,7 +1239,6 @@ impl EmbedGrid {
b"45" => *bg_color = Color::Magenta,
b"46" => *bg_color = Color::Cyan,
b"47" => *bg_color = Color::White,
b"49" => *bg_color = Color::Default,
b"90" => *fg_color = Color::Black,
@ -1077,11 +1258,17 @@ impl EmbedGrid {
b"105" => *bg_color = Color::Magenta,
b"106" => *bg_color = Color::Cyan,
b"107" => *bg_color = Color::White,
_ => {}
_ => {
debug!(
"unknown attribute Csi1 {:?} m",
String::from_utf8_lossy(buf1.as_slice())
);
}
}
}
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
grid[cursor_val!()].set_attrs(*attrs);
*state = State::Normal;
}
(c, State::Csi1(ref mut buf)) if (b'0'..=b'9').contains(&c) || c == b' ' => {
@ -1200,32 +1387,245 @@ impl EmbedGrid {
grid[cursor_val!()].set_bg(*bg_color);
*state = State::Normal;
}
(c, State::Csi3(_, _, ref mut buf)) if (b'0'..=b'9').contains(&c) => {
buf.push(c);
}
(b';', State::Csi3(ref mut buf1_p, ref mut buf2_p, ref mut buf3_p)) => {
let buf1 = std::mem::replace(buf1_p, SmallVec::new());
let buf2 = std::mem::replace(buf2_p, SmallVec::new());
let buf3 = std::mem::replace(buf3_p, SmallVec::new());
let buf4 = SmallVec::new();
*state = State::Csi4(buf1, buf2, buf3, buf4);
}
(c, State::Csi4(_, _, _, ref mut buf)) if (b'0'..=b'9').contains(&c) => {
buf.push(c);
}
(b';', State::Csi4(ref mut buf1_p, ref mut buf2_p, ref mut buf3_p, ref mut buf4_p)) => {
let buf1 = std::mem::replace(buf1_p, SmallVec::new());
let buf2 = std::mem::replace(buf2_p, SmallVec::new());
let buf3 = std::mem::replace(buf3_p, SmallVec::new());
let buf4 = std::mem::replace(buf4_p, SmallVec::new());
let buf5 = SmallVec::new();
*state = State::Csi5(buf1, buf2, buf3, buf4, buf5);
}
(c, State::Csi5(_, _, _, _, ref mut buf)) if (b'0'..=b'9').contains(&c) => {
buf.push(c);
}
(
b';',
State::Csi5(
ref mut buf1_p,
ref mut buf2_p,
ref mut buf3_p,
ref mut buf4_p,
ref mut buf5_p,
),
) => {
let buf1 = std::mem::replace(buf1_p, SmallVec::new());
let buf2 = std::mem::replace(buf2_p, SmallVec::new());
let buf3 = std::mem::replace(buf3_p, SmallVec::new());
let buf4 = std::mem::replace(buf4_p, SmallVec::new());
let buf5 = std::mem::replace(buf5_p, SmallVec::new());
let buf6 = SmallVec::new();
*state = State::Csi6(buf1, buf2, buf3, buf4, buf5, buf6);
}
(c, State::Csi6(_, _, _, _, _, ref mut buf)) if (b'0'..=b'9').contains(&c) => {
buf.push(c);
}
(
b'm',
State::Csi6(
ref mut buf1,
ref mut buf2,
ref mut _color_space_buf,
ref mut r_buf,
ref mut g_buf,
ref mut b_buf,
),
) if buf1.as_ref() == b"38" && buf2.as_ref() == b"2" => {
/* Set true foreground color */
*fg_color = match (
unsafe { std::str::from_utf8_unchecked(r_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(g_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(b_buf) }.parse::<u8>(),
) {
(Ok(r), Ok(g), Ok(b)) => Color::Rgb(r, g, b),
_ => Color::Default,
};
grid[cursor_val!()].set_fg(*fg_color);
*state = State::Normal;
}
(
b'm',
State::Csi6(
ref mut buf1,
ref mut buf2,
ref mut _color_space_buf,
ref mut r_buf,
ref mut g_buf,
ref mut b_buf,
),
) if buf1.as_ref() == b"48" && buf2.as_ref() == b"2" => {
/* Set true background color */
*bg_color = match (
unsafe { std::str::from_utf8_unchecked(r_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(g_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(b_buf) }.parse::<u8>(),
) {
(Ok(r), Ok(g), Ok(b)) => Color::Rgb(r, g, b),
_ => Color::Default,
};
grid[cursor_val!()].set_bg(*bg_color);
*state = State::Normal;
}
(
b'm',
State::Csi5(
ref mut buf1,
ref mut buf2,
ref mut r_buf,
ref mut g_buf,
ref mut b_buf,
),
) if buf1.as_ref() == b"38" && buf2.as_ref() == b"2" => {
/* Set true foreground color */
*fg_color = match (
unsafe { std::str::from_utf8_unchecked(r_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(g_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(b_buf) }.parse::<u8>(),
) {
(Ok(r), Ok(g), Ok(b)) => Color::Rgb(r, g, b),
_ => Color::Default,
};
grid[cursor_val!()].set_fg(*fg_color);
*state = State::Normal;
}
(
b'm',
State::Csi5(
ref mut buf1,
ref mut buf2,
ref mut r_buf,
ref mut g_buf,
ref mut b_buf,
),
) if buf1.as_ref() == b"48" && buf2.as_ref() == b"2" => {
/* Set true background color */
*bg_color = match (
unsafe { std::str::from_utf8_unchecked(r_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(g_buf) }.parse::<u8>(),
unsafe { std::str::from_utf8_unchecked(b_buf) }.parse::<u8>(),
) {
(Ok(r), Ok(g), Ok(b)) => Color::Rgb(r, g, b),
_ => Color::Default,
};
grid[cursor_val!()].set_bg(*bg_color);
*state = State::Normal;
}
(b'q', State::Csi1(buf))
if buf.len() == 2 && buf[1] == b' ' && (b'0'..=b'6').contains(&buf[0]) =>
{
/*
CSI Ps SP q
Set cursor style (DECSCUSR), VT520.
Ps = 0 โ‡’ blinking block.
Ps = 1 โ‡’ blinking block (default).
Ps = 2 โ‡’ steady block.
Ps = 3 โ‡’ blinking underline.
Ps = 4 โ‡’ steady underline.
Ps = 5 โ‡’ blinking bar, xterm.
Ps = 6 โ‡’ steady bar, xterm.
*/
*state = State::Normal;
}
(_, State::Csi) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Csi1(_)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Csi2(_, _)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Csi3(_, _, _)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Csi4(_, _, _, _)) => {
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Csi5(_, _, _, _, _)) => {
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Csi6(_, _, _, _, _, _)) => {
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Osc1(_)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::Osc2(_, _)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
(_, State::CsiQ(_)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
debug!(
"state: {:?} ignoring unknown code {} byte {}",
&state,
EscCode::from((&(*state), byte)),
byte
);
*state = State::Normal;
}
/* other stuff */

33
src/terminal/position.rs

@ -65,35 +65,43 @@ pub type Area = (Pos, Pos);
/// Get an area's height
///
/// Example:
/// ```
/// ```no_run
/// use meli::height;
///
/// let new_area = ((0, 0), (1, 1));
/// assert_eq!(height!(new_area), 1);
/// ```
#[macro_export]
macro_rules! height {
($a:expr) => {
(get_y(bottom_right!($a))).saturating_sub(get_y(upper_left!($a)))
($crate::get_y($crate::bottom_right!($a)))
.saturating_sub($crate::get_y($crate::upper_left!($a)))
};
}
/// Get an area's width
///
/// Example:
/// ```
/// ```no_run
/// use meli::width;
///
/// let new_area = ((0, 0), (1, 1));
/// assert_eq!(width!(new_area), 1);
/// ```
#[macro_export]
macro_rules! width {
($a:expr) => {
(get_x(bottom_right!($a))).saturating_sub(get_x(upper_left!($a)))
($crate::get_x($crate::bottom_right!($a)))
.saturating_sub($crate::get_x($crate::upper_left!($a)))
};
}
/// Get the upper left Position of an area
///
/// Example:
/// ```
/// ```no_run
/// use meli::upper_left;
///
/// let new_area = ((0, 0), (1, 1));
/// assert_eq!(upper_left!(new_area), (0, 0));
/// ```
@ -107,7 +115,9 @@ macro_rules! upper_left {
/// Get the bottom right Position of an area
///
/// Example:
/// ```
/// ```no_run
/// use meli::bottom_right;
///
/// let new_area = ((0, 0), (1, 1));
/// assert_eq!(bottom_right!(new_area), (1, 1));
/// ```
@ -121,7 +131,9 @@ macro_rules! bottom_right {
/// Check if area is valid.
///
/// Example:
/// ```
/// ```no_run
/// use meli::is_valid_area;
///
/// let valid_area = ((0, 0), (1, 1));
/// assert!(is_valid_area!(valid_area));
///
@ -132,9 +144,10 @@ macro_rules! bottom_right {
#[macro_export]
macro_rules! is_valid_area {
($a:expr) => {{
let upper_left = upper_left!($a);
let bottom_right = bottom_right!($a);
!(get_y(upper_left) > get_y(bottom_right) || get_x(upper_left) > get_x(bottom_right))
let upper_left = $crate::upper_left!($a);
let bottom_right = $crate::bottom_right!($a);
!($crate::get_y(upper_left) > $crate::get_y(bottom_right)
|| $crate::get_x(upper_left) > $crate::get_x(bottom_right))
}};
}

14
tools/Cargo.toml

@ -21,11 +21,21 @@ path = "src/imapshell.rs"
name = "smtp_conn"
path = "src/smtp_conn.rs"
[[bin]]
name = "embed"
path = "src/embed.rs"
[dependencies]
melib = { path = "../melib", version = "*", features = ["debug-tracing", "unicode_algorithms"] }
meli = { path = "..", version = "*" }
crossbeam = { version = "^0.8" }
signal-hook = { version = "^0.3", default-features = false }
signal-hook-registry = { version = "1.2.0", default-features = false }
nix = { version = "^0.24", default-features = false }
[features]
default = []
default = ["debug-tracing"]
# Print tracing logs as meli runs
debug-tracing = []
debug-tracing = ["meli/debug-tracing"]

581
tools/src/embed.rs

@ -1,7 +1,582 @@
extern crate ui;
use ui::terminal::embed::create_pty;
use meli::terminal::embed::*;
use meli::terminal::*;
use meli::*;
use nix::sys::wait::WaitStatus;
use std::fs::File;
use std::io::prelude::*;
use std::os::raw::c_int;
use std::sync::{Arc, Mutex};
fn notify(
signals: &[c_int],
sender: crossbeam::channel::Sender<ThreadEvent>,
) -> std::result::Result<crossbeam::channel::Receiver<c_int>, std::io::Error> {
use std::time::Duration;
let (alarm_pipe_r, alarm_pipe_w) =
nix::unistd::pipe().map_err(|err| std::io::Error::from_raw_os_error(err as i32))?;
let alarm_handler = move |info: &nix::libc::siginfo_t| {
let value = unsafe { info.si_value().sival_ptr as u8 };
let _ = nix::unistd::write(alarm_pipe_w, &[value]);
};
unsafe {
signal_hook_registry::register_sigaction(signal_hook::consts::SIGALRM, alarm_handler)?;
}
let (s, r) = crossbeam::channel::bounded(100);
let mut signals = signal_hook::iterator::Signals::new(signals)?;
let _ = nix::fcntl::fcntl(
alarm_pipe_r,
nix::fcntl::FcntlArg::F_SETFL(nix::fcntl::OFlag::O_NONBLOCK),
);
std::thread::spawn(move || {
let mut ctr = 0;
loop {
ctr %= 3;
if ctr == 0 {
let _ = sender
.send_timeout(ThreadEvent::Pulse, Duration::from_millis(500))
.ok();
}
for signal in signals.pending() {
let _ = s.send_timeout(signal, Duration::from_millis(500)).ok();
}
std::thread::sleep(std::time::Duration::from_millis(100));
ctr += 1;
}
});
Ok(r)
}
#[derive(Debug)]
enum EmbedStatus {
Stopped(Arc<Mutex<EmbedTerminal>>),
Running(Arc<Mutex<EmbedTerminal>>),
}
impl EmbedStatus {
#[inline(always)]
fn is_stopped(&self) -> bool {
matches!(self, Self::Stopped(_))
}
}
impl std::ops::Deref for EmbedStatus {
type Target = Arc<Mutex<EmbedTerminal>>;
fn deref(&self) -> &Arc<Mutex<EmbedTerminal>> {
use EmbedStatus::*;
match self {
Stopped(ref e) | Running(ref e) => e,
}
}
}
impl std::ops::DerefMut for EmbedStatus {
fn deref_mut(&mut self) -> &mut Arc<Mutex<EmbedTerminal>> {
use EmbedStatus::*;
match self {
Stopped(ref mut e) | Running(ref mut e) => e,
}
}
}
#[derive(Debug)]
struct EmbedContainer {
command: String,
embed_area: Area,
embed: Option<EmbedStatus>,
id: ComponentId,
dirty: bool,
log_file: File,
}
impl EmbedContainer {
fn new(command: String) -> Box<dyn Component> {
Box::new(Self {
command,
embed: None,
embed_area: ((0, 0), (80, 20)),
dirty: true,
log_file: File::open(".embed.out").unwrap(),
id: ComponentId::new_v4(),
})
}
}
impl std::fmt::Display for EmbedContainer {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "embed")
}
}
impl Component for EmbedContainer {
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
if let Some(ref mut embed_pty) =