1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 06:54:45 +03:00

termwiz: make serde an optional dep

closes: https://github.com/wez/wezterm/pull/186
This commit is contained in:
Wez Furlong 2020-06-13 09:51:17 -07:00
parent 070afa65d6
commit 3002b40fd2
15 changed files with 94 additions and 44 deletions

1
Cargo.lock generated
View File

@ -3914,7 +3914,6 @@ dependencies = [
"palette",
"pretty_assertions",
"serde",
"serde_derive",
"termwiz",
"unicode-segmentation",
"unicode-width",

View File

@ -21,7 +21,6 @@ palette = "0.5"
unicode-segmentation = "1.5"
unicode-width = "0.1"
serde = {version="1.0", features = ["rc"]}
serde_derive = "1.0"
url = "2"
[dev-dependencies]
@ -30,3 +29,4 @@ pretty_assertions = "0.6"
[dependencies.termwiz]
version = "0.10"
path = "../termwiz"
features = ["use_serde"]

View File

@ -2,7 +2,7 @@
#![cfg_attr(feature = "cargo-clippy", allow(clippy::suspicious_arithmetic_impl, clippy::redundant_field_names))]
use super::VisibleRowIndex;
use serde_derive::*;
use serde::*;
use std::time::{Duration, Instant};
pub use termwiz::input::KeyCode;

View File

@ -15,9 +15,8 @@
//!
//! The entrypoint to the crate is the [Terminal](terminal/struct.Terminal.html)
//! struct.
use serde_derive::*;
use anyhow::Error;
use serde::*;
use std::ops::{Deref, DerefMut, Range};
use std::str;

View File

@ -2,7 +2,7 @@
// and inclusive range
#![cfg_attr(feature = "cargo-clippy", allow(clippy::range_plus_one))]
use super::{ScrollbackOrVisibleRowIndex, VisibleRowIndex};
use serde_derive::*;
use serde::*;
use std::ops::Range;
/// The x,y coordinates of either the start or end of a selection region

View File

@ -25,7 +25,7 @@ num-traits = "0.2"
ordered-float = "1.0"
regex = "1"
semver = "0.9"
serde = {version="1.0", features = ["rc", "derive"]}
serde = {version="1.0", features = ["rc", "derive"], optional=true}
smallvec = "0.6"
terminfo = "0.7"
unicode-segmentation = "1.5"
@ -35,6 +35,7 @@ vtparse = { version="0.3", path="../vtparse" }
[features]
widgets = ["cassowary", "fnv"]
use_serde = ["serde"]
[dev-dependencies]
varbincode = "0.1"

View File

@ -2,6 +2,7 @@
use crate::color::ColorAttribute;
pub use crate::escape::osc::Hyperlink;
use crate::image::ImageCell;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use smallvec::SmallVec;
use std;
@ -14,7 +15,8 @@ use unicode_width::UnicodeWidthStr;
/// to reduce per-cell overhead.
/// The setter methods return a mutable self reference so that they can
/// be chained together.
#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct CellAttributes {
attributes: u16,
/// The foreground color
@ -85,7 +87,8 @@ macro_rules! bitfield {
/// implement `Intensity::Bold` by either using a bold font or by simply
/// using an alternative color. Some terminals implement `Intensity::Half`
/// as a dimmer color variant.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum Intensity {
Normal = 0,
@ -100,7 +103,8 @@ impl Default for Intensity {
}
/// Specify just how underlined you want your `Cell` to be
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum Underline {
/// The cell is not underlined
@ -127,7 +131,8 @@ impl Into<bool> for Underline {
}
/// Specify whether you want to slowly or rapidly annoy your users
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum Blink {
None = 0,
@ -195,6 +200,7 @@ impl CellAttributes {
}
}
#[cfg(feature = "use_serde")]
fn deserialize_smallvec<'de, D>(deserializer: D) -> Result<SmallVec<[u8; 4]>, D::Error>
where
D: Deserializer<'de>,
@ -203,6 +209,7 @@ where
Ok(SmallVec::from_slice(text.as_bytes()))
}
#[cfg(feature = "use_serde")]
fn serialize_smallvec<S>(value: &SmallVec<[u8; 4]>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -214,11 +221,15 @@ where
}
/// Models the contents of a cell on the terminal display
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Cell {
#[serde(
#[cfg_attr(
feature = "use_serde",
serde(
deserialize_with = "deserialize_smallvec",
serialize_with = "serialize_smallvec"
)
)]
text: SmallVec<[u8; 4]>,
attrs: CellAttributes,
@ -340,7 +351,8 @@ pub fn grapheme_column_width(s: &str) -> usize {
/// Models a change in the attributes of a cell in a stream of changes.
/// Each variant specifies one of the possible attributes; the corresponding
/// value holds the new value to be used for that attribute.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum AttributeChange {
Intensity(Intensity),
Underline(Underline),

View File

@ -3,9 +3,9 @@
#![cfg_attr(feature = "cargo-clippy", allow(clippy::useless_attribute))]
use num_derive::*;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::result::Result;
#[derive(Debug, Clone, Copy, FromPrimitive)]
#[repr(u8)]
@ -212,6 +212,7 @@ impl RgbColor {
/// is that we have to serialize RgbColor as a 7-byte string when we could
/// otherwise serialize it as a 3-byte array. There's probably a way
/// to make this work more efficiently, but for now this will do.
#[cfg(feature = "use_serde")]
impl Serialize for RgbColor {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -222,6 +223,7 @@ impl Serialize for RgbColor {
}
}
#[cfg(feature = "use_serde")]
impl<'de> Deserialize<'de> for RgbColor {
fn deserialize<D>(deserializer: D) -> Result<RgbColor, D::Error>
where
@ -271,7 +273,8 @@ impl From<RgbColor> for ColorSpec {
/// type used in the `CellAttributes` struct and can specify an optional
/// TrueColor value, allowing a fallback to a more traditional palette
/// index if TrueColor is not available.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ColorAttribute {
/// Use RgbColor when supported, falling back to the specified PaletteIndex.
TrueColorWithPaletteFallback(RgbColor, PaletteIndex),

View File

@ -6,13 +6,15 @@
//! as we recognize linkable input text during print() processing.
use anyhow::{anyhow, ensure, Error};
use regex::{Captures, Regex};
use serde::{self, Deserialize, Deserializer, Serialize};
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;
use std::fmt::{Display, Error as FmtError, Formatter};
use std::ops::Range;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hyperlink {
params: HashMap<String, String>,
uri: String,
@ -121,12 +123,13 @@ impl Display for Hyperlink {
/// URL to view the details for that issue.
/// The Rule struct is configuration that is passed to the terminal
/// and is evaluated when processing mouse hover events.
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Deserialize))]
#[derive(Debug, Clone)]
pub struct Rule {
/// The compiled regex for the rule. This is used to match
/// against a line of text from the screen (typically the line
/// over which the mouse is hovering).
#[serde(deserialize_with = "deserialize_regex")]
#[cfg_attr(feature = "use_serde", serde(deserialize_with = "deserialize_regex"))]
regex: Regex,
/// The format string that defines how to transform the matched
/// text into a URL. For example, a format string of `$0` expands
@ -141,6 +144,7 @@ pub struct Rule {
format: String,
}
#[cfg(feature = "use_serde")]
fn deserialize_regex<'de, D>(deserializer: D) -> Result<Regex, D::Error>
where
D: Deserializer<'de>,

View File

@ -12,9 +12,11 @@
// z-order.
use ordered_float::NotNan;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::sync::Arc;
#[cfg(feature = "use_serde")]
fn deserialize_notnan<'de, D>(deserializer: D) -> Result<NotNan<f32>, D::Error>
where
D: Deserializer<'de>,
@ -23,6 +25,7 @@ where
NotNan::new(value).map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
}
#[cfg(feature = "use_serde")]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::trivially_copy_pass_by_ref))]
fn serialize_notnan<S>(value: &NotNan<f32>, serializer: S) -> Result<S::Ok, S::Error>
where
@ -31,16 +34,23 @@ where
value.into_inner().serialize(serializer)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextureCoordinate {
#[serde(
#[cfg_attr(
feature = "use_serde",
serde(
deserialize_with = "deserialize_notnan",
serialize_with = "serialize_notnan"
)
)]
pub x: NotNan<f32>,
#[serde(
#[cfg_attr(
feature = "use_serde",
serde(
deserialize_with = "deserialize_notnan",
serialize_with = "serialize_notnan"
)
)]
pub y: NotNan<f32>,
}
@ -62,7 +72,8 @@ impl TextureCoordinate {
/// carve up the image and track each slice of it. Each cell needs to know
/// its "texture coordinates" within that image so that we can render the
/// right slice.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImageCell {
/// Texture coordinate for the top left of this cell.
/// (0,0) is the top left of the ImageData. (1, 1) is
@ -102,7 +113,8 @@ impl ImageCell {
static IMAGE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageData {
id: usize,
/// The image data bytes. Data is the native image file format

View File

@ -6,6 +6,7 @@ use crate::escape::{Action, CSI};
use crate::keymap::{Found, KeyMap};
use crate::readbuf::ReadBuffer;
use bitflags::bitflags;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};
use std;
@ -16,7 +17,8 @@ use winapi::um::wincon::{
};
bitflags! {
#[derive(Default, Serialize, Deserialize)]
#[cfg_attr(feature="use_serde", derive(Serialize, Deserialize))]
#[derive(Default)]
pub struct Modifiers: u8 {
const NONE = 0;
const SHIFT = 1<<1;
@ -26,7 +28,8 @@ bitflags! {
}
}
bitflags! {
#[derive(Default, Serialize, Deserialize)]
#[cfg_attr(feature="use_serde", derive(Serialize, Deserialize))]
#[derive(Default)]
pub struct MouseButtons: u8 {
const NONE = 0;
const LEFT = 1<<1;
@ -56,7 +59,8 @@ pub enum InputEvent {
Wake,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MouseEvent {
pub x: u16,
pub y: u16,
@ -64,7 +68,8 @@ pub struct MouseEvent {
pub modifiers: Modifiers,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyEvent {
/// Which key was pressed
pub key: KeyCode,
@ -76,7 +81,8 @@ pub struct KeyEvent {
/// Which key is pressed. Not all of these are probable to appear
/// on most systems. A lot of this list is @wez trawling docs and
/// making an entry for things that might be possible in this first pass.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum KeyCode {
/// The decoded unicode character
Char(char),

View File

@ -34,6 +34,11 @@
//! level.
//! * `LineEditor` provides line editing facilities similar to those
//! in the unix shell.
//!
//! ## Features
//!
//! * `widgets` - enables the widget layout and related traits
//! * `use_serde` - makes a number of structs serde serializable
pub mod caps;
pub mod cell;

View File

@ -2,6 +2,7 @@ use crate::cell::{unicode_column_width, AttributeChange, CellAttributes};
use crate::color::ColorAttribute;
pub use crate::image::{ImageData, TextureCoordinate};
use crate::surface::{CursorShape, CursorVisibility, Position};
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use unicode_segmentation::UnicodeSegmentation;
@ -9,7 +10,8 @@ use unicode_segmentation::UnicodeSegmentation;
/// `Change` describes an update operation to be applied to a `Surface`.
/// Changes to the active attributes (color, style), moving the cursor
/// and outputting text are examples of some of the values.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Change {
/// Change a single attribute
Attribute(AttributeChange),
@ -261,7 +263,8 @@ impl ChangeSequence {
/// A 4x3 cell image would set `width=3`, `height=3`, `top_left=(0,0)`, `bottom_right=(1,1)`.
/// The top left cell from that image, if it were to be included in a diff,
/// would be recorded as `width=1`, `height=1`, `top_left=(0,0)`, `bottom_right=(1/4,1/3)`.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Image {
/// measured in cells
pub width: usize,

View File

@ -3,13 +3,14 @@ use crate::cellcluster::CellCluster;
use crate::hyperlink::Rule;
use crate::surface::Change;
use bitflags::bitflags;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};
use std::ops::Range;
use std::sync::Arc;
use unicode_segmentation::UnicodeSegmentation;
bitflags! {
#[derive(Serialize, Deserialize)]
#[cfg_attr(feature="use_serde", derive(Serialize, Deserialize))]
struct LineBits : u8 {
const NONE = 0;
/// The contents of the Line have changed and cached or
@ -24,7 +25,8 @@ bitflags! {
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Line {
bits: LineBits,
cells: Vec<Cell>,

View File

@ -2,6 +2,7 @@ use crate::cell::{AttributeChange, Cell, CellAttributes};
use crate::color::ColorAttribute;
use crate::image::ImageCell;
use ordered_float::NotNan;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cmp::min;
@ -18,7 +19,8 @@ pub use self::line::Line;
/// Relative(0) is the current position in the line or
/// column and EndRelative(0) is the end position in the
/// line or column.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Position {
/// Negative values move up, positive values down, 0 means no change
Relative(isize),
@ -28,7 +30,8 @@ pub enum Position {
EndRelative(usize),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorVisibility {
Hidden,
Visible,
@ -40,7 +43,8 @@ impl Default for CursorVisibility {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorShape {
Default,
BlinkingBlock,