From 688bea9103576c176194bf49fa3d476be3aa4188 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 13 Feb 2016 23:29:46 -0600 Subject: [PATCH] WIP CLI Got the list command working for albums, artists, and media files. --- .../wdiwtlt/cli/CommandLineInterface.groovy | 152 +++++++++++------- 1 file changed, 91 insertions(+), 61 deletions(-) diff --git a/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy b/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy index bb2eb8a..d558b27 100644 --- a/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy +++ b/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy @@ -73,8 +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().eraseLine(Erase.All).cursorPrevLine().eraseLine(Erase.All) + .cursorPrevLine().eraseLine(Erase.All).toString() private int displayWidth = 79 private long msgTimeout @@ -217,7 +217,7 @@ Configuration: if (new Date() > dismissMsgDate) resetStatus() if (consoleReadBuffer.size() > 0) { line = consoleReadBuffer.remove(0) - processInput(line.split(/\w/) as LinkedList) + processInput(line.split(/\s/) as LinkedList) } else { drawLeader() Thread.sleep(250) @@ -226,8 +226,10 @@ Configuration: } private def processInput(LinkedList line) { + logger.debug("line: $line") String command = line.poll() - switch(command.toLowerCase()) { + logger.debug("command: $command") + switch(command?.toLowerCase()) { case 'album': return selectAlbum(line) case 'artist': return selectArtist(line) case 'scan': return scanMediaLibrary() @@ -237,7 +239,7 @@ Configuration: if (nextArg.toLowerCase() == 'all') return processList(line, true) else { - if (nextArg) line.push(nextArg) + if (nextArg) line.addFirst(nextArg) return processList(line, false) } case 'debug': @@ -258,46 +260,6 @@ Configuration: resetStatus() break - case "list artists for album": - if (!selection.album) { - setErr("No album is selected.") - break } - outStream.println(makeArtistList( - library.getArtistsByAlbumId(selection.album.id), - selection.album)) - drawLeader(true) - break - - case ~/list artists( .+)?/: - def name = Matcher.lastMatcher[0][1]?.trim() - def artists - if (name) artists = library.getArtistsByName(name) - else artists = library.getArtists() - - outStream.println(makeArtistList(artists)) - drawLeader(true) - break - - case "list albums for artist": - if (!selection.artist) { - setErr("No artist selected.") - break } - outStream.println(makeAlbumList( - library.getAlbumsByArtistId(selection.artist.id), - selection.artist)) - drawLeader(true) - break - - case ~/list albums( .+)?/: - def name = Matcher.lastMatcher[0][1]?.trim() - def albums - if (name) albums = library.getAlbumsByName(name) - else albums = library.getAlbums() - - outStream.println(makeAlbumList(albums)) - drawLeader(true) - break - default: status.text = errorStyle + "Unrecognized command: '$line'${normalStyle}" @@ -310,16 +272,58 @@ Configuration: } private def processList(LinkedList options, boolean all) { + logger.debug("Listing albums. Options: $options") def option = options.poll() + logger.debug("Option: $option") + + def list switch(option) { case 'albums': + if (all) list = library.getAlbums() + else if (selection.album) list = [selection.album] + else list = library.getAlbumsWhere( + playlistId: selection.playlist?.id, + artistId: selection.artist?.id, + mediaFileId: selection.mediaFile?.id) + + String albumMatch = options?.join(" ")?.trim() + if (albumMatch) list = list.findAll { it.name =~ albumMatch } + printLongMessage(makeAlbumList(list, all)) + break + case 'artists': + if (all) list = library.getArtists() + else if (selection.artist) list = [selection.artist] + else list = library.getArtistsWhere( + playlistId: selection.playlist?.id, + albumId: selection.album?.id, + mediaFileId: selection.mediaFile?.id) + + String artistMatch = options?.join(" ")?.trim() + if (artistMatch) list = list.findAll { it.name =~ artistMatch } + printLongMessage(makeArtistList(list, all)) + break + + case 'files': + if (all) list = library.getMediaFiles() + else if (selection.mediaFile) list = [selection.mediaFile] + else list = library.getMediaFilesWhere( + playlistId: selection.playlist?.id, + artistId: selection.artist?.id, + albumId: selection.album?.id) + + String mediaFileMatch = options?.join(" ")?.trim() + if (mediaFileMatch) list = list.findAll { + it.name =~ mediaFileMatch } + printLongMessage(makeMediaFileList(list, all)) + break + 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.") + "${normalStyle} to see a list of valid options.") return null } @@ -331,7 +335,7 @@ Configuration: if (!criteria) { selection.album = null; resetStatus(); return null } - Album match = library.getByIdOrName(Album, criteria) + def match = library.getByIdOrName(Album, criteria) if (!match) { setErr("No album matches '$input'."); return null } else if (match.size() > 1) { @@ -343,12 +347,12 @@ Configuration: resetStatus() return match[0] } - public void selectArtist(LinkedList input) { + public Artist selectArtist(LinkedList input) { String criteria = input.join(" ") if (!criteria) { selection.artist = null; resetStatus(); return null } - Artist match = library.getByIdOrName(Artist, criteria) + def match = library.getByIdOrName(Artist, criteria) if (!match) { setErr("No artist matches '$input'."); return null } else if (match.size() > 1) { @@ -408,33 +412,52 @@ Configuration: status.text = errorStyle + errMsg dismissMsgDate = new Date(new Date().time + msgTimeout) } - private String makeAlbumList(def albums, Artist artist = null) { + private String makeAlbumList(def albums, boolean listAll) { def result = new StringBuilder() - .append(eraseLeader) .append("--------------------\nAlbums") - if (artist) result.append(" (for artist '$artist'):\n") - else result.append(":\n\n") + if (!listAll && (selection.playlist || selection.artist || + selection.mediaFile)) + result.append("\n(for selection: ") + .append(describeSelection()) + result.append(":\n\n") result.append(albums.collect { "${it.id}: ${it}" }.join("\n")) - .append("\n\n\n") + .append("\n") return result.toString() } - private String makeArtistList(def artists, Album album = null) { + private String makeArtistList(def artists, boolean listAll) { def result = new StringBuilder() - .append(eraseLeader) .append("--------------------\nArists") - if (album) result.append(" (for album '$album'):\n") - else result.append(":\n\n") + if (!listAll && (selection.playlist || selection.artist || + selection.mediaFile)) + result.append("\n(for selection: ") + .append(describeSelection()) + result.append(":\n\n") result.append(artists.collect { "${it.id}: ${it.name}" }.join("\n")) - .append("\n\n\n") + .append("\n") return result.toString() } - private String resetStatus() { + private String makeMediaFileList(def mediaFiles, boolean listAll) { + def result = new StringBuilder() + .append("--------------------\nMedia Files") + + if (!listAll && (selection.playlist || selection.artist || + selection.mediaFile)) + result.append("\n(for selection: ") + .append(describeSelection()) + + result.append(":\n\n") + result.append(mediaFiles.collect { "${it.id}: ${it.trackNumber} - ${it.name}" }.join("\n")) + .append("\n") + + return result.toString() } + + private String describeSelection() { StringBuilder s = new StringBuilder() if (selection.playlist) s.append(playlistStyle) @@ -454,9 +477,16 @@ Configuration: if (selection.mediaFile) s.append(fileStyle) .append(selection.mediaFile) + .append(normalStyle) + + return s.toString() + } + + private String resetStatus() { + String s = describeSelection() if (s.size() == 0) status.text = "No current media selections." - else status.text = s.toString() + else status.text = s return status.text} }