WIP continuing implementation of CLI.

This commit is contained in:
Jonathan Bernard 2016-02-29 12:39:07 -06:00
parent a1230dddfd
commit f80ff182ad

View File

@ -11,6 +11,7 @@ import com.zaxxer.hikari.HikariDataSource
import org.docopt.Docopt import org.docopt.Docopt
import jline.console.ConsoleReader import jline.console.ConsoleReader
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.regex.Pattern
import java.util.regex.Matcher import java.util.regex.Matcher
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -94,7 +95,7 @@ Configuration:
/// Current play queue and selection data /// Current play queue and selection data
def selection = [:] def selection = [:]
Playlist playQueue = library.save(new Playlist( Playlist playQueue = library.save(new Playlist(
"CLI Queue ${sdf.format(new Date())}")) name: "CLI Queue ${sdf.format(new Date())}"))
Bookmark playBookmark Bookmark playBookmark
MediaFile curMediaFile MediaFile curMediaFile
@ -306,35 +307,44 @@ Configuration:
option = line.poll() option = line.poll()
switch (option) { switch (option) {
case 'album': case 'album':
return select(Album, library.getAlbumsWhere({ return selectMatches(Album, library.getAlbumsWhere({
mediaFileId: curMediaFile.id})) mediaFileId: curMediaFile.id}))
case 'artist': case 'artist':
return select(Artist, library.getArtistsWhere({ return selectMatches(Artist, library.getArtistsWhere({
mediaFileId: curMediaFile.id})) mediaFileId: curMediaFile.id}))
case 'playlist': case 'playlist':
return select(Playlist, playQueue) return selectMatches(Playlist, playQueue)
case 'file': case 'file':
return select(MediaFile, curMediaFile) return selectMatches(MediaFile, curMediaFile)
case 'tags': case 'tags':
return select(Tag, library.getTagsWhere({ return selectMatches(Tag, library.getTagsWhere({
mediaFileId: curMediaFile.id}) mediaFileId: curMediaFile.id}))
default: default:
setErr("Unrecognized option to ${promptStyle}select " + setErr("Unrecognized option to ${promptStyle}select " +
"current${errStyle}.") "current${errorStyle}.")
return null return null
} }
} }
switch (option) { switch (option) {
case 'album': return select(Album, library.getByIdOrName(line.join(' '))) case 'album':
case 'artist': return select(Artist, library.getByIdOrName(line.join(' '))) return selectMatches(Album,
case 'playlist': return select(Playlist, library.getByIdOrName(line.join(' '))) library.getByIdOrName(Album, line.join(' ')))
case 'current': return select(Current, library.getByIdOrName(line.join(' '))) case 'artist':
case 'file': return select(MediaFile, library.getByIdOrName(line.join(' '))) return selectMatches(Artist,
case 'tags': return select(Tag, library.getByIdOrName(line.join(' '))) library.getByIdOrName(Artist, line.join(' ')))
case 'playlist':
return selectMatches(Playlist,
library.getByIdOrName(Playlist, line.join(' ')))
case 'file':
return selectMatches(MediaFile,
library.getByIdOrName(MediaFile, line.join(' ')))
case 'tags':
return selectMatches(Tag,
library.getByIdOrName(Tag, line.join(' ')))
default: default:
setErr("Unrecognized option to ${promptStyle}select${errStyle}") setErr("Unrecognized option to ${promptStyle}select${errorStyle}")
return null return null
} }
} }
@ -353,7 +363,6 @@ Configuration:
} }
private def processAdd(LinkedList line) { private def processAdd(LinkedList line) {
// Add takes the form of
def options = line.poll() def options = line.poll()
switch(option) { switch(option) {
case 'playlist': case 'playlist':
@ -366,6 +375,26 @@ Configuration:
} }
} }
private def processEnqueue(LinkedList line) {
// TODO
}
private def processTag(LinkedList line) {
// TODO
}
private def processClear(LinkedList line) {
def option = line.poll()
switch(option) {
case 'queue': library.removeAllFromPlaylist(playQueue.id)
default:
printLongMessage("Unrecognized option to the ${promptStyle}" +
"add${normalStyle} command. Use ${promptStyle}help add" +
"${normalStyle} to see a list of valid options.")
return null
}
}
private def processList(LinkedList options) { private def processList(LinkedList options) {
logger.debug("Listing things. Options: $options") logger.debug("Listing things. Options: $options")
def option = options.poll() def option = options.poll()
@ -469,18 +498,18 @@ Configuration:
String key = uncapitalize(modelClass.simpleName) String key = uncapitalize(modelClass.simpleName)
this.selection[key] = null } this.selection[key] = null }
public def select(Class modelClass, def matches) { public def selectMatches(Class modelClass, def matches) {
String englishName = ${toEnglish(modelClass.simpleName} String englishName = toEnglish(modelClass.simpleName)
if (!matches) { if (!matches) {
setErr("No $englishName matches."); setErr("No $englishName matches.");
return null } return null }
else if (match.size() > 1) { else if (matches.size() > 1) {
setErr("Multiple ${englishName}s match: " + setErr("Multiple ${englishName}s match: " +
matches.collect { "${it.id}: ${it.name}" }.join(', ')) matches.collect { "${it.id}: ${it.name}" }.join(', '))
return null } return null }
selection[uncapitalize(modelClass.simpleName)] = matches[0] selection[uncapitalize(modelClass.simpleName)] = matches[0]
resetStatus resetStatus()
return matches[0] } return matches[0] }
private void drawLeader(afterOutput = false) { private void drawLeader(afterOutput = false) {
@ -593,10 +622,10 @@ Configuration:
private static String uncapitalize(String s) { private static String uncapitalize(String s) {
if (s == null) return null; if (s == null) return null;
if (s.lengh() < 2) return s.toLowerCase(); if (s.length() < 2) return s.toLowerCase();
return s[0].toLowerCase() + s[1..-1] } return s[0].toLowerCase() + s[1..-1] }
private static String toEnglish(String modelName) { private static String toEnglish(String modelName) {
return UPPERCASE_PATTERN.matcher(name). return UPPERCASE_PATTERN.matcher(modelName).
replaceAll(/$1 $2/).toLowerCase() } replaceAll(/$1 $2/).toLowerCase() }
} }