/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running g++ -Wall -O2 -s -o bully bully.cpp */ #include #include #include #include #include #include #include #include #include using namespace std; const string info = "" "bully [files...]\n" "\n" "Tally/count each distinct line, also emitting bullet points. The output is\n" " a TSV table, with headers.\n" ""; typedef map string_tally; void handle_input(istream& in, string& line, string_tally& tally) { while (getline(in, line)) { tally[line]++; } } bool handle_file(const char* path, string& line, string_tally& tally) { ifstream f(path); if (f.is_open()) { handle_input(f, line, tally); return true; } cerr << "\x1b[31mcan't open file named '" << path << "'\x1b[0m" << endl; return false; } // run returns the number of errors int run(int argc, char** argv) { string line; string_tally tally; if (argc > 1) { if ( strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--h") == 0 || strcmp(argv[1], "--help") == 0 ) { cout << info; return 0; } } size_t dashes = 0; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { dashes++; } } if (dashes > 1) { cerr << "\x1b[31mcan't use dash (stdin) more than once\x1b[0m" << endl; return 1; } cin.tie(NULL); ios_base::sync_with_stdio(false); cout << "value\ttally\tbullets" << endl; size_t errors = 0; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { handle_input(cin, line, tally); continue; } if (!handle_file(argv[i], line, tally)) { errors++; } } if (argc < 2) { handle_input(cin, line, tally); } vector sorted; sorted.reserve(tally.size()); for (auto kv = tally.begin(); kv != tally.end(); kv++) { sorted.push_back(kv->first); } sort(sorted.begin(), sorted.end(), [&tally](const auto& x, const auto& y) { auto a = tally[x]; auto b = tally[y]; if (a > b) { return true; } return x < y; }); for (const auto& k : sorted) { auto n = tally[k]; cout << k << '\t' << n << '\t'; while (n > 0) { // cout << "●"; cout << "•"; n--; } cout << endl; if (cout.eof()) { break; } } return errors; } int main(int argc, char** argv) { try { return run(argc, argv) == 0 ? 0 : 1; } catch (const bad_alloc& e) { cerr << "\x1b[31mout of memory\x1b[0m" << endl; return 1; } }