WIP Moving CLI to command-like input.

This commit is contained in:
Jonathan Bernard 2016-02-12 17:21:03 -06:00
parent 1354a92204
commit 70aa706eac

View File

@ -64,7 +64,7 @@ Configuration:
private synchronized boolean running
private String titleStyle, normalStyle, statusStyle, promptStyle,
artistStyle, albumStyle, fileStyle, errorStyle
artistStyle, albumStyle, fileStyle, errorStyle, playlistStyle
private String clearLine = new ANSI().eraseLine(Erase.All).toString()
private String afterInput =
new ANSI().eraseLine(Erase.All).scrollUp().cursorUp().toString()
@ -73,7 +73,8 @@ Configuration:
private String afterLeader =
new ANSI().restoreCursor().eraseLine(Erase.ToEnd).toString()
private String eraseLeader =
new ANSI().cursorPrevLine().eraseLine(Erase.All).cursorPrevLine().eraseLine(Erase.All).toString()
new ANSI().cursorPrevLine().eraseLine(Erase.All).cursorPrevLine()
.eraseLine(Erase.All).toString()
private int displayWidth = 79
private long msgTimeout
@ -216,15 +217,28 @@ Configuration:
if (new Date() > dismissMsgDate) resetStatus()
if (consoleReadBuffer.size() > 0) {
line = consoleReadBuffer.remove(0)
switch(line) {
case ~/quit|exit|.+\u0004$/:
running = false
consoleReaderThread.interrupt()
break
processInput(line.split(/\w/) as LinkedList)
} else {
drawLeader()
Thread.sleep(250)
}
}
}
case 'scan':
scanMediaLibrary()
break
private def processInput(LinkedList<String> line) {
String command = line.poll()
switch(command.toLowerCase()) {
case 'album': return selectAlbum(line)
case 'artist': return selectArtist(line)
case 'scan': return scanMediaLibrary()
case 'list':
String nextArg = line.poll()
if (nextArg.toLowerCase() == 'all')
return processList(line, true)
else {
if (nextArg) line.push(nextArg)
return processList(line, false) }
case 'debug':
outStream.println(
@ -233,44 +247,17 @@ Configuration:
.join("\n") +
"\n\n\n")
drawLeader(true)
break
return
case "artist":
selection.artist = null
resetStatus()
break
case 'q': case 'quit': case ':q': case 'exit': case '\u0004':
running = false
consoleReaderThread.interrupt()
return
case ~/artist (.+)/:
def input = Matcher.lastMatcher[0][1]?.trim()
def match = library.getByIdOrName(input, Artist)
if (!match) setErr("No artist matches '$input'.")
else if (match.size() > 1)
setErr("Multiple artists match '$input': " +
match.collect { "${it.id}: ${it.name}" }
.join(", "))
else {
selection.artist = match[0]
resetStatus() }
break
case "album":
selection.album = null
resetStatus()
break
case ~/album (.+)/:
def input = Matcher.lastMatcher[0][1]?.trim()
def match = library.getByIdOrName(input, Album)
if (!match) setErr("No album matches '$input'.")
else if (match.size() > 1)
setErr("Multiple albums match '$input': " +
match.collect { "${it.id}: ${it.name}" }
.join(", "))
else {
selection.album = match[0]
resetStatus() }
break
case "list artists for album":
if (!selection.album) {
setErr("No album is selected.")
@ -319,18 +306,63 @@ Configuration:
Thread.sleep(250)
break
}
} else {
drawLeader()
Thread.sleep(250)
}
private def processList(LinkedList options, boolean all) {
def option = options.poll()
switch(option) {
case 'albums':
case 'artists':
case 'bookmarks':
case 'playlists':
default:
printLongMessage("Unrecognized option to the ${promptStyle}" +
"list${normalStyle} command. Use ${promptStyle}help list" +
"to see a list of valid options.")
return null
}
}
private void scanMediaLibrary() {
public Album selectAlbum(LinkedList input) {
String criteria = input.join(" ")
if (!criteria) { selection.album = null; resetStatus(); return null }
Album match = library.getByIdOrName(Album, criteria)
if (!match) { setErr("No album matches '$input'."); return null }
else if (match.size() > 1) {
setErr("Multiple albums match '$input': " +
match.collect { "${it.id}: ${it.name}" }.join(", "))
return null }
selection.album = match[0]
resetStatus()
return match[0] }
public void selectArtist(LinkedList input) {
String criteria = input.join(" ")
if (!criteria) { selection.artist = null; resetStatus(); return null }
Artist match = library.getByIdOrName(Artist, criteria)
if (!match) { setErr("No artist matches '$input'."); return null }
else if (match.size() > 1) {
setErr("Multiple artists match '$input': " +
match.collect { "${it.id}: ${it.name}" }.join(", "))
return null }
selection.artist = match[0]
resetStatus()
return match[0] }
public MediaLibrary scanMediaLibrary() {
status.text = "Scanning media library..."
library.rescanLibrary()
status.text = "Scanned ? files."
dismissMsgDate = new Date(new Date().time + msgTimeout) }
dismissMsgDate = new Date(new Date().time + msgTimeout)
return library }
private void drawLeader(afterOutput = false) {
@ -342,6 +374,16 @@ Configuration:
outStream.print(leader)
outStream.flush() }
private void printLongMessage(String msg) {
String result = new StringBuilder()
.append(eraseLeader)
.append(msg)
.append("\n\n")
.toString()
outStream.println result
drawLeader(true) }
private String getLeader() {
StringBuilder leader = new StringBuilder()
.append(clearLine)
@ -393,6 +435,11 @@ Configuration:
private String resetStatus() {
StringBuilder s = new StringBuilder()
if (selection.playlist) s.append(playlistStyle)
.append(selection.playlist)
.append(normalStyle)
.append(": ")
if (selection.artist) s.append(artistStyle)
.append(selection.artist)
.append(normalStyle)