···11+import chalk from "chalk";
22+import { RockskyClient } from "client";
33+44+export async function search(
55+ query: string,
66+ { limit = 20, albums = false, artists = false, tracks = false, users = false }
77+) {
88+ const client = new RockskyClient();
99+ const results = await client.search(query, { size: limit });
1010+ if (results.records.length === 0) {
1111+ console.log(`No results found for ${chalk.magenta(query)}.`);
1212+ return;
1313+ }
1414+1515+ // merge all results into one array with type and sort by xata_scrore
1616+ let mergedResults = results.records.map((record) => ({
1717+ ...record,
1818+ type: record.table,
1919+ }));
2020+ mergedResults.sort((a, b) => b.xata_score - a.xata_score);
2121+2222+ if (albums) {
2323+ mergedResults = mergedResults.filter((record) => record.table === "albums");
2424+ }
2525+2626+ if (artists) {
2727+ mergedResults = mergedResults.filter(
2828+ (record) => record.table === "artists"
2929+ );
3030+ }
3131+3232+ if (tracks) {
3333+ mergedResults = mergedResults.filter(({ table }) => table === "tracks");
3434+ }
3535+3636+ if (users) {
3737+ mergedResults = mergedResults.filter(({ table }) => table === "users");
3838+ }
3939+4040+ for (const { table, record } of mergedResults) {
4141+ if (table === "users") {
4242+ console.log(
4343+ `${chalk.bold.magenta(record.handle)} ${
4444+ record.display_name
4545+ } ${chalk.yellow(`https://rocksky.app/profile/${record.did}`)}`
4646+ );
4747+ }
4848+4949+ if (table === "albums") {
5050+ const link = record.uri
5151+ ? `https://rocksky.app/${record.uri?.split("at://")[1]}`
5252+ : "";
5353+ console.log(
5454+ `${chalk.bold.magenta(record.title)} ${record.artist} ${chalk.yellow(
5555+ link
5656+ )}`
5757+ );
5858+ }
5959+6060+ if (table === "tracks") {
6161+ const link = record.uri
6262+ ? `https://rocksky.app/${record.uri?.split("at://")[1]}`
6363+ : "";
6464+ console.log(
6565+ `${chalk.bold.magenta(record.title)} ${record.artist} ${chalk.yellow(
6666+ link
6767+ )}`
6868+ );
6969+ }
7070+ }
7171+}
+14
src/index.ts
···2233import { nowplaying } from "cmd/nowplaying";
44import { scrobbles } from "cmd/scrobbles";
55+import { search } from "cmd/search";
56import { whoami } from "cmd/whoami";
67import { Command } from "commander";
78import version from "../package.json" assert { type: "json" };
···4344 .argument("[did]", "The DID or handle of the user to get the scrobbles for.")
4445 .description("Display recently played tracks.")
4546 .action(scrobbles);
4747+4848+program
4949+ .command("search")
5050+ .option("-a, --albums", "Search for albums")
5151+ .option("-t, --tracks", "Search for tracks")
5252+ .option("-u, --users", "Search for users")
5353+ .option("-l, --limit <number>", "Number of results to limit")
5454+ .argument(
5555+ "<query>",
5656+ "The search query, e.g., artist, album, title or account"
5757+ )
5858+ .description("Search for tracks, albums, or accounts.")
5959+ .action(search);
46604761program.parse(process.argv);