yum-archive/yapBox
A black box for your yapping
git clone https://git.yummers.dev/yum-archive/yapBox
a4c1870
master
1#include <curses.h> 2#include <ncurses.h> 3#include <stdio.h> 4 5#include <filesystem> 6#include <fstream> 7#include <iostream> 8#include <map> 9#include <string> 10#include <utility> 11#include <vector> 12 13typedef std::pair < std::string , std::string > datapoint_t ; 14 15const int PAGE_LINES = 40 ; 16const int TRANSCRIPT_CHARS = 120 ; 17 18void getData ( 19const std::filesystem::path & data_path , 20 std::vector < datapoint_t > & datapoints , 21 std::map < std::string , std::string > & transcripts ) { 22datapoints .clear (); 23transcripts .clear (); 24printw ("Scanning for files at %s\n" ,data_path .string ().c_str ()); 25for (const auto & entry : std::filesystem::directory_iterator (data_path )) { 26//printw(" Checking file %s\n", entry.path().string().c_str()); 27if (entry .is_regular_file ()) { 28 std::filesystem::path filepath = entry .path (); 29 std::string filename = filepath .stem ().string (); 30 31if (filepath .extension ()== ".wav" ) { 32 std::filesystem::path txt_file = filepath .replace_extension (".txt" ); 33if (std::filesystem::exists (txt_file )) { 34datapoints .emplace_back (filepath .string (),txt_file .string ()); 35 std::ifstream fileStream (txt_file ); 36 std::stringstream buffer ; 37buffer <<fileStream .rdbuf (); 38 std::string contents = buffer .str (); 39contents .erase (std::remove (contents .begin (),contents .end (),'\n' ),contents .cend ()); 40contents .erase (std::remove (contents .begin (),contents .end (),'\r' ),contents .cend ()); 41contents = contents .substr (0 ,TRANSCRIPT_CHARS ); 42transcripts [txt_file .string ()]= contents ; 43 } 44 } 45 } 46 } 47} 48 49 50int main (int argc ,char * argv []) { 51const std::filesystem::path cwd = std::filesystem::current_path (); 52 std::filesystem::path data_path = std::filesystem::current_path (); 53if (argc == 2 ) { 54data_path = std::filesystem::path (argv [1 ]); 55 } 56 57// Initialize ncurses 58initscr (); 59cbreak (); 60noecho (); 61keypad (stdscr , TRUE); 62 63// Clear the screen and wait for 'q' or 'x' 64bool run = true; 65bool redraw = true; 66int idx = 0 ; 67int page_offset = 0 ; 68 69 std::vector < datapoint_t > datapoints ; 70 std::map < std::string , std::string > transcripts ; 71 72 std::string digits ; 73while (run ) { 74clear (); 75 { 76int cur_idx = 0 ; 77getData (data_path ,datapoints ,transcripts ); 78for (const auto & [txt_path ,transcript ] :transcripts ) { 79if (cur_idx < page_offset * PAGE_LINES ) { 80++ cur_idx ; 81continue ; 82 } 83 84char selector = ((cur_idx %PAGE_LINES )== idx ) ?'>' :' ' ; 85printw ("%02d %c %s: %s\n" , (cur_idx %PAGE_LINES ),selector ,txt_path .c_str (),transcript .c_str ()); 86++ cur_idx ; 87 88if (cur_idx >= (page_offset + 1 )* PAGE_LINES ) { 89break ; 90 } 91 } 92 } 93refresh (); 94 95int ch = getch (); 96if (ch == 'q' ) { 97run = false; 98continue ; 99 }else if (ch == 'j' ) { 100int step_sz = 1 ; 101if (digits .size ()> 0 ) { 102step_sz = std::atoi (digits .c_str ()); 103digits .clear (); 104 } 105 106idx += step_sz ; 107idx = std::min (PAGE_LINES - 1 ,idx ); 108 }else if (ch == 'k' ) { 109int step_sz = 1 ; 110if (digits .size ()> 0 ) { 111step_sz = std::atoi (digits .c_str ()); 112digits .clear (); 113 } 114 115idx -= step_sz ; 116idx = std::max (0 ,idx ); 117 }else if (ch == KEY_NPAGE ) { 118++ page_offset ; 119 }else if (ch == KEY_PPAGE ) { 120-- page_offset ; 121page_offset = std::max (0 ,page_offset ); 122 }else if (ch == 'x' ) { 123int cur_idx = 0 ; 124for (const auto & [txt_path ,transcript ] :transcripts ) { 125if (cur_idx != page_offset * PAGE_LINES + idx ) { 126++ cur_idx ; 127continue ; 128 } 129 std::filesystem::path wav_file = std::filesystem::path (txt_path ).replace_extension (".wav" ); 130 std::filesystem::remove (txt_path ); 131 std::filesystem::remove (wav_file ); 132break ; 133 } 134 }else if (ch >='0' && ch <='9' ) { 135digits += ch ; 136 }else if (ch == 'g' ) { 137int target = idx ; 138if (digits .size ()> 0 ) { 139target = std::atoi (digits .c_str ()); 140digits .clear (); 141 } 142 143idx = target ; 144idx = std::min (PAGE_LINES - 1 ,idx ); 145idx = std::max (0 ,idx ); 146 }else if (ch == 27 ) {// ASCII value of esc key 147digits .clear (); 148 } 149 } 150 151// End ncurses mode 152endwin (); 153 154return 0 ; 155} 156