rustfmt, trucate text width

This commit is contained in:
Annika Hannig 2022-08-27 11:54:53 +00:00
parent fd9e5411e2
commit b219dbefa7
No known key found for this signature in database
GPG key ID: 62E226E47DDCE58D
5 changed files with 30 additions and 35 deletions

View file

@ -13,7 +13,7 @@ impl Display {
/// Open a new UDP socket and create a display instance
pub fn open(addr: String) -> Result<Self> {
let socket = UdpSocket::bind("0.0.0.0:0")?;
Ok(Self{
Ok(Self {
addr: addr,
socket: socket,
})
@ -23,10 +23,9 @@ impl Display {
pub fn send(&self, cmd: Command) -> Result<()> {
let data: Data = cmd.into();
for frame in data {
self.socket.send_to(
frame.as_slice(), self.addr.clone().as_str())?;
self.socket
.send_to(frame.as_slice(), self.addr.clone().as_str())?;
}
Ok(())
}
}

View file

@ -1,4 +1,3 @@
/// An origin marks the top left position of the
/// data sent to the display.
#[derive(Default)]
@ -15,4 +14,3 @@ impl Window {
Window(Origin(x, y), Size(w, h))
}
}

View file

@ -1,12 +1,12 @@
mod display;
mod protocol;
mod commands;
mod display;
mod geometry;
mod protocol;
mod text;
pub const TEXT_COLUMNS: usize = 56;
pub const TEXT_ROWS: usize = 20;
pub use commands::{Command};
pub use protocol::Data;
pub use commands::Command;
pub use display::Display;
pub use protocol::Data;

View file

@ -1,12 +1,11 @@
use std::convert::From;
use codepage_437::{CP437_WINGDINGS, ToCp437};
use codepage_437::{ToCp437, CP437_WINGDINGS};
use super::{
commands::{Command},
text,
geometry::{Window, Origin, Size},
TEXT_COLUMNS, TEXT_ROWS,
commands::Command,
geometry::{Origin, Size, Window},
text, TEXT_COLUMNS, TEXT_ROWS,
};
const CMD_RAW_TEXT: &'static [u8] = &[0x00, 0x03];
@ -21,20 +20,14 @@ pub type Data = Vec<Frame>;
/// Encode position data as big endian
impl From<Origin> for Frame {
fn from(Origin(x, y): Origin) -> Self {
vec![
(x >> 8) as u8, x as u8,
(y >> 8) as u8, y as u8,
]
vec![(x >> 8) as u8, x as u8, (y >> 8) as u8, y as u8]
}
}
/// Encode size as big endian
impl From<Size> for Frame {
fn from(Size(w, h): Size) -> Self {
vec![
(w >> 8) as u8, w as u8,
(h >> 8) as u8, h as u8,
]
vec![(w >> 8) as u8, w as u8, (h >> 8) as u8, h as u8]
}
}
@ -63,22 +56,30 @@ impl From<text::Buffer> for Data {
let mut data = vec![];
for (i, line) in lines.iter().enumerate() {
// Convert utf8 to codepage 437
if let Ok(bytes) = line.to_cp437(&CP437_WINGDINGS) {
let mut bytes: Frame = bytes.into();
bytes.truncate(TEXT_COLUMNS);
let len = bytes.len() as u16;
let pos = Origin(x, y + i as u16);
let size = Size(len, 1);
data.push([
Frame::from(CMD_RAW_TEXT),
pos.into(),
size.into(),
bytes.into(),
].concat());
data.push(
[
Frame::from(CMD_RAW_TEXT),
pos.into(),
size.into(),
bytes.into(),
]
.concat(),
);
}
}
data
}
}
/// Encode text command
impl From<text::Text> for Data {
fn from(text: text::Text) -> Data {
match text {

View file

@ -1,9 +1,7 @@
use std::convert::From;
use super::geometry::Origin;
/// TextRaw holds bytes and a window
pub struct Raw(pub Origin, pub Vec<u8>);
@ -14,7 +12,6 @@ impl From<Vec<u8>> for Raw {
}
}
/// TextBuffer holds a multiline block of utf8 text
/// data and a origin.
pub struct Buffer(pub Origin, pub String);