Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table dash fix #121

Merged
merged 9 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Version 0.7.4

- (#104) Files can now be opened in `$EDITOR` from `MDT`.
- Tested with Neovim and Vim.
- (#110) Add a Nix flake.
- (#105 partially) More flexible table dash parsing.
- Add highlighting for css, html, php.
- (#105) Length is defined by longest cell in column.
- Cell wrapping is not implemented yet.
- Inline code can be written with triple ticks, but it checks if it's a
codeblock first.

# Version 0.7.3

Expand Down
28 changes: 14 additions & 14 deletions 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
Expand Up @@ -35,7 +35,7 @@ image = "0.24.9"
itertools = "0.13.0"
lazy_static = "1.4.0"
notify = "6.1.1"
open = "5.1.2"
open = "5.1.3"
pest = "2.7.10"
pest_derive = "2.7.10"
ratatui = { version = "0.26.2", default-features = true, features = ["macros", "all-widgets", "serde"] }
Expand Down
20 changes: 10 additions & 10 deletions src/boxes/help_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ fn render_file_tree_help(expanded: bool, area: Rect, buf: &mut Buffer) {
let header = Row::new(vec!["Key", "Action"]);

let key_actions = [
Row::new(vec!["j or <Down>", "Move down"]),
Row::new(vec!["k or <Up>", "Move up"]),
Row::new(vec!["h or <Left>", "Go to previous page"]),
Row::new(vec!["l or <Right>", "Go to next page"]),
Row::new(vec!["j or \u{2193}", "Move down"]),
Row::new(vec!["k or \u{2191}", "Move up"]),
Row::new(vec!["h or \u{2190}", "Go to previous page"]),
Row::new(vec!["l or \u{2192}", "Go to next page"]),
Row::new(vec!["g", "Move to top"]),
Row::new(vec!["G", "Move to bottom"]),
Row::new(vec!["/ or f", "Search"]),
Row::new(vec!["<Enter>", "Open file"]),
Row::new(vec!["\u{21b5}", "Open file"]),
Row::new(vec!["q", "Quit"]),
];

Expand All @@ -81,19 +81,19 @@ fn render_markdown_help(expandend: bool, area: Rect, buf: &mut Buffer) {
let header = Row::new(vec!["Key", "Action"]);

let key_actions = [
Row::new(vec!["j or <Down>", "Move down"]),
Row::new(vec!["k or <Up>", "Move up"]),
Row::new(vec!["j or \u{2193}", "Move down"]),
Row::new(vec!["k or \u{2191}", "Move up"]),
Row::new(vec!["l or \u{2192}", "Move full page down"]),
Row::new(vec!["h or \u{2190}", "Move full page up"]),
Row::new(vec!["d", "Move half page down"]),
Row::new(vec!["h or <Left>", "Move full page up"]),
Row::new(vec!["l or <Right>", "Move full page down"]),
Row::new(vec!["u", "Move half page up"]),
Row::new(vec!["G", "Move to bottom"]),
Row::new(vec!["g", "Move to top"]),
Row::new(vec!["/ or f", "Search"]),
Row::new(vec!["b", "Go back to previous file"]),
Row::new(vec!["t", "Toggle file tree"]),
Row::new(vec!["s", "Enter select mode"]),
Row::new(vec!["<Enter>", "Open link/file"]),
Row::new(vec!["\u{21b5}", "Open link/file"]),
Row::new(vec!["e", "Edit file"]),
Row::new(vec!["q", "Quit"]),
];
Expand Down
4 changes: 2 additions & 2 deletions src/md.pest
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bold_italic = {
| ((NEWLINE | WHITESPACE_S) ~ WHITESPACE_S* ~ !"\\" ~ "_**" ~ (bold_italic_word | (NEWLINE ~ quote_prefix?))+ ~ "**_")
}

code = { NEWLINE? ~ WHITESPACE_S* ~ "`" ~ code_word+ ~ "`" }
code = { NEWLINE? ~ WHITESPACE_S* ~ ("`" ~ code_word+ ~ "`") | ("```" ~ code_word+ ~ "```")}
code_line = { NEWLINE ~ (c_line_char | WHITESPACE_S)* }
latex = { NEWLINE? ~ WHITESPACE_S* ~ !"\\" ~ "$"+ ~ WHITESPACE_S? ~ (latex_word | (NEWLINE ~ quote_prefix?))+ ~ "$"+ }
link = { NEWLINE? ~ WHITESPACE_S* ~ (link_line | wiki_link) }
Expand All @@ -80,7 +80,7 @@ sentence = _{ (latex | code | link | bold_italic | italic | bold | strikethrou
t_sentence = _{ (!"|" ~ (latex | code | link | italic | bold | strikethrough | t_normal))+ }

table_cell = { "|" ~ WHITESPACE_S* ~ t_sentence+ ~ WHITESPACE_S* ~ ("|" ~ " "* ~ NEWLINE)? }
table_seperator = { ("|"? ~ (WHITESPACE_S | ":")? ~ "-"+ ~ (WHITESPACE_S | ":")? ~ "|") }
table_seperator = { ("|"? ~ (WHITESPACE_S | ":")* ~ "-"+ ~ (WHITESPACE_S | ":")* ~ "|") }

u_list = { indent ~ ("-" | "*") ~ WHITESPACE_S ~ sentence+ }
o_list = { indent ~ o_list_counter ~ sentence+ }
Expand Down
19 changes: 15 additions & 4 deletions src/pages/markdown_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,20 @@ fn render_table(

let titles = content.chunks(column_count).next().unwrap().to_vec();

let widths = meta_info
.iter()
.map(|c| Constraint::Length(c.content().len() as u16))
let mut widths = vec![0; column_count];

content.chunks(column_count).for_each(|c| {
c.iter().enumerate().for_each(|(i, c)| {
let len = c.iter().map(|c| c.content().len()).sum::<usize>() + 1;
if len > widths[i] as usize {
widths[i] = len as u16;
}
});
});

let widths = widths
.into_iter()
.map(Constraint::Length)
.collect::<Vec<_>>();

let moved_content = content.chunks(column_count).skip(1).collect::<Vec<_>>();
Expand Down Expand Up @@ -422,7 +433,7 @@ fn render_table(
Clipping::None => (),
}

let table = Table::new(rows, &widths)
let table = Table::new(rows, widths)
.header(
header.style(
Style::default()
Expand Down