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

@ -1,4 +1,7 @@
use std::{process::Command, str};
pub struct SongMetadata
{
pub name: String,
@ -7,7 +10,7 @@ pub struct SongMetadata
}
impl SongMetadata
{
fn new() -> SongMetadata
pub fn new() -> SongMetadata
{
SongMetadata {
name: "NONE".to_string(),
@ -20,8 +23,21 @@ impl 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(command)
.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()
}
Loading…
Cancel
Save