@ecmaos/coreutils 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +26 -0
- package/dist/commands/cron.d.ts +4 -0
- package/dist/commands/cron.d.ts.map +1 -0
- package/dist/commands/cron.js +439 -0
- package/dist/commands/cron.js.map +1 -0
- package/dist/commands/man.d.ts.map +1 -1
- package/dist/commands/man.js +10 -0
- package/dist/commands/man.js.map +1 -1
- package/dist/commands/open.d.ts +4 -0
- package/dist/commands/open.d.ts.map +1 -0
- package/dist/commands/open.js +74 -0
- package/dist/commands/open.js.map +1 -0
- package/dist/commands/play.d.ts +4 -0
- package/dist/commands/play.d.ts.map +1 -0
- package/dist/commands/play.js +231 -0
- package/dist/commands/play.js.map +1 -0
- package/dist/commands/tar.d.ts.map +1 -1
- package/dist/commands/tar.js +67 -17
- package/dist/commands/tar.js.map +1 -1
- package/dist/commands/video.d.ts +4 -0
- package/dist/commands/video.d.ts.map +1 -0
- package/dist/commands/video.js +250 -0
- package/dist/commands/video.js.map +1 -0
- package/dist/commands/view.d.ts +4 -0
- package/dist/commands/view.d.ts.map +1 -0
- package/dist/commands/view.js +488 -0
- package/dist/commands/view.js.map +1 -0
- package/dist/commands/web.d.ts +4 -0
- package/dist/commands/web.d.ts.map +1 -0
- package/dist/commands/web.js +348 -0
- package/dist/commands/web.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
- package/src/commands/cron.ts +499 -0
- package/src/commands/man.ts +8 -0
- package/src/commands/open.ts +84 -0
- package/src/commands/play.ts +249 -0
- package/src/commands/tar.ts +62 -19
- package/src/commands/video.ts +267 -0
- package/src/commands/view.ts +526 -0
- package/src/commands/web.ts +377 -0
- package/src/index.ts +20 -2
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import chalk from 'chalk'
|
|
3
|
+
import type { Kernel, Process, Shell, Terminal } from '@ecmaos/types'
|
|
4
|
+
import { TerminalCommand } from '../shared/terminal-command.js'
|
|
5
|
+
import { writelnStderr, writelnStdout } from '../shared/helpers.js'
|
|
6
|
+
|
|
7
|
+
function printUsage(process: Process | undefined, terminal: Terminal): void {
|
|
8
|
+
const usage = `Usage: play [OPTIONS] [FILE...]
|
|
9
|
+
Play an audio file.
|
|
10
|
+
|
|
11
|
+
--help display this help and exit
|
|
12
|
+
--no-autoplay don't start playing automatically
|
|
13
|
+
--loop loop the audio
|
|
14
|
+
--muted start muted
|
|
15
|
+
--volume <0-100> set volume (0-100, default: 100)
|
|
16
|
+
--quiet play without opening a window (background playback)
|
|
17
|
+
|
|
18
|
+
Examples:
|
|
19
|
+
play song.mp3 play an audio file
|
|
20
|
+
play --loop music.mp3 play audio in a loop
|
|
21
|
+
play --no-autoplay audio.mp3 load audio without auto-playing
|
|
22
|
+
play --volume 50 track.mp3 play at 50% volume
|
|
23
|
+
play --quiet background.mp3 play audio in background
|
|
24
|
+
play song1.mp3 song2.mp3 play multiple audio files`
|
|
25
|
+
writelnStderr(process, terminal, usage)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getMimeType(filePath: string): string {
|
|
29
|
+
const ext = path.extname(filePath).toLowerCase()
|
|
30
|
+
const mimeTypes: Record<string, string> = {
|
|
31
|
+
'.mp3': 'audio/mpeg',
|
|
32
|
+
'.wav': 'audio/wav',
|
|
33
|
+
'.ogg': 'audio/ogg',
|
|
34
|
+
'.oga': 'audio/ogg',
|
|
35
|
+
'.opus': 'audio/opus',
|
|
36
|
+
'.m4a': 'audio/mp4',
|
|
37
|
+
'.aac': 'audio/aac',
|
|
38
|
+
'.flac': 'audio/flac',
|
|
39
|
+
'.webm': 'audio/webm',
|
|
40
|
+
'.wma': 'audio/x-ms-wma',
|
|
41
|
+
'.aiff': 'audio/aiff',
|
|
42
|
+
'.aif': 'audio/aiff',
|
|
43
|
+
'.3gp': 'audio/3gpp',
|
|
44
|
+
'.amr': 'audio/amr'
|
|
45
|
+
}
|
|
46
|
+
return mimeTypes[ext] || 'audio/mpeg'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function loadAudioMetadata(audioElement: HTMLAudioElement): Promise<{ duration: number }> {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const timeout = setTimeout(() => {
|
|
52
|
+
reject(new Error('Timeout loading audio metadata'))
|
|
53
|
+
}, 10000)
|
|
54
|
+
|
|
55
|
+
audioElement.onloadedmetadata = () => {
|
|
56
|
+
clearTimeout(timeout)
|
|
57
|
+
resolve({
|
|
58
|
+
duration: audioElement.duration
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
audioElement.onerror = () => {
|
|
63
|
+
clearTimeout(timeout)
|
|
64
|
+
reject(new Error('Failed to load audio metadata'))
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function formatDuration(seconds: number): string {
|
|
70
|
+
if (!isFinite(seconds) || isNaN(seconds)) return '?:??'
|
|
71
|
+
const hours = Math.floor(seconds / 3600)
|
|
72
|
+
const minutes = Math.floor((seconds % 3600) / 60)
|
|
73
|
+
const secs = Math.floor(seconds % 60)
|
|
74
|
+
|
|
75
|
+
if (hours > 0) {
|
|
76
|
+
return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
|
|
77
|
+
}
|
|
78
|
+
return `${minutes}:${secs.toString().padStart(2, '0')}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function createCommand(kernel: Kernel, shell: Shell, terminal: Terminal): TerminalCommand {
|
|
82
|
+
return new TerminalCommand({
|
|
83
|
+
command: 'play',
|
|
84
|
+
description: 'Play an audio file',
|
|
85
|
+
kernel,
|
|
86
|
+
shell,
|
|
87
|
+
terminal,
|
|
88
|
+
run: async (pid: number, argv: string[]) => {
|
|
89
|
+
const process = kernel.processes.get(pid) as Process | undefined
|
|
90
|
+
|
|
91
|
+
if (!process) return 1
|
|
92
|
+
|
|
93
|
+
if (argv.length > 0 && (argv[0] === '--help' || argv[0] === '-h')) {
|
|
94
|
+
printUsage(process, terminal)
|
|
95
|
+
return 0
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Parse options
|
|
99
|
+
const options: {
|
|
100
|
+
autoplay: boolean
|
|
101
|
+
loop: boolean
|
|
102
|
+
muted: boolean
|
|
103
|
+
volume: number
|
|
104
|
+
quiet: boolean
|
|
105
|
+
} = {
|
|
106
|
+
autoplay: true,
|
|
107
|
+
loop: false,
|
|
108
|
+
muted: false,
|
|
109
|
+
volume: 100,
|
|
110
|
+
quiet: false
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const files: string[] = []
|
|
114
|
+
|
|
115
|
+
for (let i = 0; i < argv.length; i++) {
|
|
116
|
+
const arg = argv[i]
|
|
117
|
+
if (arg === '--no-autoplay') {
|
|
118
|
+
options.autoplay = false
|
|
119
|
+
} else if (arg === '--loop') {
|
|
120
|
+
options.loop = true
|
|
121
|
+
} else if (arg === '--muted') {
|
|
122
|
+
options.muted = true
|
|
123
|
+
} else if (arg === '--quiet') {
|
|
124
|
+
options.quiet = true
|
|
125
|
+
} else if (arg === '--volume' && i + 1 < argv.length) {
|
|
126
|
+
const volumeArg = argv[i + 1]
|
|
127
|
+
if (!volumeArg) {
|
|
128
|
+
await writelnStderr(process, terminal, chalk.red(`play: missing volume value`))
|
|
129
|
+
return 1
|
|
130
|
+
}
|
|
131
|
+
const volume = parseFloat(volumeArg)
|
|
132
|
+
if (isNaN(volume) || volume < 0 || volume > 100) {
|
|
133
|
+
await writelnStderr(process, terminal, chalk.red(`play: invalid volume: ${volumeArg} (must be 0-100)`))
|
|
134
|
+
return 1
|
|
135
|
+
}
|
|
136
|
+
options.volume = volume
|
|
137
|
+
i++ // Skip next argument
|
|
138
|
+
} else if (arg && !arg.startsWith('--')) {
|
|
139
|
+
files.push(arg)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (files.length === 0) {
|
|
144
|
+
await writelnStderr(process, terminal, `play: missing file argument`)
|
|
145
|
+
await writelnStderr(process, terminal, `Try 'play --help' for more information.`)
|
|
146
|
+
return 1
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Process each audio file
|
|
150
|
+
for (const file of files) {
|
|
151
|
+
const fullPath = path.resolve(shell.cwd, file)
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
// Check if file exists
|
|
155
|
+
if (!(await shell.context.fs.promises.exists(fullPath))) {
|
|
156
|
+
await writelnStderr(process, terminal, chalk.red(`play: file not found: ${fullPath}`))
|
|
157
|
+
continue
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Read file
|
|
161
|
+
await writelnStdout(process, terminal, chalk.blue(`Loading audio: ${file}...`))
|
|
162
|
+
const fileData = await shell.context.fs.promises.readFile(fullPath)
|
|
163
|
+
const mimeType = getMimeType(fullPath)
|
|
164
|
+
const blob = new Blob([new Uint8Array(fileData)], { type: mimeType })
|
|
165
|
+
const url = URL.createObjectURL(blob)
|
|
166
|
+
|
|
167
|
+
// Load audio metadata
|
|
168
|
+
const audioElement = document.createElement('audio')
|
|
169
|
+
audioElement.src = url
|
|
170
|
+
audioElement.preload = 'metadata'
|
|
171
|
+
|
|
172
|
+
let duration = 0
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
const metadata = await loadAudioMetadata(audioElement)
|
|
176
|
+
duration = metadata.duration
|
|
177
|
+
} catch (error) {
|
|
178
|
+
await writelnStderr(process, terminal, chalk.yellow(`play: warning: could not load metadata for ${file}`))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Set audio properties
|
|
182
|
+
audioElement.volume = options.volume / 100
|
|
183
|
+
if (options.autoplay) audioElement.autoplay = true
|
|
184
|
+
if (options.loop) audioElement.loop = true
|
|
185
|
+
if (options.muted) audioElement.muted = true
|
|
186
|
+
|
|
187
|
+
if (options.quiet) {
|
|
188
|
+
// Background playback - no window
|
|
189
|
+
audioElement.play().catch((error) => {
|
|
190
|
+
// Autoplay may be blocked by browser, but that's okay for quiet mode
|
|
191
|
+
console.warn('Autoplay blocked:', error)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
if (duration > 0) {
|
|
195
|
+
const durationStr = formatDuration(duration)
|
|
196
|
+
await writelnStdout(process, terminal, chalk.green(`Playing in background: ${file} (${durationStr})`))
|
|
197
|
+
} else {
|
|
198
|
+
await writelnStdout(process, terminal, chalk.green(`Playing in background: ${file}`))
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
// Create a simple audio player window
|
|
202
|
+
const windowTitle = files.length > 1
|
|
203
|
+
? `${path.basename(file)} (${files.indexOf(file) + 1}/${files.length})`
|
|
204
|
+
: path.basename(file)
|
|
205
|
+
|
|
206
|
+
// Create audio player HTML with controls
|
|
207
|
+
const audioId = `audio-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
|
|
208
|
+
const audioHtml = `
|
|
209
|
+
<div style="width:100%;height:100%;display:flex;flex-direction:column;align-items:center;justify-content:center;background:#1e1e1e;color:#fff;font-family:monospace;padding:20px;box-sizing:border-box;">
|
|
210
|
+
<div style="font-size:18px;margin-bottom:20px;text-align:center;word-break:break-word;">${path.basename(file)}</div>
|
|
211
|
+
<audio id="${audioId}" src="${url}" ${options.autoplay ? 'autoplay' : ''} ${options.loop ? 'loop' : ''} ${options.muted ? 'muted' : ''} controls style="width:100%;max-width:600px;"></audio>
|
|
212
|
+
</div>
|
|
213
|
+
<script>
|
|
214
|
+
(function() {
|
|
215
|
+
const audio = document.getElementById('${audioId}');
|
|
216
|
+
if (audio) {
|
|
217
|
+
audio.volume = ${options.volume / 100};
|
|
218
|
+
audio.addEventListener('play', () => console.log('Playing: ${file}'));
|
|
219
|
+
audio.addEventListener('ended', () => console.log('Finished: ${file}'));
|
|
220
|
+
}
|
|
221
|
+
})();
|
|
222
|
+
</script>
|
|
223
|
+
`
|
|
224
|
+
|
|
225
|
+
kernel.windows.create({
|
|
226
|
+
title: windowTitle,
|
|
227
|
+
html: audioHtml,
|
|
228
|
+
width: 500,
|
|
229
|
+
height: 200,
|
|
230
|
+
max: false
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
if (duration > 0) {
|
|
234
|
+
const durationStr = formatDuration(duration)
|
|
235
|
+
await writelnStdout(process, terminal, chalk.green(`Playing: ${file} (${durationStr})`))
|
|
236
|
+
} else {
|
|
237
|
+
await writelnStdout(process, terminal, chalk.green(`Playing: ${file}`))
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} catch (error) {
|
|
241
|
+
await writelnStderr(process, terminal, chalk.red(`play: error playing ${file}: ${error instanceof Error ? error.message : 'Unknown error'}`))
|
|
242
|
+
return 1
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return 0
|
|
247
|
+
}
|
|
248
|
+
})
|
|
249
|
+
}
|
package/src/commands/tar.ts
CHANGED
|
@@ -89,27 +89,70 @@ function parseArgs(argv: string[]): { options: TarOptions; files: string[] } {
|
|
|
89
89
|
i++
|
|
90
90
|
} else if (arg.startsWith('-')) {
|
|
91
91
|
// Handle combined flags like -czf
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
const flagString = arg.slice(1)
|
|
93
|
+
let flagIndex = 0
|
|
94
|
+
while (flagIndex < flagString.length) {
|
|
95
|
+
const flag = flagString[flagIndex]
|
|
96
|
+
if (flag === 'c') {
|
|
97
|
+
options.create = true
|
|
98
|
+
flagIndex++
|
|
99
|
+
} else if (flag === 'x') {
|
|
100
|
+
options.extract = true
|
|
101
|
+
flagIndex++
|
|
102
|
+
} else if (flag === 't') {
|
|
103
|
+
options.list = true
|
|
104
|
+
flagIndex++
|
|
105
|
+
} else if (flag === 'f') {
|
|
106
|
+
// -f needs to be followed by filename
|
|
107
|
+
// Check if there's a path in the same string after 'f'
|
|
108
|
+
const remaining = flagString.slice(flagIndex + 1)
|
|
109
|
+
if (remaining.length > 0 && !remaining.startsWith('-')) {
|
|
110
|
+
// Path is in the same string
|
|
111
|
+
options.file = remaining
|
|
112
|
+
flagIndex = flagString.length // Done processing this arg
|
|
113
|
+
} else if (i + 1 < argv.length) {
|
|
114
|
+
// Check next argument
|
|
115
|
+
const nextArg = argv[i + 1]
|
|
116
|
+
if (typeof nextArg === 'string' && !nextArg.startsWith('-')) {
|
|
117
|
+
i++
|
|
118
|
+
options.file = nextArg
|
|
119
|
+
flagIndex++
|
|
120
|
+
} else {
|
|
121
|
+
flagIndex++
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
flagIndex++
|
|
103
125
|
}
|
|
104
|
-
} else if (flag === 'z')
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
126
|
+
} else if (flag === 'z') {
|
|
127
|
+
options.gzip = true
|
|
128
|
+
flagIndex++
|
|
129
|
+
} else if (flag === 'v') {
|
|
130
|
+
options.verbose = true
|
|
131
|
+
flagIndex++
|
|
132
|
+
} else if (flag === 'C') {
|
|
133
|
+
// -C needs to be followed by directory
|
|
134
|
+
// Check if there's a path in the same string after 'C'
|
|
135
|
+
const remaining = flagString.slice(flagIndex + 1)
|
|
136
|
+
if (remaining.length > 0 && !remaining.startsWith('-')) {
|
|
137
|
+
// Path is in the same string (e.g., -xz-C/tmp/dir)
|
|
138
|
+
options.directory = remaining
|
|
139
|
+
flagIndex = flagString.length // Done processing this arg
|
|
140
|
+
} else if (i + 1 < argv.length) {
|
|
141
|
+
// Check next argument
|
|
142
|
+
const nextArg = argv[i + 1]
|
|
143
|
+
if (typeof nextArg === 'string' && !nextArg.startsWith('-')) {
|
|
144
|
+
i++
|
|
145
|
+
options.directory = nextArg
|
|
146
|
+
flagIndex++
|
|
147
|
+
} else {
|
|
148
|
+
flagIndex++
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
flagIndex++
|
|
112
152
|
}
|
|
153
|
+
} else {
|
|
154
|
+
// Unknown flag, skip it
|
|
155
|
+
flagIndex++
|
|
113
156
|
}
|
|
114
157
|
}
|
|
115
158
|
i++
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import chalk from 'chalk'
|
|
3
|
+
import type { Kernel, Process, Shell, Terminal } from '@ecmaos/types'
|
|
4
|
+
import { TerminalCommand } from '../shared/terminal-command.js'
|
|
5
|
+
import { writelnStderr, writelnStdout } from '../shared/helpers.js'
|
|
6
|
+
|
|
7
|
+
function printUsage(process: Process | undefined, terminal: Terminal): void {
|
|
8
|
+
const usage = `Usage: video [OPTIONS] [FILE...]
|
|
9
|
+
Play a video file in a window.
|
|
10
|
+
|
|
11
|
+
--help display this help and exit
|
|
12
|
+
--no-autoplay don't start playing automatically
|
|
13
|
+
--no-controls hide video controls
|
|
14
|
+
--loop loop the video
|
|
15
|
+
--muted start muted
|
|
16
|
+
--fullscreen open in fullscreen mode
|
|
17
|
+
--width <width> set window width (default: video width or screen width)
|
|
18
|
+
--height <height> set window height (default: video height or screen height)
|
|
19
|
+
|
|
20
|
+
Examples:
|
|
21
|
+
video movie.mp4 play a video file
|
|
22
|
+
video --loop clip.mp4 play a video in a loop
|
|
23
|
+
video --no-autoplay video.mp4 load video without auto-playing
|
|
24
|
+
video --fullscreen movie.mp4 play video in fullscreen mode
|
|
25
|
+
video video1.mp4 video2.mp4 play multiple videos`
|
|
26
|
+
writelnStderr(process, terminal, usage)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getMimeType(filePath: string): string {
|
|
30
|
+
const ext = path.extname(filePath).toLowerCase()
|
|
31
|
+
const mimeTypes: Record<string, string> = {
|
|
32
|
+
'.mp4': 'video/mp4',
|
|
33
|
+
'.webm': 'video/webm',
|
|
34
|
+
'.ogg': 'video/ogg',
|
|
35
|
+
'.ogv': 'video/ogg',
|
|
36
|
+
'.mov': 'video/quicktime',
|
|
37
|
+
'.avi': 'video/x-msvideo',
|
|
38
|
+
'.mkv': 'video/x-matroska',
|
|
39
|
+
'.m4v': 'video/mp4',
|
|
40
|
+
'.flv': 'video/x-flv',
|
|
41
|
+
'.wmv': 'video/x-ms-wmv',
|
|
42
|
+
'.3gp': 'video/3gpp'
|
|
43
|
+
}
|
|
44
|
+
return mimeTypes[ext] || 'video/mp4'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function loadVideoMetadata(videoElement: HTMLVideoElement): Promise<{ width: number; height: number; duration: number }> {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const timeout = setTimeout(() => {
|
|
50
|
+
reject(new Error('Timeout loading video metadata'))
|
|
51
|
+
}, 10000)
|
|
52
|
+
|
|
53
|
+
videoElement.onloadedmetadata = () => {
|
|
54
|
+
clearTimeout(timeout)
|
|
55
|
+
resolve({
|
|
56
|
+
width: videoElement.videoWidth,
|
|
57
|
+
height: videoElement.videoHeight,
|
|
58
|
+
duration: videoElement.duration
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
videoElement.onerror = () => {
|
|
63
|
+
clearTimeout(timeout)
|
|
64
|
+
reject(new Error('Failed to load video metadata'))
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function createCommand(kernel: Kernel, shell: Shell, terminal: Terminal): TerminalCommand {
|
|
70
|
+
return new TerminalCommand({
|
|
71
|
+
command: 'video',
|
|
72
|
+
description: 'Play a video file',
|
|
73
|
+
kernel,
|
|
74
|
+
shell,
|
|
75
|
+
terminal,
|
|
76
|
+
run: async (pid: number, argv: string[]) => {
|
|
77
|
+
const process = kernel.processes.get(pid) as Process | undefined
|
|
78
|
+
|
|
79
|
+
if (!process) return 1
|
|
80
|
+
|
|
81
|
+
if (argv.length > 0 && (argv[0] === '--help' || argv[0] === '-h')) {
|
|
82
|
+
printUsage(process, terminal)
|
|
83
|
+
return 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Parse options
|
|
87
|
+
const options: {
|
|
88
|
+
autoplay: boolean
|
|
89
|
+
controls: boolean
|
|
90
|
+
loop: boolean
|
|
91
|
+
muted: boolean
|
|
92
|
+
fullscreen: boolean
|
|
93
|
+
width?: number
|
|
94
|
+
height?: number
|
|
95
|
+
} = {
|
|
96
|
+
autoplay: true,
|
|
97
|
+
controls: true,
|
|
98
|
+
loop: false,
|
|
99
|
+
muted: false,
|
|
100
|
+
fullscreen: false
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const files: string[] = []
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < argv.length; i++) {
|
|
106
|
+
const arg = argv[i]
|
|
107
|
+
if (arg === '--no-autoplay') {
|
|
108
|
+
options.autoplay = false
|
|
109
|
+
} else if (arg === '--no-controls') {
|
|
110
|
+
options.controls = false
|
|
111
|
+
} else if (arg === '--loop') {
|
|
112
|
+
options.loop = true
|
|
113
|
+
} else if (arg === '--muted') {
|
|
114
|
+
options.muted = true
|
|
115
|
+
} else if (arg === '--fullscreen') {
|
|
116
|
+
options.fullscreen = true
|
|
117
|
+
} else if (arg === '--width' && i + 1 < argv.length) {
|
|
118
|
+
const widthArg = argv[i + 1]
|
|
119
|
+
if (!widthArg) {
|
|
120
|
+
await writelnStderr(process, terminal, chalk.red(`video: missing width value`))
|
|
121
|
+
return 1
|
|
122
|
+
}
|
|
123
|
+
const width = parseInt(widthArg, 10)
|
|
124
|
+
if (isNaN(width) || width <= 0) {
|
|
125
|
+
await writelnStderr(process, terminal, chalk.red(`video: invalid width: ${widthArg}`))
|
|
126
|
+
return 1
|
|
127
|
+
}
|
|
128
|
+
options.width = width
|
|
129
|
+
i++ // Skip next argument
|
|
130
|
+
} else if (arg === '--height' && i + 1 < argv.length) {
|
|
131
|
+
const heightArg = argv[i + 1]
|
|
132
|
+
if (!heightArg) {
|
|
133
|
+
await writelnStderr(process, terminal, chalk.red(`video: missing height value`))
|
|
134
|
+
return 1
|
|
135
|
+
}
|
|
136
|
+
const height = parseInt(heightArg, 10)
|
|
137
|
+
if (isNaN(height) || height <= 0) {
|
|
138
|
+
await writelnStderr(process, terminal, chalk.red(`video: invalid height: ${heightArg}`))
|
|
139
|
+
return 1
|
|
140
|
+
}
|
|
141
|
+
options.height = height
|
|
142
|
+
i++ // Skip next argument
|
|
143
|
+
} else if (arg && !arg.startsWith('--')) {
|
|
144
|
+
files.push(arg)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (files.length === 0) {
|
|
149
|
+
await writelnStderr(process, terminal, `video: missing file argument`)
|
|
150
|
+
await writelnStderr(process, terminal, `Try 'video --help' for more information.`)
|
|
151
|
+
return 1
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Process each video file
|
|
155
|
+
for (const file of files) {
|
|
156
|
+
const fullPath = path.resolve(shell.cwd, file)
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
// Check if file exists
|
|
160
|
+
if (!(await shell.context.fs.promises.exists(fullPath))) {
|
|
161
|
+
await writelnStderr(process, terminal, chalk.red(`video: file not found: ${fullPath}`))
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Read file
|
|
166
|
+
await writelnStdout(process, terminal, chalk.blue(`Loading video: ${file}...`))
|
|
167
|
+
const fileData = await shell.context.fs.promises.readFile(fullPath)
|
|
168
|
+
const mimeType = getMimeType(fullPath)
|
|
169
|
+
const blob = new Blob([new Uint8Array(fileData)], { type: mimeType })
|
|
170
|
+
const url = URL.createObjectURL(blob)
|
|
171
|
+
|
|
172
|
+
// Load video metadata
|
|
173
|
+
const videoElement = document.createElement('video')
|
|
174
|
+
videoElement.src = url
|
|
175
|
+
videoElement.preload = 'metadata'
|
|
176
|
+
|
|
177
|
+
let videoWidth: number
|
|
178
|
+
let videoHeight: number
|
|
179
|
+
let duration: number
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
const metadata = await loadVideoMetadata(videoElement)
|
|
183
|
+
videoWidth = metadata.width
|
|
184
|
+
videoHeight = metadata.height
|
|
185
|
+
duration = metadata.duration
|
|
186
|
+
} catch (error) {
|
|
187
|
+
await writelnStderr(process, terminal, chalk.yellow(`video: warning: could not load metadata for ${file}, using default size`))
|
|
188
|
+
videoWidth = 640
|
|
189
|
+
videoHeight = 360
|
|
190
|
+
duration = 0
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Calculate window dimensions
|
|
194
|
+
const { innerWidth, innerHeight } = window
|
|
195
|
+
let windowWidth: number
|
|
196
|
+
let windowHeight: number
|
|
197
|
+
|
|
198
|
+
if (options.fullscreen) {
|
|
199
|
+
windowWidth = innerWidth
|
|
200
|
+
windowHeight = innerHeight
|
|
201
|
+
} else if (options.width && options.height) {
|
|
202
|
+
windowWidth = options.width
|
|
203
|
+
windowHeight = options.height
|
|
204
|
+
} else if (options.width) {
|
|
205
|
+
windowWidth = options.width
|
|
206
|
+
windowHeight = Math.round((videoHeight / videoWidth) * windowWidth)
|
|
207
|
+
} else if (options.height) {
|
|
208
|
+
windowHeight = options.height
|
|
209
|
+
windowWidth = Math.round((videoWidth / videoHeight) * windowHeight)
|
|
210
|
+
} else {
|
|
211
|
+
// Auto-size: fit to screen if video is larger, otherwise use video dimensions
|
|
212
|
+
const scale = Math.min(innerWidth / videoWidth, innerHeight / videoHeight, 1)
|
|
213
|
+
windowWidth = Math.round(videoWidth * scale)
|
|
214
|
+
windowHeight = Math.round(videoHeight * scale)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Ensure minimum size
|
|
218
|
+
windowWidth = Math.max(windowWidth, 320)
|
|
219
|
+
windowHeight = Math.max(windowHeight, 180)
|
|
220
|
+
|
|
221
|
+
// Build video attributes
|
|
222
|
+
const videoAttrs: string[] = []
|
|
223
|
+
if (options.autoplay) videoAttrs.push('autoplay')
|
|
224
|
+
if (options.controls) videoAttrs.push('controls')
|
|
225
|
+
if (options.loop) videoAttrs.push('loop')
|
|
226
|
+
if (options.muted) videoAttrs.push('muted')
|
|
227
|
+
videoAttrs.push('style="width:100%;height:100%;object-fit:contain"')
|
|
228
|
+
|
|
229
|
+
const videoHtml = `<video src="${url}" ${videoAttrs.join(' ')}></video>`
|
|
230
|
+
|
|
231
|
+
// Create window
|
|
232
|
+
const windowTitle = files.length > 1
|
|
233
|
+
? `${path.basename(file)} (${files.indexOf(file) + 1}/${files.length})`
|
|
234
|
+
: path.basename(file)
|
|
235
|
+
|
|
236
|
+
kernel.windows.create({
|
|
237
|
+
title: windowTitle,
|
|
238
|
+
html: videoHtml,
|
|
239
|
+
width: windowWidth,
|
|
240
|
+
height: windowHeight,
|
|
241
|
+
max: options.fullscreen
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
// Clean up URL when window is closed (if possible)
|
|
245
|
+
// Note: This is a best-effort cleanup since we don't have direct access to window close events
|
|
246
|
+
setTimeout(() => {
|
|
247
|
+
// Cleanup after a delay to allow video to load
|
|
248
|
+
// In a real implementation, you'd want to hook into window close events
|
|
249
|
+
}, 1000)
|
|
250
|
+
|
|
251
|
+
if (duration > 0) {
|
|
252
|
+
const minutes = Math.floor(duration / 60)
|
|
253
|
+
const seconds = Math.floor(duration % 60)
|
|
254
|
+
await writelnStdout(process, terminal, chalk.green(`Playing: ${file} (${minutes}:${seconds.toString().padStart(2, '0')})`))
|
|
255
|
+
} else {
|
|
256
|
+
await writelnStdout(process, terminal, chalk.green(`Playing: ${file}`))
|
|
257
|
+
}
|
|
258
|
+
} catch (error) {
|
|
259
|
+
await writelnStderr(process, terminal, chalk.red(`video: error playing ${file}: ${error instanceof Error ? error.message : 'Unknown error'}`))
|
|
260
|
+
return 1
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return 0
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
}
|