Basic metadata querying and displaying working

master
Joey Pollack 2 years ago
parent e3ec07ca47
commit 295136e8e2

@ -1,7 +1,11 @@
use std::{process::Command, str}; use std::time::{Duration, Instant};
use eframe::egui; use eframe::egui;
mod player_interface;
use player_interface::SongMetadata;
fn main() -> Result<(), eframe::Error> fn main() -> Result<(), eframe::Error>
{ {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
@ -10,7 +14,7 @@ fn main() -> Result<(), eframe::Error>
..Default::default() ..Default::default()
}; };
eframe::run_native( eframe::run_native(
"My egui App", "Media Metadata Viewer",
options, options,
Box::new(|cc| { Box::new(|cc| {
// This gives us image support: // This gives us image support:
@ -23,8 +27,8 @@ fn main() -> Result<(), eframe::Error>
struct JpmmvApp struct JpmmvApp
{ {
command: String, last_query_time: Instant,
result: String, current_metadata: SongMetadata,
} }
impl Default for JpmmvApp impl Default for JpmmvApp
@ -32,8 +36,8 @@ impl Default for JpmmvApp
fn default() -> Self fn default() -> Self
{ {
Self { Self {
command: "command".to_string(), last_query_time: Instant::now(),
result: "N/A".to_string() current_metadata: SongMetadata::new(),
} }
} }
} }
@ -42,21 +46,29 @@ impl eframe::App for JpmmvApp
{ {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame)
{ {
egui::CentralPanel::default().show(ctx, |ui|
{
ui.heading("Media Meta Viewer");
ui.text_edit_singleline(&mut self.command);
if ui.button("Run Command").clicked() if self.last_query_time.elapsed() >= Duration::from_secs(1)
{ {
self.result = run_command(&format!("playerctl --player=strawberry {}", self.command)) self.current_metadata = player_interface::get_song_metadata();
self.last_query_time = Instant::now();
} }
ui.label(format!("Command result: {}", self.result)); egui::CentralPanel::default().show(ctx, |ui|
{
ui.heading("Media Meta Viewer");
ui.horizontal_centered(|ui|
{
ui.image(egui::include_image!( ui.image(egui::include_image!(
"../assets/Music Note.png" "../assets/Music Note.png"
)); ));
ui.vertical(|ui|{
ui.label(format!("{}", self.current_metadata.name));
ui.label(format!("by: {}", self.current_metadata.artist));
ui.label(format!("album: {}", self.current_metadata.album));
});
});
}); });
} }
} }

@ -1,4 +1,7 @@
use std::{process::Command, str};
pub struct SongMetadata pub struct SongMetadata
{ {
pub name: String, pub name: String,
@ -7,7 +10,7 @@ pub struct SongMetadata
} }
impl SongMetadata impl SongMetadata
{ {
fn new() -> SongMetadata pub fn new() -> SongMetadata
{ {
SongMetadata { SongMetadata {
name: "NONE".to_string(), name: "NONE".to_string(),
@ -20,8 +23,21 @@ impl SongMetadata
pub fn get_song_metadata() -> SongMetadata pub fn get_song_metadata() -> SongMetadata
{ {
const COM_START: String = "playerctl --player=strawberry "; const COM_START: &str = "playerctl --player=strawberry";
let command = format!("{} {}", COM_START, "metadata --format {{title}},{{artist}},{{album}}");
let result = run_command(&command);
let mut sm = SongMetadata::new();
let split: Vec<&str> = result.split(',').collect();
sm.name = split[0].to_string();
sm.artist = split[1].to_string();
sm.album = split[2].to_string();
sm
} }
@ -31,7 +47,7 @@ fn run_command(command: &str) -> String
.arg("-c") .arg("-c")
.arg(command) .arg(command)
.output() .output()
.expect("Failed to run the command"); .expect(&format!("Failed to run the command: {}", command));
str::from_utf8(&output.stdout).expect("Failed to convert command result to utf8 string").to_string() str::from_utf8(&output.stdout).expect("Failed to convert command result to utf8 string").to_string()
} }
Loading…
Cancel
Save