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 synchronized boolean running
private String titleStyle, normalStyle, statusStyle, promptStyle, 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 clearLine = new ANSI().eraseLine(Erase.All).toString()
private String afterInput = private String afterInput =
new ANSI().eraseLine(Erase.All).scrollUp().cursorUp().toString() new ANSI().eraseLine(Erase.All).scrollUp().cursorUp().toString()
@ -73,7 +73,8 @@ Configuration:
private String afterLeader = private String afterLeader =
new ANSI().restoreCursor().eraseLine(Erase.ToEnd).toString() new ANSI().restoreCursor().eraseLine(Erase.ToEnd).toString()
private String eraseLeader = 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 int displayWidth = 79
private long msgTimeout private long msgTimeout
@ -216,15 +217,28 @@ Configuration:
if (new Date() > dismissMsgDate) resetStatus() if (new Date() > dismissMsgDate) resetStatus()
if (consoleReadBuffer.size() > 0) { if (consoleReadBuffer.size() > 0) {
line = consoleReadBuffer.remove(0) line = consoleReadBuffer.remove(0)
switch(line) { processInput(line.split(/\w/) as LinkedList)
case ~/quit|exit|.+\u0004$/: } else {
running = false drawLeader()
consoleReaderThread.interrupt() Thread.sleep(250)
break }
}
}
case 'scan': private def processInput(LinkedList<String> line) {
scanMediaLibrary() String command = line.poll()
break 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': case 'debug':
outStream.println( outStream.println(
@ -233,44 +247,17 @@ Configuration:
.join("\n") + .join("\n") +
"\n\n\n") "\n\n\n")
drawLeader(true) drawLeader(true)
break return
case "artist": case 'q': case 'quit': case ':q': case 'exit': case '\u0004':
selection.artist = null running = false
resetStatus() consoleReaderThread.interrupt()
break 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 selection.album = null
resetStatus() resetStatus()
break 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": case "list artists for album":
if (!selection.album) { if (!selection.album) {
setErr("No album is selected.") setErr("No album is selected.")
@ -319,18 +306,63 @@ Configuration:
Thread.sleep(250) Thread.sleep(250)
break 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..." status.text = "Scanning media library..."
library.rescanLibrary() library.rescanLibrary()
status.text = "Scanned ? files." 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) { private void drawLeader(afterOutput = false) {
@ -342,6 +374,16 @@ Configuration:
outStream.print(leader) outStream.print(leader)
outStream.flush() } 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() { private String getLeader() {
StringBuilder leader = new StringBuilder() StringBuilder leader = new StringBuilder()
.append(clearLine) .append(clearLine)
@ -393,6 +435,11 @@ Configuration:
private String resetStatus() { private String resetStatus() {
StringBuilder s = new StringBuilder() StringBuilder s = new StringBuilder()
if (selection.playlist) s.append(playlistStyle)
.append(selection.playlist)
.append(normalStyle)
.append(": ")
if (selection.artist) s.append(artistStyle) if (selection.artist) s.append(artistStyle)
.append(selection.artist) .append(selection.artist)
.append(normalStyle) .append(normalStyle)