Added NO_MIME option

This commit is contained in:
pavlik_dev 2024-08-23 20:01:16 +03:00 committed by GitHub
parent d3a4864708
commit bebe7ec0a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,10 +1,14 @@
// Set this if you don't want MIME types
//#define NO_MIME
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <sys/stat.h>
#include <cstring>
#include <unistd.h>
#ifdef NO_MIME
#include <magic.h>
#endif
#include <string>
#include <vector>
#include <sys/types.h>
@ -13,11 +17,12 @@
enum FileType {
Unknown,
File, Folder,
#ifndef NO_MIME
Archive, Audio,
Application,
Video, Binary,
Text
#endif
};
std::vector<int> get_files_in_directory(const std::string& path)
@ -117,6 +122,7 @@ bool is_archive(const std::string& mime_type)
std::string get_name_from_filetype(const enum FileType fs)
{
switch (fs) {
#ifndef NO_MIME
case FileType::Application:
return "Application";
case FileType::Archive:
@ -125,14 +131,17 @@ std::string get_name_from_filetype(const enum FileType fs)
return "Audio";
case FileType::Binary:
return "Binary";
#endif
case FileType::File:
return "File";
case FileType::Folder:
return "Folder";
#ifndef NO_MIME
case FileType::Text:
return "Text";
case FileType::Video:
return "Video";
#endif
default:
return "Unknown";
}
@ -140,6 +149,7 @@ std::string get_name_from_filetype(const enum FileType fs)
std::string get_emoji_from_filetype(const enum FileType fs)
{
switch (fs) {
#ifndef NO_MIME
case FileType::Application:
return "⚙️";
case FileType::Archive:
@ -148,14 +158,17 @@ std::string get_emoji_from_filetype(const enum FileType fs)
return "🎵";
case FileType::Binary:
return "⚙️";
#endif
case FileType::File:
return "📄";
case FileType::Folder:
return "📁";
#ifndef NO_MIME
case FileType::Text:
return "📝";
case FileType::Video:
return "🎥";
#endif
default:
return "?";
}
@ -182,6 +195,7 @@ std::string readable_fs(const long int size /*in bytes*/, const double divide_by
return str_result;
}
#ifndef NO_MIME
std::string get_mime(const std::string& filename)
{
const char* file_path = filename.c_str();
@ -213,6 +227,7 @@ std::string get_mime(const std::string& filename)
return result;
}
#endif
std::string print_permissions(mode_t mode, bool is_dir) {
std::string user;
@ -290,6 +305,9 @@ int main(int argc, char *argv[])
if(!is_file) name += "/";
std::string mime_type = get_mime(full_name);
FileType file;
#ifdef NO_MIME
file = (is_file) ? FileType::File : FileType::Folder;
#else
if (starts_with("inode/directory", mime_type) || !is_file) {
file = FileType::Folder;
} else if (is_archive(mime_type)) {
@ -307,6 +325,7 @@ int main(int argc, char *argv[])
} else {
file = FileType::Unknown;
}
#endif
std::string type = get_name_from_filetype(file);
std::string emoji = get_emoji_from_filetype(file);
if (emojis) printf("%s ", emoji.c_str());
@ -314,7 +333,9 @@ int main(int argc, char *argv[])
printf("\tName: %s\n", name.c_str());
printf("\tType: %s\n", type.c_str());
if (absolute) printf("\tAbsolute path: %s\n", full_name.c_str());
#ifndef NO_MIME
printf("\tMIME: %s\n", mime_type.c_str());
#endif
if (is_file) {
std::string _1024 = readable_fs(file_stat.st_size, 1000, "B");
std::string _1000 = readable_fs(file_stat.st_size, 1024, "iB");