Added lua methods to access track data

custom
ailurux 7 months ago
parent f58679983e
commit c4ec089a90
  1. 5
      lua/browser.lua
  2. 72
      src/tangara/lua/lua_database.cpp
  3. 2
      src/tangara/lua/lua_database.hpp
  4. 24
      src/tangara/lua/property.cpp

@ -8,6 +8,7 @@ local styles = require("styles")
local playback = require("playback") local playback = require("playback")
local theme = require("theme") local theme = require("theme")
local screen = require("screen") local screen = require("screen")
local database = require("database")
return screen:new{ return screen:new{
create_ui = function(self) create_ui = function(self)
@ -101,6 +102,10 @@ return screen:new{
}) })
else else
queue.clear() queue.clear()
local track = database.track_by_id(contents)
if (track) then
print("Track saved position: ", track.saved_position)
end
queue.add(contents) queue.add(contents)
playback.playing:set(true) playback.playing:set(true)
backstack.push(playing:new()) backstack.push(playing:new())

@ -73,6 +73,56 @@ static auto indexes(lua_State* state) -> int {
return 1; return 1;
} }
auto pushTagValue(lua_State* L, const database::TagValue& val) -> void {
std::visit(
[&](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::pmr::string>) {
lua_pushlstring(L, arg.data(), arg.size());
} else if constexpr (std::is_same_v<
T, std::span<const std::pmr::string>>) {
lua_createtable(L, 0, arg.size());
for (const auto& i : arg) {
lua_pushlstring(L, i.data(), i.size());
lua_pushboolean(L, true);
lua_rawset(L, -3);
}
} else if constexpr (std::is_same_v<T, uint32_t>) {
lua_pushinteger(L, arg);
} else {
lua_pushnil(L);
}
},
val);
}
static void pushTrack(lua_State* L, const database::Track& track) {
lua_newtable(L);
lua_pushliteral(L, "tags");
lua_newtable(L);
for (const auto& tag : track.tags().allPresent()) {
lua_pushstring(L, database::tagName(tag).c_str());
pushTagValue(L, track.tags().get(tag));
lua_settable(L, -3);
}
lua_settable(L, -3);
lua_pushliteral(L, "id");
lua_pushinteger(L, track.data().id);
lua_settable(L, -3);
lua_pushliteral(L, "filepath");
lua_pushstring(L, track.data().filepath.c_str());
lua_settable(L, -3);
lua_pushliteral(L, "saved_position");
lua_pushinteger(L, track.data().last_position);
lua_settable(L, -3);
}
static auto version(lua_State* L) -> int { static auto version(lua_State* L) -> int {
Bridge* instance = Bridge::Get(L); Bridge* instance = Bridge::Get(L);
auto db = instance->services().database().lock(); auto db = instance->services().database().lock();
@ -111,9 +161,29 @@ static auto update(lua_State* L) -> int {
return 0; return 0;
} }
static auto track_by_id(lua_State* L) -> int {
auto id = luaL_checkinteger(L, -1);
Bridge* instance = Bridge::Get(L);
auto db = instance->services().database().lock();
if (!db) {
return 0;
}
auto track = db->getTrack(id);
if (!track) {
return 0;
}
pushTrack(L, *track);
return 1;
}
static const struct luaL_Reg kDatabaseFuncs[] = { static const struct luaL_Reg kDatabaseFuncs[] = {
{"indexes", indexes}, {"version", version}, {"size", size}, {"indexes", indexes}, {"version", version}, {"size", size},
{"recreate", recreate}, {"update", update}, {NULL, NULL}}; {"recreate", recreate}, {"update", update}, {"track_by_id", track_by_id},
{NULL, NULL}};
/* /*
* Struct to be used as userdata for the Lua representation of database records. * Struct to be used as userdata for the Lua representation of database records.

@ -14,6 +14,8 @@ namespace lua {
auto db_check_iterator(lua_State*, int stack_pos) -> database::Iterator*; auto db_check_iterator(lua_State*, int stack_pos) -> database::Iterator*;
auto pushTagValue(lua_State* L, const database::TagValue& val) -> void;
auto RegisterDatabaseModule(lua_State*) -> void; auto RegisterDatabaseModule(lua_State*) -> void;
} // namespace lua } // namespace lua

@ -19,6 +19,7 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "lua.h" #include "lua.h"
#include "lua.hpp" #include "lua.hpp"
#include "lua/lua_database.hpp"
#include "lua/lua_thread.hpp" #include "lua/lua_thread.hpp"
#include "lvgl.h" #include "lvgl.h"
#include "memory_resource.hpp" #include "memory_resource.hpp"
@ -240,29 +241,6 @@ auto Property::set(const LuaValue& val) -> bool {
return true; return true;
} }
static auto pushTagValue(lua_State* L, const database::TagValue& val) -> void {
std::visit(
[&](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::pmr::string>) {
lua_pushlstring(L, arg.data(), arg.size());
} else if constexpr (std::is_same_v<
T, std::span<const std::pmr::string>>) {
lua_createtable(L, 0, arg.size());
for (const auto& i : arg) {
lua_pushlstring(L, i.data(), i.size());
lua_pushboolean(L, true);
lua_rawset(L, -3);
}
} else if constexpr (std::is_same_v<T, uint32_t>) {
lua_pushinteger(L, arg);
} else {
lua_pushnil(L);
}
},
val);
}
static void pushTrack(lua_State* L, const audio::TrackInfo& track) { static void pushTrack(lua_State* L, const audio::TrackInfo& track) {
lua_newtable(L); lua_newtable(L);

Loading…
Cancel
Save