Expand description
This crate lets you display simple markdown snippets or scrollable wrapped markdown texts in the terminal.
In order to use Termimad you typically need
- some markdown: a string which you can have loaded or dynamically built
- a skin: which defines the colors and style attributes of every parts
Additionnaly, you might define an area of the screen in which to draw (and maybe scroll).
§The skin
It’s an instance of MadSkin
whose fields you customize according
to your tastes or (better) to your application’s configuration.
use termimad::crossterm::style::{Color::*, Attribute::*};
use termimad::*;
// start with the default skin
let mut skin = MadSkin::default();
// let's decide bold is in light gray
skin.bold.set_fg(gray(20));
// let's make strikeout not striked out but red, with no specific background, and bold
skin.strikeout = CompoundStyle::new(Some(Red), None, Bold.into());
Beware:
- you may define colors in full
rgb
but this will limit compatibility with old terminals. It’s recommended to stick to Ansi colors, gray levels, or Crossterm predefined values. - styles are composed. For example a word may very well be italic, bold and striked out. It might not be wise to have them differ only by their background color for example.
§Display a simple inline snippet
// with the default skin, nothing simpler:
termimad::print_inline("value: **52**");
§Print a text
A multi-line markdown string can be printed the same way than an inline snippet, but you usually want it to be wrapped according to the available terminal width.
eprintln!("{}", skin.term_text(my_markdown));
MadSkin
contains other functions to prepare a text for no specific size or for one which isn’t the terminal’s width. It also offers several functions to print it either on stdout
or on a given Write
.
§Display a text, maybe scroll it
A terminal application often uses an alternate screen instead of just dumping its text to stdout, and you often want to display in a specific rect of that screen, with adequate wrapping and not writing outside that rect.
You may also want to display a scrollbar if the text doesn’t fit the area. A MadView
makes that simple:
let area = Area::new(0, 0, 10, 12);
let mut view = MadView::from(markdown, area, skin);
view.write().unwrap();
If you don’t want to give ownership of the skin, markdown and area, you may prefer to use a TextView
.
You may see how to write a text viewer responding to key inputs to scroll a markdown text in the scrollable example.
§Templates
In order to separate the rendering format from the content, the format!
macro is not always a good solution because you may not be sure the content is free of characters which may mess the markdown.
A solution is to use one of the templating functions or macros.
Example:
mad_print_inline!(
&skin,
"**$0 formula:** *$1*", // the markdown template, interpreted once
"Disk", // fills $0
"2*π*r", // fills $1. Note that the stars don't mess the markdown
);
Main difference with using print!(format!( ... ))
:
- the markdown parsing and template building are done only once (using
once_cell
internally) - the given values aren’t interpreted as markdown fragments and don’t impact the style
- arguments can be omited, repeated, given in any order
- no support for fmt parameters or arguments other than
&str
(in the current version)
You’ll find more examples and advice in the templates example.
§Examples
The repository contains several other examples, which hopefully cover the whole API while being simple enough. It’s recommended you start by trying them or at least glance at their code.
Re-exports§
Modules§
Macros§
- ask
- ask the user to choose among proposed answers.
- mad_
print_ inline - print a markdown template, with other arguments taking
$0
to$9
places in the template. - mad_
write_ inline - write a markdown template, with other arguments taking
$0
to$9
places in the template.
Structs§
- Answer
- one of the proposed answers to a question
- Area
- A rectangular part of the screen
- Compound
Style - A style which may be applied to a compound
- Crop
Writer - wrap a writer to ensure that at most
allowed
columns are written. - Displayable
Line - A facility to write just a line of a text.
- Event
Source - a thread backed event listener emmiting events on a channel.
- Event
Source Options - Filling
- something to fill with
- Fitter
- A fitter can shorten a composite to make it fit a target width without wrapping (by removing parts and replacing them with ellipsis)
- FmtComposite
- Wrap Minimad compounds with their style and termimad specific information
- FmtInline
- A directly printable markdown snippet, complete with the reference to a skin so that it can implement the Display trait.
- FmtTable
Row - Wrap a standard table row
- FmtTable
Rule - A separator or alignment rule in a table.
- FmtText
- a formatted text, implementing Display.
- Input
Field - A simple input field, managing its cursor position and either handling the events you give it or being managed through direct manipulation functions (put_char, del_char_left, etc.).
- Insufficient
Width Error - Error thrown when fitting isn’t possible
- Line
Style - A style applicable to a type of line.
- List
View - A filterable list whose columns can be automatically resized.
- List
View Cell - List
View Column - MadSkin
- A skin defining how a parsed markdown appears on the terminal (fg and bg colors, bold, italic, underline, etc.)
- MadView
- A MadView is like a textview but it owns everything, from the source markdown to the area and the skin, which often makes it more convenient for dynamic texts. It’s also resizeable.
- Progress
Bar - A pixel precise horizontal bar
- Question
- a question that can be asked to the user, requiring him to type the key of the desired answer
- Rect
- A drawable rect, with various types of borders and an optional filling.
- Rect
Border Style - Scroll
BarStyle - A scrollbar style defined by two styled chars, one for the track, and one for the thumb.
- Spacing
- StrFit
- Information about the fitting of a string into a given width in cols.
- Styled
Char - A modifiable character which can be easily written or repeated. Can be used for bullets, horizontal rules or quote marks.
- Table
Border Chars - The set of characters to use to render table borders
- TblFit
- A fitter, accumulating data about the table which must fit into a given width, then computing the best column widths.
- TblFit
Result - Result of the fitting operation (always a success)
- Text
View - A scrollable text, in a specific area.
- Ticker
- A tick generator, sending a paylod of your choice (can be
()
for a simple timer) at regular inverval, once, several times or infinitely. - Timed
Event - A user event based on a crossterm event, decorated
Enums§
- Alignment
- Left, Center, Right or Unspecified
- Composite
Kind - The global kind of a composite
- Error
- Termimad error type
- FmtLine
- A line in a text. This structure should normally not be used outside of the lib.
- List
Items Indentation Mode - Describe how list items are indented when the item has to be wrapped: either only the first line (the one with the bullet) or the whole item as a block.
- Parse
Align Error - Parse
Attribute Error - Parse
Color Error - Parse
Style Token Error - Relative
Position - Top, Bottom, or other
- Style
Token - something which may be part of a style
Statics§
- ASCII_
TABLE_ BORDER_ CHARS - For tables made only of ASCII (not extended)
- ATTRIBUTES
- The attributes which are often supported
- BORDER_
STYLE_ BLAND - BORDER_
STYLE_ HALF_ WIDTH_ OUTSIDE - BORDER_
STYLE_ MIDDLE_ ROUND_ LINE - BORDER_
STYLE_ MIDDLE_ SQUARE_ LINE - DEFAULT_
TAB_ REPLACEMENT - ELLIPSIS
- ROUNDED_
TABLE_ BORDER_ CHARS - Allow tables to be more rounded
- SPACE_
FILLING - STANDARD_
TABLE_ BORDER_ CHARS - Default square tables
- TAB_
REPLACEMENT
Traits§
Functions§
- ansi
- Build an ANSI color
- compute_
scrollbar - Compute the min and max y (from the top of the terminal, both inclusive) for the thumb part of the scrollbar which would represent the scrolled content in the available height.
- fill_bg
- fix_
all_ tables - Modify the rows of all tables in order to ensure it fits the widths and all cells have the widths of their column.
- get_
default_ skin - Return a reference to the global skin
- gray
- Build a gray-level color, from 0 (mostly dark) to 23 (light).
- inline
- Return a formatted line, which implements Display.
- parse_
align - Read a Minimad Alignment from a string.
- parse_
attribute - Read a Minimad Attributement from a string.
- parse_
color - Read a Crossterm color from a string.
- parse_
compound_ style - Read a Minimad CompoundStyle from a string.
- parse_
line_ style - Read a line_style from a string.
- parse_
style_ token - parse_
style_ tokens - parse_
styled_ char - Read a styled char from a string.
- print_
inline - Write a string interpreted as markdown with the default skin.
- print_
text - Write a text interpreted as markdown with the default skin.
- rgb
- Build a RGB color
- style_
tokens_ to_ string - term_
text - Return a terminal wrapped formatted text, implementing Display.
- terminal_
size - Return a (width, height) with the dimensions of the available terminal in characters.
- text
- Return an unwrapped formatted text, implementing Display.
- write_
align - write_
attribute - write_
color - write_
style_ tokens