-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlib-text-file-widget.js
More file actions
149 lines (122 loc) · 3.9 KB
/
lib-text-file-widget.js
File metadata and controls
149 lines (122 loc) · 3.9 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: purple; icon-glyph: file-alt;
/* **********************************************
Name : lib-text-file-widget.js
Author : @supermamon
Version : 1.1.0
Desc : A widget to display the contents of a
text file.
Dependencies :
* iCloud enabled for Scriptable
Changelog:
-------------------------------------------------
v1.1.0 | 2022-09-16
* Center text in circular widgets.
v1.0.0 | 2022-09-16
* Initial release
********************************************** */
class TextFileWidget extends ListWidget {
constructor(filename, { minimumScale = 0.65, absolute = false, font = Font.body(), padding = 10, showFilename = false, centerContent = false } = {}) {
super()
if (config.runsInAccessoryWidget) {
// override padding when in home screen
padding = 0
if (config.widgetFamily == 'accessoryCircular') {
centerContent = true
font = Font.headline()
this.addAccessoryWidgetBackground = true
}
}
this.loading = true
this.minimumScale = minimumScale
this.absolute = absolute
this.filename = filename
this.padding = padding
this.setPadding(padding, padding, padding, padding)
this.font = font
this.showFilename = showFilename
this.centerContent = centerContent
if (!filename) {
this.addText('filename not provided')
this.loading = false
return
}
const Files = FileManager.iCloud()
this.filepath = this.absolute ? filename : Files.joinPath(Files.documentsDirectory(), filename)
if (this.showFilename) {
log('displaying filename')
const fileNameStack = this.addStack()
fileNameStack.layoutHorizontally()
fileNameStack.addSpacer()
const fnText = fileNameStack.addText(Files.fileName(this.filepath))
fnText.font = Font.regularSystemFont(8)
fileNameStack.addSpacer()
this.addSpacer(2)
}
if (!Files.fileExists(this.filepath)) {
log('file does not exists')
const text = this.addText("File doesn't exists.")
text.minimumScaleFactor = this.minimumScale
text.font = this.font
this.loading = false
} else {
Files.downloadFileFromiCloud(this.filepath)
.then(() => {
log('loading file contents')
try {
const content = Files.readString(this.filepath)
log(content)
const text = this.addText(content)
if (centerContent) {
text.centerAlignText()
}
text.minimumScaleFactor = this.minimumScale
text.font = this.font
log('file loaded and displayed')
} catch (e) {
log(e.message)
const text = this.addText(e.message)
text.minimumScaleFactor = this.minimumScale
text.font = this.font
log('encountered error')
}
this.loading = false
})
}
// test refresh every minute
const nextRefresh = new Date()
nextRefresh.setTime(nextRefresh.getTime() + 1000 * 60)
this.refreshAfterDate = nextRefresh
}
waitForLoad() {
return new Promise((resolve, reject) => {
if (!this.loading) {
log('not waiting because not loading')
resolve(0)
return
}
const timeout = 1000 * 3 // seconds
let iterations = 0
const t = new Timer()
t.timeInterval = 200
t.schedule(() => {
iterations += 200
if (!this.loading) {
log('stop waiting because file is loaded')
t.invalidate()
resolve(0)
return
} else if (iterations >= timeout) {
log('stop waiting because timeout reached')
this.loading = false
t.invalidate()
resolve(1)
return
}
log('still loading...')
})
})
}
}
module.exports = { TextFileWidget }