Skip to content

Commit

Permalink
feat: add peerflix-style interactive select mode (#234)
Browse files Browse the repository at this point in the history
* feat: add peerflix-style interactive select mode

* Handle potential errors from inquirer

* Fix formatting
  • Loading branch information
alexose committed Jun 2, 2022
1 parent 7e822d8 commit 4540a0a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 28 additions & 1 deletion bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import createTorrent from 'create-torrent'
import ecstatic from 'ecstatic'
import fs from 'fs'
import http from 'http'
import inquirer from 'inquirer'
import mime from 'mime'
import moment from 'moment'
import networkAddress from 'network-address'
Expand Down Expand Up @@ -42,6 +43,7 @@ const options = {
simple: {
o: { alias: 'out', desc: 'Set download destination', type: 'string', requiresArg: true },
s: { alias: 'select', desc: 'Select specific file in torrent', defaultDescription: 'List files' },
i: { alias: 'interactive-select', desc: 'Interactively select specific file in torrent', type: 'boolean' },
t: { alias: 'subtitles', desc: 'Load subtitles file', type: 'string', requiresArg: true }
},
advanced: {
Expand Down Expand Up @@ -370,7 +372,7 @@ async function runDownload (torrentId) {
}
}

function onReady () {
async function onReady () {
if (argv.select && typeof argv.select !== 'number') {
console.log('Select a file to download:')

Expand All @@ -385,6 +387,31 @@ async function runDownload (torrentId) {
return gracefulExit()
}

if (argv['interactive-select'] && torrent.files.length > 1) {
const paths = torrent.files.map(d => d.path)
const answers = await inquirer.prompt([{
type: 'list',
name: 'file',
message: 'Choose one file',
choices: Array.from(torrent.files)
.sort((file1, file2) => file1.path.localeCompare(file2.path))
.map(function (file, i) {
return {
name: file.name + ' : ' + prettierBytes(file.length),
value: paths.indexOf(file.path)
}
})
}])
.catch(err => {
if (err.isTtyError) {
return errorAndExit('Could not render interactive selection mode in this terminal.')
} else {
return errorAndExit('Could not start interactive selection mode: ' + err)
}
})
argv.select = answers.file
}

// if no index specified, use largest file
const index = (typeof argv.select === 'number')
? argv.select
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"create-torrent": "^5.0.0",
"dlnacasts": "^0.1.0",
"ecstatic": "^4.1.4",
"inquirer": "^8.2.2",
"memory-chunk-store": "^1.3.5",
"mime": "^3.0.0",
"moment": "^2.29.1",
Expand All @@ -28,9 +29,9 @@
"parse-torrent": "^9.1.3",
"prettier-bytes": "^1.0.4",
"vlc-command": "^1.2.0",
"yargs": "^17.0.1",
"webtorrent": "^1.3.2",
"winreg": "^1.2.4"
"winreg": "^1.2.4",
"yargs": "^17.0.1"
},
"devDependencies": {
"@webtorrent/semantic-release-config": "1.0.7",
Expand Down

0 comments on commit 4540a0a

Please sign in to comment.