marvi 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +10 -0
- data/exe/marvi +29 -0
- data/lib/marvi/ansi.rb +17 -0
- data/lib/marvi/ast_walker.rb +168 -0
- data/lib/marvi/document.rb +31 -0
- data/lib/marvi/renderer/ansi.rb +36 -0
- data/lib/marvi/renderer/curses.rb +136 -0
- data/lib/marvi/version.rb +5 -0
- data/lib/marvi.rb +12 -0
- data/sig/marvi.rbs +4 -0
- metadata +101 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 81a8d64fd49b33e6d17b7d1b2bf898bca77b6abb347ec5d4f6844f6b2969b647
|
|
4
|
+
data.tar.gz: b8020fc1f24ab9806f1dc3d8b94baf4061e6ab329a0dc54598db747465e5c5d7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: decbb5c0688e364d432aced0e156a9b0cdd1d33f3aeaa6c9bba5f7761f1bfd5182e740cc023b956926838261e1d66fa8a90f0666ded886d72dd397010c793ac7
|
|
7
|
+
data.tar.gz: 5f969855ab9c5d49801bc4675ecd19d4c815a8e49e7532559bc6f46ffc6d76f3ae6f735e6f73c495bee7406ccbfcd091305127f29c00e0d0c859179c3447543b
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"marvi" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["takkanm@gmail.com"](mailto:"takkanm@gmail.com").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mitsutaka Mimura
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Marvi
|
|
2
|
+
|
|
3
|
+
Marvi is a terminal Markdown renderer. It parses Markdown and displays it with ANSI colors and styles directly in your terminal.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
gem install marvi
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or add to your Gemfile:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add marvi
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### CLI
|
|
20
|
+
|
|
21
|
+
Render a Markdown file:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
marvi README.md
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Read from stdin:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
cat README.md | marvi
|
|
31
|
+
echo "# Hello **world**" | marvi
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Ruby API
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
require "marvi"
|
|
38
|
+
|
|
39
|
+
markdown = File.read("README.md")
|
|
40
|
+
puts Marvi::Renderer.new.render(markdown)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Supported Markdown elements
|
|
44
|
+
|
|
45
|
+
- Headings (`#`, `##`, `###`, ...)
|
|
46
|
+
- Bold (`**text**`) and italic (`*text*`)
|
|
47
|
+
- Inline code (`` `code` ``)
|
|
48
|
+
- Code blocks (` ``` `)
|
|
49
|
+
- Unordered and ordered lists
|
|
50
|
+
- Blockquotes (`> text`)
|
|
51
|
+
- Horizontal rules (`---`)
|
|
52
|
+
|
|
53
|
+
## Development
|
|
54
|
+
|
|
55
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt.
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
bin/setup
|
|
59
|
+
rake test
|
|
60
|
+
bin/console
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Contributing
|
|
64
|
+
|
|
65
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/takkanm/marvi.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/exe/marvi
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "optparse"
|
|
5
|
+
require_relative "../lib/marvi"
|
|
6
|
+
|
|
7
|
+
OptionParser.new do |opts|
|
|
8
|
+
opts.banner = "Usage: marvi [FILE]\n cat FILE | marvi"
|
|
9
|
+
opts.on("-h", "--help", "Show help") do
|
|
10
|
+
puts opts
|
|
11
|
+
exit
|
|
12
|
+
end
|
|
13
|
+
opts.on("-v", "--version", "Show version") do
|
|
14
|
+
puts Marvi::VERSION
|
|
15
|
+
exit
|
|
16
|
+
end
|
|
17
|
+
end.parse!
|
|
18
|
+
|
|
19
|
+
markdown = if ARGV.empty?
|
|
20
|
+
$stdin.read
|
|
21
|
+
else
|
|
22
|
+
File.read(ARGV[0])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
if $stdout.tty?
|
|
26
|
+
Marvi::Renderer::Curses.new.render(markdown)
|
|
27
|
+
else
|
|
28
|
+
print Marvi::Renderer::ANSI.new.render(markdown)
|
|
29
|
+
end
|
data/lib/marvi/ansi.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Marvi
|
|
4
|
+
module ANSI
|
|
5
|
+
RESET = "\e[0m"
|
|
6
|
+
BOLD = "\e[1m"
|
|
7
|
+
ITALIC = "\e[3m"
|
|
8
|
+
CYAN = "\e[36m"
|
|
9
|
+
YELLOW = "\e[33m"
|
|
10
|
+
GREEN = "\e[32m"
|
|
11
|
+
MAGENTA = "\e[35m"
|
|
12
|
+
WHITE = "\e[37m"
|
|
13
|
+
BG_DARK = "\e[48;5;236m"
|
|
14
|
+
|
|
15
|
+
HEADER_COLORS = [CYAN, GREEN, YELLOW, MAGENTA, WHITE, WHITE].freeze
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "kramdown"
|
|
4
|
+
require "kramdown-parser-gfm"
|
|
5
|
+
|
|
6
|
+
module Marvi
|
|
7
|
+
class ASTWalker
|
|
8
|
+
HEADER_COLORS = %i[cyan green yellow magenta white white].freeze
|
|
9
|
+
|
|
10
|
+
def walk(markdown)
|
|
11
|
+
doc = Kramdown::Document.new(markdown, input: "GFM")
|
|
12
|
+
lines = render_block(doc.root)
|
|
13
|
+
lines.pop while lines.last&.plain_text&.empty?
|
|
14
|
+
lines
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def render_block(el, indent: 0, list_type: nil, list_index: nil)
|
|
20
|
+
case el.type
|
|
21
|
+
when :root
|
|
22
|
+
el.children.flat_map { |child| render_block(child) }
|
|
23
|
+
when :header
|
|
24
|
+
render_header(el)
|
|
25
|
+
when :p
|
|
26
|
+
[RichLine.new(render_inline_children(el)), RichLine.blank]
|
|
27
|
+
when :ul
|
|
28
|
+
el.children.flat_map { |child| render_block(child, indent: indent, list_type: :ul) } + [RichLine.blank]
|
|
29
|
+
when :ol
|
|
30
|
+
el.children.each_with_index.flat_map do |child, i|
|
|
31
|
+
render_block(child, indent: indent, list_type: :ol, list_index: i + 1)
|
|
32
|
+
end + [RichLine.blank]
|
|
33
|
+
when :li
|
|
34
|
+
render_li(el, indent: indent, list_type: list_type, list_index: list_index)
|
|
35
|
+
when :codeblock
|
|
36
|
+
render_codeblock(el)
|
|
37
|
+
when :blockquote
|
|
38
|
+
render_blockquote(el)
|
|
39
|
+
when :hr
|
|
40
|
+
[RichLine.new([Span.new(text: "─" * 60, color: :cyan)]), RichLine.blank]
|
|
41
|
+
when :table
|
|
42
|
+
render_table(el)
|
|
43
|
+
when :blank
|
|
44
|
+
[RichLine.blank]
|
|
45
|
+
else
|
|
46
|
+
el.children.flat_map { |child| render_block(child) }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def render_header(el)
|
|
51
|
+
level = el.options[:level]
|
|
52
|
+
color = HEADER_COLORS[level - 1]
|
|
53
|
+
prefix = Span.new(text: "#" * level + " ", bold: true, color: color)
|
|
54
|
+
content = render_inline_children(el).map do |s|
|
|
55
|
+
Span.new(text: s.text, bold: true, italic: s.italic, color: s.color || color, bg_color: s.bg_color)
|
|
56
|
+
end
|
|
57
|
+
[RichLine.new([prefix] + content), RichLine.blank]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def render_li(el, indent:, list_type:, list_index:)
|
|
61
|
+
bullet = list_type == :ol ? "#{list_index}." : "•"
|
|
62
|
+
prefix = Span.new(text: "#{" " * indent}#{bullet} ", color: :cyan)
|
|
63
|
+
lines = []
|
|
64
|
+
|
|
65
|
+
el.children.each do |child|
|
|
66
|
+
case child.type
|
|
67
|
+
when :ul, :ol
|
|
68
|
+
nested = render_block(child, indent: indent + 1, list_type: child.type)
|
|
69
|
+
nested.pop while nested.last&.plain_text&.empty?
|
|
70
|
+
lines += nested
|
|
71
|
+
when :p
|
|
72
|
+
if lines.empty?
|
|
73
|
+
lines << RichLine.new([prefix] + render_inline_children(child))
|
|
74
|
+
else
|
|
75
|
+
lines += render_block(child)
|
|
76
|
+
end
|
|
77
|
+
else
|
|
78
|
+
if lines.empty?
|
|
79
|
+
lines << RichLine.new([prefix] + render_inline(child))
|
|
80
|
+
else
|
|
81
|
+
lines << RichLine.new(render_inline(child))
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
lines
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def render_codeblock(el)
|
|
89
|
+
lang = el.options[:lang]
|
|
90
|
+
lines = []
|
|
91
|
+
lines << RichLine.new([Span.new(text: lang, color: :yellow)]) if lang
|
|
92
|
+
el.value.chomp.split("\n").each do |line|
|
|
93
|
+
lines << RichLine.new([Span.new(text: " #{line}", color: :green, bg_color: :dark)])
|
|
94
|
+
end
|
|
95
|
+
lines << RichLine.blank
|
|
96
|
+
lines
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def render_blockquote(el)
|
|
100
|
+
inner = el.children.flat_map { |child| render_block(child) }
|
|
101
|
+
prefix = Span.new(text: "│ ", color: :cyan)
|
|
102
|
+
inner.map { |line| RichLine.new([prefix] + line.spans) } + [RichLine.blank]
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def render_table(el)
|
|
106
|
+
rows = el.children.flat_map(&:children)
|
|
107
|
+
header_row = el.children.find { |s| s.type == :thead }&.children&.first
|
|
108
|
+
|
|
109
|
+
cell_spans = rows.map { |row| row.children.map { |cell| render_inline_children(cell) } }
|
|
110
|
+
col_widths = cell_spans.map { |row| row.map { |spans| spans.sum { |s| s.text.length } } }
|
|
111
|
+
.transpose.map { |col| col.max }
|
|
112
|
+
|
|
113
|
+
lines = []
|
|
114
|
+
top = col_widths.map { |w| "─" * (w + 2) }.join("┬")
|
|
115
|
+
lines << RichLine.new([Span.new(text: "┌#{top}┐", color: :cyan)])
|
|
116
|
+
|
|
117
|
+
rows.each_with_index do |row, ri|
|
|
118
|
+
is_header = row == header_row
|
|
119
|
+
row_spans = []
|
|
120
|
+
row.children.each_with_index do |cell, ci|
|
|
121
|
+
content = cell_spans[ri][ci]
|
|
122
|
+
plain_len = content.sum { |s| s.text.length }
|
|
123
|
+
padding = col_widths[ci] - plain_len
|
|
124
|
+
styled = is_header ? content.map { |s| Span.new(text: s.text, bold: true, color: :cyan) } : content
|
|
125
|
+
row_spans += [Span.new(text: "│ ", color: :cyan)] + styled + [Span.new(text: " " * (padding + 1))]
|
|
126
|
+
end
|
|
127
|
+
row_spans << Span.new(text: "│", color: :cyan)
|
|
128
|
+
lines << RichLine.new(row_spans)
|
|
129
|
+
|
|
130
|
+
if is_header
|
|
131
|
+
sep = col_widths.map { |w| "─" * (w + 2) }.join("┼")
|
|
132
|
+
lines << RichLine.new([Span.new(text: "├#{sep}┤", color: :cyan)])
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
bottom = col_widths.map { |w| "─" * (w + 2) }.join("┴")
|
|
137
|
+
lines << RichLine.new([Span.new(text: "└#{bottom}┘", color: :cyan)])
|
|
138
|
+
lines << RichLine.blank
|
|
139
|
+
lines
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def render_inline_children(el)
|
|
143
|
+
el.children.flat_map { |child| render_inline(child) }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def render_inline(el)
|
|
147
|
+
case el.type
|
|
148
|
+
when :text
|
|
149
|
+
[Span.new(text: el.value)]
|
|
150
|
+
when :strong
|
|
151
|
+
render_inline_children(el).map { |s| Span.new(text: s.text, bold: true, italic: s.italic, color: s.color, bg_color: s.bg_color) }
|
|
152
|
+
when :em
|
|
153
|
+
render_inline_children(el).map { |s| Span.new(text: s.text, bold: s.bold, italic: true, color: s.color, bg_color: s.bg_color) }
|
|
154
|
+
when :codespan
|
|
155
|
+
[Span.new(text: " #{el.value} ", color: :cyan, bg_color: :dark)]
|
|
156
|
+
when :br
|
|
157
|
+
[Span.new(text: "\n")]
|
|
158
|
+
when :smart_quote
|
|
159
|
+
char = {lsquo: "'", rsquo: "'", ldquo: "\u201C", rdquo: "\u201D"}.fetch(el.value, "'")
|
|
160
|
+
[Span.new(text: char)]
|
|
161
|
+
when :entity
|
|
162
|
+
[Span.new(text: el.options[:original] || el.value.char)]
|
|
163
|
+
else
|
|
164
|
+
render_inline_children(el)
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Marvi
|
|
4
|
+
class Span
|
|
5
|
+
attr_reader :text, :bold, :italic, :color, :bg_color
|
|
6
|
+
|
|
7
|
+
def initialize(text:, bold: false, italic: false, color: nil, bg_color: nil)
|
|
8
|
+
@text = text
|
|
9
|
+
@bold = bold
|
|
10
|
+
@italic = italic
|
|
11
|
+
@color = color
|
|
12
|
+
@bg_color = bg_color
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class RichLine
|
|
17
|
+
attr_reader :spans
|
|
18
|
+
|
|
19
|
+
def initialize(spans = [])
|
|
20
|
+
@spans = spans
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def plain_text
|
|
24
|
+
@spans.map(&:text).join
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.blank
|
|
28
|
+
new([])
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Marvi
|
|
4
|
+
module Renderer
|
|
5
|
+
class ANSI
|
|
6
|
+
COLOR_MAP = {
|
|
7
|
+
cyan: Marvi::ANSI::CYAN,
|
|
8
|
+
green: Marvi::ANSI::GREEN,
|
|
9
|
+
yellow: Marvi::ANSI::YELLOW,
|
|
10
|
+
magenta: Marvi::ANSI::MAGENTA,
|
|
11
|
+
white: Marvi::ANSI::WHITE
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
def render(markdown)
|
|
15
|
+
lines = ASTWalker.new.walk(markdown)
|
|
16
|
+
lines.map { |line| render_line(line) }.join("\n") + "\n"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def render_line(line)
|
|
22
|
+
line.spans.map { |span| render_span(span) }.join
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def render_span(span)
|
|
26
|
+
codes = []
|
|
27
|
+
codes << Marvi::ANSI::BOLD if span.bold
|
|
28
|
+
codes << Marvi::ANSI::ITALIC if span.italic
|
|
29
|
+
codes << COLOR_MAP[span.color] if span.color
|
|
30
|
+
codes << Marvi::ANSI::BG_DARK if span.bg_color == :dark
|
|
31
|
+
return span.text if codes.empty?
|
|
32
|
+
"#{codes.join}#{span.text}#{Marvi::ANSI::RESET}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "curses"
|
|
4
|
+
|
|
5
|
+
module Marvi
|
|
6
|
+
module Renderer
|
|
7
|
+
class Curses
|
|
8
|
+
COLOR_PAIRS = {
|
|
9
|
+
cyan: 1,
|
|
10
|
+
green: 2,
|
|
11
|
+
yellow: 3,
|
|
12
|
+
magenta: 4,
|
|
13
|
+
white: 5,
|
|
14
|
+
green_on_dark: 6,
|
|
15
|
+
cyan_on_dark: 7
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
ITALIC_ATTR = (defined?(::Curses::A_ITALIC) ? ::Curses::A_ITALIC : 0)
|
|
19
|
+
|
|
20
|
+
def render(markdown)
|
|
21
|
+
@lines = ASTWalker.new.walk(markdown)
|
|
22
|
+
@scroll = 0
|
|
23
|
+
|
|
24
|
+
::Curses.init_screen
|
|
25
|
+
::Curses.start_color
|
|
26
|
+
::Curses.use_default_colors
|
|
27
|
+
::Curses.noecho
|
|
28
|
+
::Curses.cbreak
|
|
29
|
+
::Curses.stdscr.keypad(true)
|
|
30
|
+
setup_colors
|
|
31
|
+
draw
|
|
32
|
+
|
|
33
|
+
catch(:quit) do
|
|
34
|
+
loop { handle_key(::Curses.getch) }
|
|
35
|
+
end
|
|
36
|
+
ensure
|
|
37
|
+
::Curses.close_screen
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def setup_colors
|
|
43
|
+
::Curses.init_pair(COLOR_PAIRS[:cyan], ::Curses::COLOR_CYAN, -1)
|
|
44
|
+
::Curses.init_pair(COLOR_PAIRS[:green], ::Curses::COLOR_GREEN, -1)
|
|
45
|
+
::Curses.init_pair(COLOR_PAIRS[:yellow], ::Curses::COLOR_YELLOW, -1)
|
|
46
|
+
::Curses.init_pair(COLOR_PAIRS[:magenta], ::Curses::COLOR_MAGENTA, -1)
|
|
47
|
+
::Curses.init_pair(COLOR_PAIRS[:white], ::Curses::COLOR_WHITE, -1)
|
|
48
|
+
::Curses.init_pair(COLOR_PAIRS[:green_on_dark], ::Curses::COLOR_GREEN, ::Curses::COLOR_BLACK)
|
|
49
|
+
::Curses.init_pair(COLOR_PAIRS[:cyan_on_dark], ::Curses::COLOR_CYAN, ::Curses::COLOR_BLACK)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def handle_key(key)
|
|
53
|
+
case key
|
|
54
|
+
when "q", "Q", 27 then throw :quit
|
|
55
|
+
when "j", ::Curses::Key::DOWN then scroll_by(1)
|
|
56
|
+
when "k", ::Curses::Key::UP then scroll_by(-1)
|
|
57
|
+
when "d" then scroll_by(page_size / 2)
|
|
58
|
+
when "u" then scroll_by(-page_size / 2)
|
|
59
|
+
when "f", " ", ::Curses::Key::NPAGE then scroll_by(page_size)
|
|
60
|
+
when "b", ::Curses::Key::PPAGE then scroll_by(-page_size)
|
|
61
|
+
when "g" then @scroll = 0; draw
|
|
62
|
+
when "G" then @scroll = max_scroll; draw
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def draw
|
|
67
|
+
::Curses.clear
|
|
68
|
+
visible_lines.each_with_index do |line, row|
|
|
69
|
+
::Curses.setpos(row, 0)
|
|
70
|
+
render_line(line)
|
|
71
|
+
end
|
|
72
|
+
draw_status_bar
|
|
73
|
+
::Curses.refresh
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def draw_status_bar
|
|
77
|
+
::Curses.setpos(::Curses.lines - 1, 0)
|
|
78
|
+
::Curses.attron(::Curses.color_pair(COLOR_PAIRS[:cyan])) do
|
|
79
|
+
top = @scroll + 1
|
|
80
|
+
bottom = [@scroll + page_size, @lines.size].min
|
|
81
|
+
status = " #{top}-#{bottom}/#{@lines.size} j/k scroll g/G top/bottom q quit"
|
|
82
|
+
::Curses.addstr(status.ljust(::Curses.cols)[0, ::Curses.cols])
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def render_line(line)
|
|
87
|
+
line.spans.each do |span|
|
|
88
|
+
render_span(span)
|
|
89
|
+
rescue ::Curses::Error
|
|
90
|
+
# ignore write errors at line edge
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def render_span(span)
|
|
95
|
+
attr = build_attr(span)
|
|
96
|
+
if attr != 0
|
|
97
|
+
::Curses.attron(attr) { ::Curses.addstr(span.text) }
|
|
98
|
+
else
|
|
99
|
+
::Curses.addstr(span.text)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def build_attr(span)
|
|
104
|
+
attr = 0
|
|
105
|
+
attr |= ::Curses::A_BOLD if span.bold
|
|
106
|
+
attr |= ITALIC_ATTR if span.italic
|
|
107
|
+
|
|
108
|
+
pair_key = if span.bg_color == :dark
|
|
109
|
+
span.color == :cyan ? :cyan_on_dark : :green_on_dark
|
|
110
|
+
elsif span.color
|
|
111
|
+
span.color
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
attr |= ::Curses.color_pair(COLOR_PAIRS[pair_key]) if pair_key
|
|
115
|
+
attr
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def visible_lines
|
|
119
|
+
@lines[@scroll, page_size] || []
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def page_size
|
|
123
|
+
[::Curses.lines - 1, 1].max
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def max_scroll
|
|
127
|
+
[@lines.size - page_size, 0].max
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def scroll_by(delta)
|
|
131
|
+
@scroll = [[@scroll + delta, 0].max, max_scroll].min
|
|
132
|
+
draw
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
data/lib/marvi.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "marvi/version"
|
|
4
|
+
require_relative "marvi/ansi"
|
|
5
|
+
require_relative "marvi/document"
|
|
6
|
+
require_relative "marvi/ast_walker"
|
|
7
|
+
require_relative "marvi/renderer/ansi"
|
|
8
|
+
require_relative "marvi/renderer/curses"
|
|
9
|
+
|
|
10
|
+
module Marvi
|
|
11
|
+
class Error < StandardError; end
|
|
12
|
+
end
|
data/sig/marvi.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: marvi
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mitsutaka Mimura
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: kramdown
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '2.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '2.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: kramdown-parser-gfm
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: curses
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '1.0'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.0'
|
|
54
|
+
description: Renders Markdown with ANSI colors in pipes and an interactive curses
|
|
55
|
+
pager in TTY.
|
|
56
|
+
email:
|
|
57
|
+
- takkanm@gmail.com
|
|
58
|
+
executables:
|
|
59
|
+
- marvi
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- CHANGELOG.md
|
|
64
|
+
- CODE_OF_CONDUCT.md
|
|
65
|
+
- LICENSE.txt
|
|
66
|
+
- README.md
|
|
67
|
+
- Rakefile
|
|
68
|
+
- exe/marvi
|
|
69
|
+
- lib/marvi.rb
|
|
70
|
+
- lib/marvi/ansi.rb
|
|
71
|
+
- lib/marvi/ast_walker.rb
|
|
72
|
+
- lib/marvi/document.rb
|
|
73
|
+
- lib/marvi/renderer/ansi.rb
|
|
74
|
+
- lib/marvi/renderer/curses.rb
|
|
75
|
+
- lib/marvi/version.rb
|
|
76
|
+
- sig/marvi.rbs
|
|
77
|
+
homepage: https://github.com/takkanm/marvi
|
|
78
|
+
licenses:
|
|
79
|
+
- MIT
|
|
80
|
+
metadata:
|
|
81
|
+
homepage_uri: https://github.com/takkanm/marvi
|
|
82
|
+
source_code_uri: https://github.com/takkanm/marvi
|
|
83
|
+
changelog_uri: https://github.com/takkanm/marvi/blob/main/CHANGELOG.md
|
|
84
|
+
rdoc_options: []
|
|
85
|
+
require_paths:
|
|
86
|
+
- lib
|
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
|
+
requirements:
|
|
89
|
+
- - ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: 3.2.0
|
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
requirements: []
|
|
98
|
+
rubygems_version: 4.0.3
|
|
99
|
+
specification_version: 4
|
|
100
|
+
summary: Terminal Markdown renderer
|
|
101
|
+
test_files: []
|