Skip to content

Commit

Permalink
feature: see library stats (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshankman authored Oct 27, 2024
1 parent 7bc4fed commit 61f6e87
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ This project was built with the following technologies:
- [x] Adjustable album art size
- [x] Shuffle "history" for hitting previous
- [x] Mute and max buttons
- [x] Show stats about your library somewhere, like GB and # of songs
- [ ] Edit song metadata
- [ ] Show stats about your library somewhere, like GB and # of songs
- [ ] Hide and show columns in the explorer
- [ ] iTunes-1.0-like "browser" for seeing scrollable list of artists or albums
- [ ] Toggle column visibility in the explorer
- [ ] iTunes-1.0-like browser with list of artists or albums
- [ ] Queue next-up song


Expand Down
63 changes: 63 additions & 0 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,60 @@ import {
shell,
BrowserWindow,
MenuItemConstructorOptions,
dialog,
} from 'electron';
import fs from 'fs';
import path from 'path';

interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
selector?: string;
submenu?: DarwinMenuItemConstructorOptions[] | Menu;
}

/**
* Get JSON data from a file at some filepath
*
* @param fp filepath
* @returns the parsed data from the file at the filepath
*/
function parseData(fp: string): any {
try {
return JSON.parse(fs.readFileSync(fp, 'utf8')) as any;
} catch (error) {
return {};
}
}

/**
* Get the user's configuration data from the userConfig.json file
*
* @returns the user's configuration data as a StoreStructure
*/
function getUserConfig(): any {
return parseData(path.join(app.getPath('userData'), 'userConfig.json'));
}

function getLibraryStats(): { songCount: number; sizeInGB: number } {
const userConfig = getUserConfig();
const { library } = userConfig;
const songCount = Object.keys(library).length;

let totalSize = 0;
// eslint-disable-next-line no-restricted-syntax
for (const filePath of Object.keys(library)) {
try {
const stats = fs.statSync(filePath);
totalSize += stats.size;
} catch (error) {
console.error(`Error reading file size for ${filePath}:`, error);

Check warning on line 52 in src/main/menu.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
}
}

const sizeInGB = totalSize / (1024 * 1024 * 1024);

return { songCount, sizeInGB };
}

export default class MenuBuilder {
mainWindow: BrowserWindow;

Expand Down Expand Up @@ -105,6 +152,22 @@ export default class MenuBuilder {
// },
{ type: 'separator' },

{
label: 'see library stats',
click: () => {
const stats = getLibraryStats();
dialog.showMessageBox(this.mainWindow, {
type: 'info',
title: 'Library Stats',
message: `Songs: ${
stats.songCount
}\nSize: ${stats.sizeInGB.toFixed(2)} GB`,
});
},
},

{ type: 'separator' },

{
label: 'hide hihat',
accelerator: 'Command+H',
Expand Down

0 comments on commit 61f6e87

Please sign in to comment.