Skip to content

Commit

Permalink
chore: add table borders
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeprokisch committed Sep 3, 2024
1 parent f251bd3 commit 72234e9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smd"
version = "0.2.8"
version = "0.2.10"
edition = "2021"
authors = ["Guilherme Prokisch <guilherme.prokisch@gmail.com>"]
homepage = "https://github.com/guilhermeprokisch/smd"
Expand Down
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,33 @@ theme = "default"
code_highlight_theme = "Solarized (dark)"
max_image_width = 40
max_image_height = 13
disable_images = false
disable_links = false
render_images = true
render_links = true
render_table_borders = false
```

- `theme`: Overall color scheme (default: "default")
- `code_highlight_theme`: Theme for code syntax highlighting (default: "Solarized (dark)")
- `max_image_width` and `max_image_height`: Maximum dimensions for rendered images
- `disable_images`: If true, images will not be rendered
- `disable_links`: If true, links will not be clickable
- `render_images`: If false, images will not be rendered
- `render_links`: If false, links will not be clickable
- `render_table_borders`: If true, tables will be rendered with ASCII borders (default: false)

### Available Code Highlight Themes

The `code_highlight_theme` option can be set to any of the following values:

- "base16-ocean.dark"
- "base16-eighties.dark"
- "base16-mocha.dark"
- "base16-ocean.light"
- "InspiredGitHub"
- "Solarized (dark)"
- "Solarized (light)"

These are the default themes provided by the syntect library. Choose the theme that best suits your terminal's color scheme and personal preferences.

Note: The actual appearance of these themes may vary slightly depending on your terminal's color settings.

## Contributing

Expand Down
58 changes: 54 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ static DOCS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/docs");

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
pub theme: String,
pub code_highlight_theme: String,
pub max_image_width: Option<u32>,
pub max_image_height: Option<u32>,
pub render_images: bool,
pub render_links: bool,
pub render_table_borders: bool,
}

impl Default for Config {
fn default() -> Self {
Config {
theme: "default".to_string(),
code_highlight_theme: "Solarized (dark)".to_string(),
max_image_width: Some(40),
max_image_height: Some(13),
render_images: true,
render_links: true,
render_table_borders: false,
}
}
}
Expand Down Expand Up @@ -377,6 +377,7 @@ fn style_to_termcolor(style: &Style) -> Option<Color> {

fn render_table(node: &Value) -> io::Result<()> {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
let config = get_config();

if let Some(children) = node["children"].as_array() {
let mut column_widths = Vec::new();
Expand All @@ -398,7 +399,17 @@ fn render_table(node: &Value) -> io::Result<()> {
// Render table
for (i, row) in children.iter().enumerate() {
if let Some(cells) = row["children"].as_array() {
// Top border for the first row
if i == 0 && config.render_table_borders {
print_horizontal_border(&column_widths, "┌", "┬", "┐")?;
}

print!("{}", get_indent());

if config.render_table_borders {
print!("│ ");
}

for (j, cell) in cells.iter().enumerate() {
let content = cell["children"][0]["value"].as_str().unwrap_or("");

Expand All @@ -413,20 +424,59 @@ fn render_table(node: &Value) -> io::Result<()> {
}

print!("{:<width$}", content, width = column_widths[j]);
stdout.reset()?;

if j < cells.len() - 1 {
if config.render_table_borders {
if j < cells.len() - 1 {
print!(" │ ");
} else {
print!(" │");
}
} else if j < cells.len() - 1 {
print!(" "); // Add two spaces between columns
}
}

println!();
stdout.reset()?;

// Print horizontal line after header and between rows
if config.render_table_borders {
if i == 0 {
print_horizontal_border(&column_widths, "├", "┼", "┤")?;
} else if i < children.len() - 1 {
print_horizontal_border(&column_widths, "├", "┼", "┤")?;
}
}
}
}

// Print bottom border
if config.render_table_borders {
print_horizontal_border(&column_widths, "└", "┴", "┘")?;
}
}

Ok(())
}

fn print_horizontal_border(
column_widths: &[usize],
left: &str,
middle: &str,
right: &str,
) -> io::Result<()> {
print!("{}", get_indent());
print!("{}", left);
for (i, width) in column_widths.iter().enumerate() {
print!("{}", "─".repeat(width + 2)); // +2 for the padding spaces
if i < column_widths.len() - 1 {
print!("{}", middle);
}
}
println!("{}", right);
Ok(())
}

fn render_list(node: &Value) -> io::Result<()> {
let is_ordered = node["ordered"].as_bool().unwrap_or(false);
if let Ok(mut list_stack) = LIST_STACK.lock() {
Expand Down

0 comments on commit 72234e9

Please # to comment.