inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
keymaps.hpp
Go to the documentation of this file.
1#ifndef KEYMAPS_HPP
2#define KEYMAPS_HPP
3
5#include <cstdlib>
6#include <iostream>
7#include <string_view>
8#include <unordered_map>
9
41
51{
52 Keybinds keybinds;
53
54 // Mapping of special key names to their ASCII values
55 const std::unordered_map<std::string_view, char> special_keys = {
56 {"Tab", '\t'},
57 {"Space", ' '},
58 {"Enter", '\n'},
59 {"Esc", 27},
60 };
61
71 auto reportError = [](const std::string& field, const std::string_view& key)
72 {
73 std::cerr << "Error: Unsupported or empty keybind '" << key << "' detected for field '" << field
74 << "'. Please check your configuration." << std::endl;
75 std::exit(EXIT_FAILURE);
76 };
77
89 auto handle_special_keys = [&](const std::string_view& key, const std::string& field) -> char
90 {
91 if (key.empty())
92 {
93 reportError(field, key);
94 }
95 if (special_keys.find(key) != special_keys.end())
96 {
97 return special_keys.at(key);
98 }
99 if (key.size() == 1)
100 {
101 return key[0]; // Single-character keys
102 }
103 reportError(field, key);
104 return -1; // Unreachable case
105 };
106
107 // Map each field name to the corresponding member in the Keybinds struct
108 const std::unordered_map<std::string, char*> field_map = {
109 {"scroll_up", &keybinds.scroll_up},
110 {"scroll_down", &keybinds.scroll_down},
111 {"toggle_focus", &keybinds.toggle_focus},
112 {"show_help", &keybinds.show_help},
113 {"toggle_play", &keybinds.toggle_play},
114 {"play_song", &keybinds.play_song},
115 {"play_song_next", &keybinds.play_song_next},
116 {"play_song_prev", &keybinds.play_song_prev},
117 {"vol_up", &keybinds.vol_up},
118 {"vol_down", &keybinds.vol_down},
119 {"toggle_mute", &keybinds.toggle_mute},
120 {"quit_app", &keybinds.quit_app},
121 {"seek_ahead_5", &keybinds.seek_ahead_5},
122 {"seek_behind_5", &keybinds.seek_behind_5},
123 {"view_lyrics", &keybinds.view_lyrics},
124 {"replay_song", &keybinds.replay_song},
125 {"goto_main_screen", &keybinds.goto_main_screen},
126 {"add_song_to_queue", &keybinds.add_song_to_queue},
127 {"add_artists_songs_to_queue", &keybinds.add_artists_songs_to_queue},
128 {"remove_song_from_queue", &keybinds.remove_song_from_queue},
129 {"play_this_song_next", &keybinds.play_this_song_next},
130 {"view_song_queue", &keybinds.view_song_queue},
131 {"view_current_song_info", &keybinds.view_current_song_info}};
132
133 // Populate the keybinds struct by reading the fields from the TOML configuration
134 for (const auto& [field, member_ptr] : field_map)
135 {
136 *member_ptr = handle_special_keys(parseTOMLField(PARENT_KEYBINDS, field),
137 field);
138 }
139
140 return keybinds;
141}
142
143#endif
Keybinds parseKeybinds()
Parses the keybinds from the TOML configuration.
Definition keymaps.hpp:50
Struct to hold keybinding mappings.
Definition keymaps.hpp:16
char goto_main_screen
Definition keymaps.hpp:32
char show_help
Definition keymaps.hpp:20
char add_artists_songs_to_queue
Definition keymaps.hpp:35
char scroll_up
Definition keymaps.hpp:17
char seek_ahead_5
Definition keymaps.hpp:29
char toggle_mute
Definition keymaps.hpp:27
char replay_song
Definition keymaps.hpp:33
char vol_down
Definition keymaps.hpp:26
char view_song_queue
Definition keymaps.hpp:38
char view_lyrics
Definition keymaps.hpp:31
char play_song_prev
Definition keymaps.hpp:24
char seek_behind_5
Definition keymaps.hpp:30
char scroll_down
Definition keymaps.hpp:18
char play_song_next
Definition keymaps.hpp:23
char toggle_play
Definition keymaps.hpp:21
char remove_song_from_queue
Definition keymaps.hpp:36
char play_this_song_next
Definition keymaps.hpp:37
char toggle_focus
Definition keymaps.hpp:19
char vol_up
Definition keymaps.hpp:25
char add_song_to_queue
Definition keymaps.hpp:34
char quit_app
Definition keymaps.hpp:28
char view_current_song_info
Definition keymaps.hpp:39
char play_song
Definition keymaps.hpp:22
string_view parseTOMLField(string parent, string field)
Parses a string field from the TOML configuration.
Definition toml_parser.hpp:121
#define PARENT_KEYBINDS
Definition toml_parser.hpp:30