« 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: ca62f1746c84a1370d0e63726a30a707f0c5a18c (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
// omg I am tired of code

const { AppElement } = require('./ui')
const { updatePlaylistFormat } = require('./playlist-utils')
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')

// 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 flushable = new Flushable(process.stdout, true)
  flushable.screenLines = size.lines
  flushable.screenCols = size.cols
  flushable.shouldShowCompressionStatistics = process.argv.includes('--show-ansi-stats')
  flushable.write(ansi.clearScreen())
  flushable.flush()

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

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

  await appElement.setup()

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

  let grouplike = {
    items: [
      {name: 'bears', downloaderArg: 'http://www.billwurtz.com/bears.mp3'},
      {name: 'alphabet shuffle', downloaderArg: 'http://www.billwurtz.com/alphabet-shuffle.mp3'},
      {name: 'in california', downloaderArg: 'http://www.billwurtz.com/in-california.mp3'},
      {name: 'i love you', downloaderArg: 'http://www.billwurtz.com/i-love-you.mp3'},
      {name: 'movie star', downloaderArg: 'http://www.billwurtz.com/movie-star.mp3'},
      {name: 'got to know what\'s going on', downloaderArg: 'http://www.billwurtz.com/got-to-know-whats-going-on.mp3'},
      {name: 'outside', downloaderArg: 'http://www.billwurtz.com/outside.mp3'},
      {name: 'La de da de da de da de day oh', downloaderArg: 'http://www.billwurtz.com/la-de-da-de-da-de-da-de-day-oh.mp3'},
      {name: 'and the day goes on', downloaderArg: 'http://www.billwurtz.com/and-the-day-goes-on.mp3'}
    ]
  }

  grouplike = require(process.argv[2] || './library.json')

  grouplike = updatePlaylistFormat(grouplike)

  appElement.grouplikeListingElement.loadGrouplike(grouplike)

  root.select(appElement.form)

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

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