-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhacker_effects.go
More file actions
342 lines (291 loc) · 8.52 KB
/
hacker_effects.go
File metadata and controls
342 lines (291 loc) · 8.52 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"math"
"math/rand"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/charmbracelet/lipgloss"
)
type HackerEffects struct {
MatrixRain []MatrixColumn
TypeWriter TypeWriterEffect
GlitchEffect GlitchState
ASCIISpinners map[string]*spinner.Spinner
LastUpdate time.Time
}
type MatrixColumn struct {
X int
Chars []rune
Head int
Speed float64
Length int
}
type TypeWriterEffect struct {
Text string
Position int
Speed time.Duration
LastUpdate time.Time
Complete bool
}
type GlitchState struct {
Text string
GlitchIntensity float64
LastGlitch time.Time
}
func NewHackerEffects(width, height int) *HackerEffects {
h := &HackerEffects{
MatrixRain: make([]MatrixColumn, width/2), // Sparse columns
ASCIISpinners: make(map[string]*spinner.Spinner),
LastUpdate: time.Now(),
}
// Initialize matrix columns
for i := range h.MatrixRain {
h.MatrixRain[i] = MatrixColumn{
X: i * 2,
Chars: make([]rune, height),
Head: rand.Intn(height),
Speed: 0.5 + rand.Float64()*2, // 0.5-2.5 chars per second
Length: 5 + rand.Intn(15), // 5-20 chars long
}
h.generateMatrixChars(&h.MatrixRain[i], height)
}
// Initialize ASCII spinners (monochrome only)
h.initSpinners()
return h
}
func (h *HackerEffects) initSpinners() {
// Hacker-friendly monochrome spinners
spinnerSets := map[string][]string{
"dots": {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
"line": {"|", "/", "-", "\\"},
"arrows": {"←", "↖", "↑", "↗", "→", "↘", "↓", "↙"},
"blocks": {"▁", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃"},
"bounce": {"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"},
"pulse": {"●", "◐", "◑", "◒", "◓", "◔", "◕", "○"},
"binary": {"0", "1"},
"matrix": {"日", "a", "Z", "3", "ハ", "ミ", "ヒ", "ー", "ウ", "シ", "ナ", "モ", "ニ", "サ", "ワ", "ツ", "オ", "リ", "ア", "ホ", "テ", "マ", "ケ", "メ", "エ", "カ", "キ", "ム", "ユ", "ラ", "セ", "ネ", "ス", "タ", "ヌ", "ヘ"},
"scanner": {"[ ]", "[= ]", "[== ]", "[=== ]", "[====]", "[ ===]", "[ ==]", "[ =]"},
"wave": {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃", "▂"},
}
for name, charset := range spinnerSets {
s := spinner.New(charset, 100*time.Millisecond)
s.Color("white") // Monochrome
h.ASCIISpinners[name] = s
}
}
func (h *HackerEffects) generateMatrixChars(col *MatrixColumn, height int) {
// Classic Matrix characters: katakana, latin, numbers
matrixChars := []rune{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '=',
'[', ']', '{', '}', '|', '\\', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/',
}
for i := range col.Chars {
col.Chars[i] = matrixChars[rand.Intn(len(matrixChars))]
}
}
func (h *HackerEffects) Update(width, height int) {
now := time.Now()
dt := now.Sub(h.LastUpdate).Seconds()
h.LastUpdate = now
// Update matrix rain
for i := range h.MatrixRain {
col := &h.MatrixRain[i]
col.Head += int(col.Speed * dt * 10) // Scale for visibility
if col.Head > height+col.Length {
col.Head = -col.Length
col.Speed = 0.5 + rand.Float64()*2
h.generateMatrixChars(col, height)
}
}
// Update typewriter
if !h.TypeWriter.Complete && now.Sub(h.TypeWriter.LastUpdate) > h.TypeWriter.Speed {
if h.TypeWriter.Position < len(h.TypeWriter.Text) {
h.TypeWriter.Position++
h.TypeWriter.LastUpdate = now
} else {
h.TypeWriter.Complete = true
}
}
// Update glitch effect
if now.Sub(h.GlitchEffect.LastGlitch) > 100*time.Millisecond {
h.GlitchEffect.GlitchIntensity = rand.Float64() * 0.3 // Max 30% glitch
h.GlitchEffect.LastGlitch = now
}
}
func (h *HackerEffects) RenderMatrixRain(width, height int) string {
if len(h.MatrixRain) == 0 {
return ""
}
// Create grid
grid := make([][]rune, height)
for i := range grid {
grid[i] = make([]rune, width)
for j := range grid[i] {
grid[i][j] = ' '
}
}
// Render columns
for _, col := range h.MatrixRain {
if col.X >= width {
continue
}
for i := 0; i < col.Length; i++ {
y := col.Head - i
if y >= 0 && y < height {
charIndex := (y + i) % len(col.Chars)
// Fade effect: head is brighter
intensity := float64(col.Length-i) / float64(col.Length)
if intensity > 0.3 {
grid[y][col.X] = col.Chars[charIndex]
} else if intensity > 0.1 && rand.Float64() < intensity {
grid[y][col.X] = col.Chars[charIndex]
}
}
}
}
// Convert grid to string
var result strings.Builder
style := lipgloss.NewStyle().Foreground(lipgloss.Color("8")) // Dark gray for background effect
for _, row := range grid {
line := string(row)
if strings.TrimSpace(line) != "" {
result.WriteString(style.Render(line) + "\n")
} else {
result.WriteString("\n")
}
}
return result.String()
}
func (h *HackerEffects) StartTypeWriter(text string, speed time.Duration) {
h.TypeWriter = TypeWriterEffect{
Text: text,
Position: 0,
Speed: speed,
LastUpdate: time.Now(),
Complete: false,
}
}
func (h *HackerEffects) RenderTypeWriter() string {
if h.TypeWriter.Position == 0 {
return ""
}
displayed := h.TypeWriter.Text[:h.TypeWriter.Position]
// Add cursor if not complete
if !h.TypeWriter.Complete {
cursor := "▋"
if time.Now().UnixNano()/500000000%2 == 0 { // Blink every 500ms
cursor = " "
}
displayed += cursor
}
return displayed
}
func (h *HackerEffects) ApplyGlitchEffect(text string) string {
if h.GlitchEffect.GlitchIntensity == 0 {
return text
}
glitchChars := []rune{'#', '@', '&', '%', '$', '!', '?', '*', '+', '=', '~'}
result := []rune(text)
for i := range result {
if rand.Float64() < h.GlitchEffect.GlitchIntensity {
result[i] = glitchChars[rand.Intn(len(glitchChars))]
}
}
return string(result)
}
func (h *HackerEffects) GetSpinner(name string) string {
if s, exists := h.ASCIISpinners[name]; exists {
return s.Prefix
}
return "●"
}
func (h *HackerEffects) CreateASCIIChart(values []float64, width, height int) string {
if len(values) == 0 {
return ""
}
// Find min/max for scaling
min, max := values[0], values[0]
for _, v := range values {
if v < min {
min = v
}
if v > max {
max = v
}
}
var result strings.Builder
chartChars := []rune{' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
// Render from top to bottom
for y := height - 1; y >= 0; y-- {
for x := 0; x < width && x < len(values); x++ {
// Normalize value to chart height
normalized := (values[x] - min) / (max - min)
valueHeight := int(normalized * float64(height))
if valueHeight > y {
// Calculate which character to use based on partial fill
charIndex := len(chartChars) - 1
if valueHeight == y+1 {
// Partial character for smooth effect
partial := (normalized*float64(height) - float64(y)) * float64(len(chartChars)-1)
charIndex = int(partial)
if charIndex >= len(chartChars) {
charIndex = len(chartChars) - 1
}
}
result.WriteRune(chartChars[charIndex])
} else {
result.WriteRune(' ')
}
}
result.WriteRune('\n')
}
return result.String()
}
func (h *HackerEffects) CreateWaveEffect(text string, phase float64) string {
result := []rune(text)
for i, char := range result {
if char == ' ' {
continue
}
// Create wave displacement
wave := math.Sin(float64(i)*0.5 + phase)
if wave > 0.3 {
// Make character "float up"
result[i] = char
} else if wave > -0.3 {
// Normal position
result[i] = char
} else {
// Make character "sink down"
result[i] = rune(strings.ToLower(string(char))[0])
}
}
return string(result)
}
func (h *HackerEffects) CreateScanlineEffect(text string, position int) string {
lines := strings.Split(text, "\n")
for i, line := range lines {
if i == position%len(lines) {
// Highlight current scanline
style := lipgloss.NewStyle().Reverse(true)
lines[i] = style.Render(line)
} else if abs(i-position%len(lines)) == 1 {
// Dim adjacent lines
style := lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
lines[i] = style.Render(line)
}
}
return strings.Join(lines, "\n")
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}