This repository was archived by the owner on Jun 7, 2024. It is now read-only.
forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocs.js
More file actions
105 lines (90 loc) · 2.38 KB
/
docs.js
File metadata and controls
105 lines (90 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict'
const blessed = require('blessed')
const contrib = require('blessed-contrib')
const fs = require('fs')
const path = require('path')
function renderDocs () {
const docsBase = path.join(path.dirname(require.resolve('fastify')), 'docs')
if (!docsBase) return console.error('Something went wrong finding docs... please report a bug! https://github.com/fastify/fastify-cli')
const fileNames = fs.readdirSync(docsBase)
const docNames = []
const fileContents = []
for (const fileName of fileNames) {
const fullFilePath = path.join(docsBase, fileName)
if (fs.lstatSync(fullFilePath).isFile()) {
docNames.push(fileName.replace(/-/g, ' ').replace(/\.md$/, ''))
fileContents.push((fs.readFileSync(fullFilePath)).toString('utf8'))
}
}
const screen = blessed.screen({
smartCSR: true
})
// eslint-disable-next-line
const grid = new contrib.grid({ rows: 12, cols: 12, screen: screen })
const list = grid.set(0, 0, 12, 3, blessed.list, {
label: 'Docs',
items: docNames,
keys: true,
vi: true,
mouse: true,
style: {
border: {
fg: 'green'
},
selected: {
fg: 'green',
bg: 'white'
}
}
})
list.select(0)
list.focus()
list.on('select', (el, selected) => {
markdown.setLabel(docNames[selected])
markdown.setMarkdown(fileContents[selected])
screen.render()
})
const markdown = grid.set(0, 3, 12, 9, contrib.markdown, {
label: docNames[0],
scrollable: true,
keys: true,
vi: true,
mouse: true,
style: {
border: {
fg: 'white'
}
},
alwaysScroll: true,
scrollbar: {
ch: ' ',
bg: 'blue'
}
})
markdown.setMarkdown(fileContents[0])
list.on('keypress', (el, e) => {
if (e.full === 'right') {
markdown.focus()
list.style.selected.fg = null
list.style.selected.bg = null
list.style.border.fg = 'white'
markdown.style.border.fg = 'green'
screen.render()
}
})
markdown.on('keypress', (el, e) => {
if (e.full === 'left') {
list.focus()
list.style.selected.fg = 'green'
list.style.selected.bg = 'white'
list.style.border.fg = 'green'
markdown.style.border.fg = 'white'
screen.render()
}
})
screen.render()
screen.key(['escape', 'q', 'C-c'], function (ch, key) {
return process.exit(0)
})
}
module.exports = renderDocs