This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
99 lines (91 loc) · 2.79 KB
/
App.vue
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
<template>
<screen ref="screen" :smartCSR="true" :autoPadding="true">
<text top="0" left="center" :top="2" :content="`${ this.pageTitle }`" />
<box :border="{ type: 'line' }" :top="this.pageTitle ? 4 : 2" :bottom="2" :left="4" :right="4" >
<box ref="content" @click="nextSlide" :top="1" :bottom="1" :left="2" :right="2" :autofocus="true" :style="contentStyle" :scrollbar="true" :alwaysScroll="true" :scrollable="true" :mouse="true" :keys="true" :vi="true" :content="this.pageContent" />
</box>
</screen>
</template>
<script>
const fs = require('fs')
const marked = require('marked')
const TerminalRenderer = require('marked-terminal')
import Vue from 'blessed-vue'
marked.setOptions({
renderer: new TerminalRenderer()
})
export default {
name: 'app',
computed: {
contentStyle: function() {
return {
scrollbar: {
bg: 'gray'
},
}
}
},
data: function() {
return {
page: 0,
pages: [],
pageTitle: '',
pageContent: '',
}
},
mounted: function() {
// Close the program when CTRL+C is pressed.
this.$refs.screen.key(['C-c', 'escape'], () => {
process.exit(0)
})
this.$refs.content.focus()
// Error if no file provided.
if (typeof process.argv[2] === 'undefined') {
throw new Error("No file provided.")
}
// Read file with UTF-8 encoding, splitting it into pages based on !!!
fs.readFile(process.argv[2], 'utf8', (err, contents) => {
// Generates SGR sequences which can be read by blessed.
if (contents.indexOf('!!!') !== -1) {
this.pages = contents.split('!!!').slice(1)
this.pageTitle = marked(this.pages[0].split(/\n/)[0])
this.pageContent = marked(this.pages[0].split(/\n/).slice(1).join('\n'))
} else {
this.pages = [contents]
this.pageTitle = ''
this.pageContent = marked(this.pages[0])
}
})
this.$refs.screen.key(['right', 'space'], () => {
this.nextSlide()
})
this.$refs.screen.key(['left'], () => {
this.prevSlide()
})
},
methods: {
prevSlide() {
if (this.page > 0) {
this.pageContent = ''
this.$refs.content.setScroll(0)
this.page -= 1
this.pageTitle = marked(this.pages[this.page].split(/\n/)[0])
Vue.nextTick(() => {
this.pageContent = marked(this.pages[this.page].split(/\n/).slice(1).join('\n'))
})
}
},
nextSlide() {
if (this.page < this.pages.length - 2) {
this.pageContent = ''
this.$refs.content.setScroll(0)
this.page += 1
this.pageTitle = marked(this.pages[this.page].split(/\n/)[0])
Vue.nextTick(() => {
this.pageContent = marked(this.pages[this.page].split(/\n/).slice(1).join('\n'))
})
}
}
}
}
</script>