-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
237 lines (198 loc) · 6.39 KB
/
io.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package gg
import (
"bytes"
"io"
"io/fs"
"os"
r "reflect"
"strings"
)
/*
Creates a read-closer able to read from the given string or byte slice, where
the "close" operation does nothing. Similar to combining stdlib functions, but
shorter and avoids allocation in case of bytes-to-string or string-to-bytes
conversion:
// Longer and marginally less efficient:
io.NopCloser(bytes.NewReader([]byte(`some_data`)))
io.NopCloser(strings.NewReader(string(`some_data`)))
// Equivalent, shorter, marginally more efficient:
gg.NewReadCloser([]byte(`some_data`))
gg.NewReadCloser(string(`some_data`))
*/
func NewReadCloser[A Text](val A) io.ReadCloser {
if Kind[A]() == r.String {
return new(StringReadCloser).Reset(ToString(val))
}
return new(BytesReadCloser).Reset(ToBytes(val))
}
// Variant of `strings.Reader` that also implements nop `io.Closer`.
type StringReadCloser struct{ strings.Reader }
// Calls `(*strings.Reader).Reset`.
func (self *StringReadCloser) Reset(src string) *StringReadCloser {
self.Reader.Reset(src)
return self
}
// Implement `io.Closer`. This is a nop. The error is always nil.
func (*StringReadCloser) Close() error { return nil }
// Variant of `bytes.Reader` that also implements nop `io.Closer`.
type BytesReadCloser struct{ bytes.Reader }
// Calls `(*bytes.Reader).Reset`.
func (self *BytesReadCloser) Reset(src []byte) *BytesReadCloser {
self.Reader.Reset(src)
return self
}
// Implement `io.Closer`. This is a nop. The error is always nil.
func (*BytesReadCloser) Close() error { return nil }
/*
Same as `io.ReadAll` but with different error handling.
If reader is nil, returns nil. Panics on errors.
*/
func ReadAll(src io.Reader) []byte {
if src == nil {
return nil
}
return Try1(io.ReadAll(src))
}
/*
Variant of `ReadAll` that closes the provided reader when done.
If reader is nil, returns nil. Panics on errors.
*/
func ReadCloseAll(src io.ReadCloser) []byte {
if src == nil {
return nil
}
defer src.Close()
return Try1(io.ReadAll(src))
}
/*
Shortcut for using `os.Stat` to check if there is an existing file or directory
at the given path.
*/
func PathExists(path string) bool {
info := fileInfo(path)
return info != nil
}
/*
Shortcut for using `os.Stat` to check if there is an existing directory at the
given path.
*/
func DirExists(path string) bool {
info := fileInfo(path)
return info != nil && info.IsDir()
}
/*
Shortcut for using `os.Stat` to check if the file at the given path exists,
and is not a directory.
*/
func FileExists(path string) bool {
info := fileInfo(path)
return info != nil && !info.IsDir()
}
func fileInfo(path string) os.FileInfo {
if path == `` {
return nil
}
info, _ := os.Stat(path)
return info
}
// Shortcut for `os.ReadDir`. Panics on error.
func ReadDir(path string) []fs.DirEntry { return Try1(os.ReadDir(path)) }
/*
Shortcut for using `os.ReadDir` to return a list of file names in the given
directory. Panics on error.
*/
func ReadDirFileNames(path string) []string {
return MapCompact(ReadDir(path), dirEntryToFileName)
}
/*
Shortcut for `os.ReadFile`. Panics on error. Converts the content to the
requested text type without an additional allocation.
*/
func ReadFile[A Text](path string) A {
return ToText[A](Try1(os.ReadFile(path)))
}
/*
Shortcut for `os.WriteFile` with default permissions `os.ModePerm`. Panics on
error. Takes an arbitrary text type conforming to `Text` and converts it to
bytes without an additional allocation.
*/
func WriteFile[A Text](path string, body A) {
Try(os.WriteFile(path, ToBytes(body), os.ModePerm))
}
/*
Fully reads the given stream via `io.ReadAll` and returns two "forks". If
reading fails, panics. If the input is nil, both outputs are nil.
*/
func ForkReader(src io.Reader) (_, _ io.Reader) {
if src == nil {
return nil, nil
}
defer Detail(`failed to read for forking`)
text := ReadAll(src)
return NewReadCloser(text), NewReadCloser(text)
}
/*
Fully reads the given stream via `io.ReadAll`, closing it at the end, and
returns two "forks". Used internally by `(*gh.Req).CloneBody` and
`(*gh.Res).CloneBody`. If reading fails, panics. If the input is nil, both
outputs are nil.
*/
func ForkReadCloser(src io.ReadCloser) (_, _ io.ReadCloser) {
if src == nil {
return nil, nil
}
defer Detail(`failed to read for forking`)
text := ReadCloseAll(src)
return NewReadCloser(text), NewReadCloser(text)
}
// Shortcut for `os.Getwd` that panics on error.
func Cwd() string { return Try1(os.Getwd()) }
// If the given closer is non-nil, closes it. Panics on error.
func Close(val io.Closer) {
if val != nil {
Try(val.Close())
}
}
// Shortcut for `os.MkdirAll` with `os.ModePerm`. Panics on error.
func MkdirAll(path string) { Try(os.MkdirAll(path, os.ModePerm)) }
// Shortcut for `os.Stat` that panics on error.
func Stat(path string) fs.FileInfo { return Try1(os.Stat(path)) }
/*
Shortcut for writing the given text to the given `io.Writer`.
Automatically converts text to bytes and panics on errors.
*/
func Write[Out io.Writer, Src Text](out Out, src Src) {
Try1(out.Write(ToBytes(src)))
}
// https://en.wikipedia.org/wiki/ANSI_escape_code
const (
// Standard terminal escape sequence. Same as "\x1b" or "\033".
TermEsc = string(rune(27))
// Control Sequence Introducer. Used for other codes.
TermEscCsi = TermEsc + `[`
// Update cursor position to first row, first column.
TermEscCup = TermEscCsi + `1;1H`
// Supposed to clear the screen without clearing the scrollback, aka soft
// clear. Seems insufficient on its own, at least in some terminals.
TermEscErase2 = TermEscCsi + `2J`
// Supposed to clear the screen and the scrollback, aka hard clear. Seems
// insufficient on its own, at least in some terminals.
TermEscErase3 = TermEscCsi + `3J`
// Supposed to reset the terminal to initial state, aka super hard clear.
// Seems insufficient on its own, at least in some terminals.
TermEscReset = TermEsc + `c`
// Clear screen without clearing scrollback.
TermEscClearSoft = TermEscCup + TermEscErase2
// Clear screen AND scrollback.
TermEscClearHard = TermEscCup + TermEscReset + TermEscErase3
)
/*
Prints `TermEscClearSoft` to `os.Stdout`, causing the current TTY to clear the
screen but not the scrollback, pushing existing content out of view.
*/
func TermClearSoft() { _, _ = io.WriteString(os.Stdout, TermEscClearSoft) }
/*
Prints `TermEscClearHard` to `os.Stdout`, clearing the current TTY completely
(both screen and scrollback).
*/
func TermClearHard() { _, _ = io.WriteString(os.Stdout, TermEscClearHard) }