« get me outta code hell

index.js - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/index.js
blob: 59c91eced494a68a7fcf6d6ee30eefb857e817ee (plain)
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
#!/usr/bin/env node

// omg I am tired of code

const { AppElement } = require('./ui')
const { updatePlaylistFormat } = require('./playlist-utils')
const { getAllCrawlersForArg } = require('./crawlers')
const fs = require('fs')
const util = require('util')
const processSmartPlaylist = require('./smart-playlist')
const ansi = require('./tui-lib/util/ansi')
const CommandLineInterfacer = require('./tui-lib/util/CommandLineInterfacer')
const EventEmitter = require('events')
const Flushable = require('./tui-lib/util/Flushable')
const Root = require('./tui-lib/ui/Root')

const readFile = util.promisify(fs.readFile)

// Hack to get around errors when piping many things to stdout/err
// (from general-util promisifyProcess)
process.stdout.setMaxListeners(Infinity)
process.stderr.setMaxListeners(Infinity)

process.on('unhandledRejection', error => {
  console.error(error.stack)
  process.exit(1)
})

async function main() {
  const interfacer = new CommandLineInterfacer()
  const size = await interfacer.getScreenSize()

  const root = new Root(interfacer)
  root.w = size.width
  root.h = size.height

  interfacer.on('resize', newSize => {
    root.w = newSize.width
    root.h = newSize.height
    flushable.resizeScreen(newSize)
    root.fixAllLayout()
  })

  const appElement = new AppElement()
  root.addChild(appElement)
  root.select(appElement)

  const result = await appElement.setup()

  if (result.error) {
    console.error(result.error)
    process.exit(1)
  }

  appElement.on('quitRequested', () => {
    process.stdout.write(ansi.cleanCursor())
    process.exit(0)
  })

  let grouplike = {
    name: 'My ~/Music Library',
    comment: (
      '(Add songs and folders to ~/Music to make them show up here,' +
      ' or pass mtui your own playlist.json file!)'),
    source: ['crawl-local', process.env.HOME + '/Music']
  }

  if (process.argv[2]) {
    console.log('Opening playlist...')

    const crawlers = getAllCrawlersForArg(process.argv[2])

    if (crawlers.length === 0) {
      console.error(`No suitable playlist crawler for "${process.argv[2]}".`)
      process.exit(1)
    }

    const crawler = crawlers[0]

    if (crawlers.length > 1) {
      console.warn(`More than one suitable crawler for "${process.argv[2]}" - using ${crawler.name}.`)
    }

    grouplike = await crawler(process.argv[2])
  }

  grouplike = await processSmartPlaylist(grouplike)

  appElement.grouplikeListingElement.loadGrouplike(grouplike)

  root.select(appElement.form)

  const flushable = new Flushable(process.stdout, true)
  flushable.resizeScreen(size)
  flushable.shouldShowCompressionStatistics = process.argv.includes('--show-ansi-stats')
  flushable.write(ansi.clearScreen())
  flushable.flush()

  setInterval(() => {
    root.renderTo(flushable)
    flushable.flush()
  }, 50)
}

main().catch(err => {
  console.error(err)
  process.exit(1)
})