Skip to content

Commit

Permalink
tick_beam
Browse files Browse the repository at this point in the history
  • Loading branch information
Canop committed Oct 26, 2024
1 parent e9782e7 commit a45f387
Show file tree
Hide file tree
Showing 48 changed files with 571 additions and 421 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
*If you're reading this because you try make sense of some new API or a breaking change, you might also be interested in coming to the chat for explanations or guidance.*

<a name="v0.31.0"></a>
### v0.31.0 - 2024-10-26
- reexport crossbeam
- new Ticker tool: emit tick(s) on a channel

<a name="v0.30.1"></a>
### v0.30.1 - 2024-10-10
- input_field#display returns the curstor position if it's rendered - experimental - Thanks @xubaiwang
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "termimad"
version = "0.30.1"
version = "0.31.0"
authors = ["dystroy <denys.seguret@gmail.com>"]
repository = "https://github.com/Canop/termimad"
description = "Markdown Renderer for the Terminal"
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@
[s4]: https://miaou.dystroy.org/static/shields/room.svg
[l4]: https://miaou.dystroy.org/3

A CLI utilities library leveraging Markdown to format terminal rendering, allowing separation of structure, data and skin.
## Introduction

Based on [crossterm](#crossterm-compatibility) so works on most terminals (even on windows).
Termimad is a set of cross-platform utilities dedicated to CLI and TUI apps,
- leveraging Markdown to format terminal rendering, allowing separation of structure, data and skin,
- using [crossterm](https://github.com/crossterm-rs/crossterm) as backend for styling and event,
- based on [crokey](https://docs.rs/crokey/latest/crokey/) for key combinations support
- with a focus on total control and performances, with no active loop,
- using [crossbeam](https://docs.rs/crossbeam/latest/crossbeam/) for event passing,
- striving to be correct, even on Unicode and wide characters

Termimad is **not**
- a TUI framework: Unless you have serious performance concerns or want precise control, you'll find it much easier and faster, when building a TUI application, to just use one of the TUI frameworks of the Rust ecosystem
- a generic Markdown renderer
- consistent or complete in any way

## Markdown in Termimad

![text](doc/text.png)

The goal isn't to display any markdown text with its various extensions (a terminal isn't really fit for that).
The goal is rather to improve the display of texts in a terminal application when we want both the text and the skin to be easily configured.

Termimad also includes a few utilities helping efficient managing of events and user input in a multithread application.

**Wrapping**, table balancing, and **scrolling** are essential features of Termimad.

A text or a table can be displayed in an *a priori* unknown part of the screen, scrollable if desired, with a dynamically discovered width.
Expand Down Expand Up @@ -286,6 +297,10 @@ scrollbar: "#fb0 gray(11) |"

Execute `cargo run --example skin-file` for an example and explanations.

## Events and inputs

Termimad also includes a few utilities helping efficient managing of events and user input in a multithread application.

## Advices to get started

* Start by reading the examples (in `/examples`): they cover almost the whole API, including templates, how to use an alternate screen or scroll the page, etc. Many examples print a bunch of relevant documentation.
Expand Down
1 change: 1 addition & 0 deletions bacon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ command = [
"--",
"-A", "clippy::match_like_matches_macro",
"-A", "clippy::manual_range_contains",
"-A", "clippy::new_without_default",
]
need_stdout = false
watch = ["tests", "benches", "examples"]
Expand Down
13 changes: 7 additions & 6 deletions src/area.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use {
crate::crossterm::terminal,
std::convert::{TryFrom, TryInto},
std::convert::{
TryFrom,
TryInto,
},
};

/// A default width which is used when we failed measuring the real terminal width
Expand Down Expand Up @@ -67,10 +70,7 @@ impl Area {

/// tell whether the char at (x,y) is in the area
pub const fn contains(&self, x: u16, y: u16) -> bool {
x >= self.left
&& x < self.left + self.width
&& y >= self.top
&& y < self.top + self.height
x >= self.left && x < self.left + self.width && y >= self.top && y < self.top + self.height
}

/// shrink the area
Expand Down Expand Up @@ -101,7 +101,8 @@ impl Area {
scroll: U, // number of lines hidden on top
content_height: U,
) -> Option<(u16, u16)>
where U: Into<usize>
where
U: Into<usize>,
{
compute_scrollbar(scroll, content_height, self.height, self.top)
}
Expand Down
5 changes: 1 addition & 4 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use {
crate::crossterm::style::Color,
};
use crate::crossterm::style::Color;

/// Build a RGB color
///
Expand All @@ -21,4 +19,3 @@ pub fn gray(mut level: u8) -> Color {
pub const fn ansi(level: u8) -> Color {
Color::AnsiValue(level)
}

7 changes: 4 additions & 3 deletions src/composite.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use {
crate::*,
minimad::{Composite, Compound},
minimad::{
Composite,
Compound,
},
unicode_width::UnicodeWidthStr,
};

/// Wrap Minimad compounds with their style and
/// termimad specific information
#[derive(Debug, Clone)]
pub struct FmtComposite<'s> {

pub kind: CompositeKind,

pub compounds: Vec<Compound<'s>>,
Expand All @@ -17,7 +19,6 @@ pub struct FmtComposite<'s> {
pub visible_length: usize,

pub spacing: Option<Spacing>,

}

impl<'s> FmtComposite<'s> {
Expand Down
1 change: 0 additions & 1 deletion src/composite_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub enum CompositeKind {
Quote,
}


impl From<CompositeStyle> for CompositeKind {
fn from(ty: CompositeStyle) -> Self {
match ty {
Expand Down
42 changes: 15 additions & 27 deletions src/compound_style.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use {
crate::{
errors::Result,
crossterm::{
QueueableCommand,
style::{
Attribute,
Attributes,
Expand All @@ -13,11 +11,19 @@ use {
SetForegroundColor,
StyledContent,
},
terminal::{Clear, ClearType},
terminal::{
Clear,
ClearType,
},
QueueableCommand,
},
errors::Result,
styled_char::StyledChar,
},
std::fmt::{self, Display},
std::fmt::{
self,
Display,
},
};

/// The attributes which are often supported
Expand All @@ -32,7 +38,6 @@ pub static ATTRIBUTES: &[Attribute] = &[
Attribute::OverLined,
];


/// A style which may be applied to a compound
#[derive(Default, Clone, Debug, PartialEq)]
pub struct CompoundStyle {
Expand Down Expand Up @@ -75,7 +80,7 @@ impl CompoundStyle {
///
/// The `dest` color can be for example a [crossterm] color or a [coolor] one.
pub fn blend_with<C: Into<coolor::Color>>(&mut self, dest: C, weight: f32) {
debug_assert!(weight>=0.0 && weight<=1.0);
debug_assert!(weight >= 0.0 && weight <= 1.0);
let dest: coolor::Color = dest.into();
if let Some(fg) = self.object_style.foreground_color.as_mut() {
let src: coolor::Color = (*fg).into();
Expand All @@ -89,29 +94,17 @@ impl CompoundStyle {

/// Get an new instance of `CompoundStyle`
pub fn with_fgbg(fg: Color, bg: Color) -> Self {
Self::new(
Some(fg),
Some(bg),
Attributes::default(),
)
Self::new(Some(fg), Some(bg), Attributes::default())
}

/// Get an new instance of `CompoundStyle`
pub fn with_fg(fg: Color) -> Self {
Self::new(
Some(fg),
None,
Attributes::default(),
)
Self::new(Some(fg), None, Attributes::default())
}

/// Get an new instance of `CompoundStyle`
pub fn with_bg(bg: Color) -> Self {
Self::new(
None,
Some(bg),
Attributes::default(),
)
Self::new(None, Some(bg), Attributes::default())
}

/// Get an new instance of `CompoundStyle`
Expand Down Expand Up @@ -180,12 +173,7 @@ impl CompoundStyle {

/// Write a char several times with the line compound style
#[inline(always)]
pub fn repeat_char(
&self,
f: &mut fmt::Formatter<'_>,
c: char,
count: usize,
) -> fmt::Result {
pub fn repeat_char(&self, f: &mut fmt::Formatter<'_>, c: char, count: usize) -> fmt::Result {
if count > 0 {
let s = std::iter::repeat(c).take(count).collect::<String>();
write!(f, "{}", self.apply_to(s))?;
Expand Down
5 changes: 1 addition & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::{
fit::InsufficientWidthError,
};
use crate::fit::InsufficientWidthError;

/// Termimad error type
#[derive(thiserror::Error, Debug)]
pub enum Error {

#[error("IO error: {0}")]
IO(#[from] std::io::Error),

Expand Down
17 changes: 10 additions & 7 deletions src/events/escape_sequence.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use {
crate::crossterm::{
event::{
KeyCode,
KeyEvent,
KeyModifiers,
},
crate::crossterm::event::{
KeyCode,
KeyEvent,
KeyModifiers,
},
std::fmt,
};
Expand All @@ -26,7 +24,12 @@ pub struct EscapeSequence {
impl fmt::Display for EscapeSequence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for key in &self.keys {
if let KeyEvent { code: KeyCode::Char(c), modifiers: KeyModifiers::NONE, .. } = key {
if let KeyEvent {
code: KeyCode::Char(c),
modifiers: KeyModifiers::NONE,
..
} = key
{
write!(f, "{}", c)?;
}
}
Expand Down
Loading

0 comments on commit a45f387

Please sign in to comment.