From 28cfbbf62324152e6ab7d969d41b3f8a2bc83741 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Fri, 3 Dec 2021 21:10:43 +0300 Subject: [PATCH] Rewrite in C++ --- .gitignore | 4 +- CONTRIBUTING.md | 19 ------- Cargo.lock | 115 ----------------------------------------- Cargo.toml | 13 ----- LICENSE | 6 +-- Makefile | 17 ++++++ README.md | 81 ----------------------------- install.sh | 18 ++----- src/fumosay.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 109 --------------------------------------- 10 files changed, 159 insertions(+), 357 deletions(-) delete mode 100644 CONTRIBUTING.md delete mode 100644 Cargo.lock delete mode 100644 Cargo.toml create mode 100644 Makefile delete mode 100644 README.md create mode 100644 src/fumosay.cpp delete mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 074194e..704f48a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ -/target -.cargo/ releasefiles/ -fumosay \ No newline at end of file +fumosay diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index e6e6ea8..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,19 +0,0 @@ -# Thank you for being interested in contributing to fumosay ! - -Fumosay is very small, so it's not like there's a lot work involved to ask for help, but -I will be very thankful to you if you decide to add some new fumos in fumofiles ! - -If you don`t know what steps to follow in order to start contributing: [consider to look here](https://www.dataschool.io/how-to-contribute-on-github/) - -``` - __ __ - | \ / | () - | \____/ | () - |__// V \\__| - /|| ᗜ ᗜ || \ - \_\\_︶_//_/ - jj⑨lll - ()_/@V@V@V\_() - /@V@V@V@V\ - (_)-----(_) -``` \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index c269545..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,115 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi", -] - -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "clap" -version = "2.33.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" -dependencies = [ - "ansi_term", - "atty", - "bitflags", - "strsim", - "textwrap", - "unicode-width", - "vec_map", -] - -[[package]] -name = "fumosay" -version = "0.4.3" -dependencies = [ - "clap", -] - -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - -[[package]] -name = "libc" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765" - -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - -[[package]] -name = "unicode-width" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" - -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index 755c957..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "fumosay" -version = "0.4.3" -edition = "2018" -authors = ["Unbewohnte | Nikolay Kasyanov "] -description = "Like cowsay, but with soft friends" -readme = "README.md" -license = "MIT" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -clap = "~2.33.3" diff --git a/LICENSE b/LICENSE index a08fc5c..9537d71 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,7 @@ -The MIT License (MIT) - -Copyright © 2021 Unbewohne | Nikolay Kasyanov +Copyright © 2021 Kasyanov Nikolay Alexeevich (Unbewohnte (me@unbewohnte.xyz)) 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. +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. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2bd65ee --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +SRC_DIR := src +FUMOSAY_BIN_NAME := fumosay +FUMOFILES := fumofiles +FUMOSAY_INSTALLATION_DIR := /usr/local/bin +FUMOFILES_DST_DIR := /usr/local/share/fumosay + +all: + g++ -O2 -Wall -Werror -static src/fumosay.cpp -o fumosay + +install: all + mkdir -p $(FUMOSAY_INSTALLATION_DIR) && \ + mkdir -p $(FUMOSAY_INSTALLATION_DIR) && \ + cp -r $(FUMOFILES) $(FUMOFILES_DST_DIR) && \ + cp $(FUMOSAY_BIN_NAME) $(FUMOSAY_INSTALLATION_DIR) + +clean: + rm fumosay \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 753f08d..0000000 --- a/README.md +++ /dev/null @@ -1,81 +0,0 @@ -# fumosay -## Like cowsay, but with soft friends (ᗜˬᗜ) -``` - __ __ <⑨ There are no buses in Gensokyo ⑨> - | \ / | () - | \____/ | () - |__// V \\__| - /|| ᗜ ᗜ || \ - \_\\_︶_//_/ - jj⑨lll - ()_/@V@V@V\_() - /@V@V@V@V\ - (_)-----(_) -``` ---- - -# Installation - -## "Compile it yourself" way -install Rust -- [installation instructions](https://www.rust-lang.org/tools/install) - -clone this repository -- `git clone https://github.com/Unbewohnte/fumosay` - -cd into cloned repo -- `cd fumosay/` - -compile for your OS && Architecture -- `cargo build --release` - -move compiled executable to the current directory -- `mv target/release/fumosay .` or `mv target/[target_name]/release/fumosay .` - -**or** - -## "Download a pre-compiled version (Linux amd64 only)" way -- [Download a version of your choice](https://github.com/Unbewohnte/fumosay/releases) - -unzip -- `unzip [zip_archive_name]` or `7z x [7z_archive_name]` - -cd into unzipped directory -- `cd fumosay/` - -## After compilation|downloading - -run installation script -- `chmod +x install.sh && sudo ./install.sh` -**binary file and fumofiles must be in the same directory as install.sh !** - -**Now you have fumosay installed !** - ---- - -# Use -``` -fumosay message_here -``` -prints a message with a default fumo.fumo template - -``` -fumosay -f mini.fumo message_here -``` -prints a message with a mini.fumo template. You can add your own fumofiles -in /usr/share/fumosay/fumofiles/ and use them with -f flag - -``` -fumosay -d path/to/your/fumofiles/ -f your_fumo.fumo fumofumo -``` -uses a non-default path to fumofiles and a custom fumofile - ---- - -# TODO list - -- Add more fumos -- ~~Make it possible to use other fumos~~ -- Improve message {box|bubble} -- ~~Embed fumofiles or take care of ways of locating them~~ -- ~~Make an `install.sh` script~~ diff --git a/install.sh b/install.sh index 7c2e76c..1e5c7e9 100755 --- a/install.sh +++ b/install.sh @@ -8,27 +8,19 @@ FUMOFILES_DIR_PATH=/usr/local/share/fumosay/fumofiles/ mkdir -p $FUMOSAY_BIN_DIR_PATH && mkdir -p $FUMOFILES_DIR_PATH # copy fumosay binary -if [ -e fumosay ] +if [ -e fumosay ] then - cp fumosay $FUMOSAY_BIN_DIR_PATH && echo "Copied binary file" -elif [ -e target/x86_64-unknown-linux-musl/release/fumosay ] # For debug purposes. The binary file should be in the same directory as the script !!! -then - cp target/x86_64-unknown-linux-musl/release/fumosay $FUMOSAY_BIN_DIR_PATH && echo "Copied binary from linux-musl directory" + cp fumosay $FUMOSAY_BIN_DIR_PATH else - echo "Could not find a fumosay binary file" + echo "Could not find fumosay in current directory" exit 1 fi # copy fumofiles if [ -d fumofiles ] then - cp -r fumofiles/* $FUMOFILES_DIR_PATH && echo "Copied fumofiles" + cp -r fumofiles/* $FUMOFILES_DIR_PATH else echo "Could not find fumofiles directory" exit 1 -fi - - -echo "Installation completed" - - +fi \ No newline at end of file diff --git a/src/fumosay.cpp b/src/fumosay.cpp new file mode 100644 index 0000000..fc235ab --- /dev/null +++ b/src/fumosay.cpp @@ -0,0 +1,134 @@ +/* +Copyright © 2021 Kasyanov Nikolay Alexeevich (Unbewohnte (me@unbewohnte.xyz)) + +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. +*/ + +#include +#include +#include +#include + +class Fumo { + private: + const std::string m_message_identificator = "!message"; + const std::string m_default_fumofile = "cirno.fumo"; + + std::string m_fumotext; + + public: + // Opens specified fumofile if present and reads it + Fumo(std::string fumofile, std::string fumofiles_path) { + std::string path = fumofiles_path + "/" + fumofile; + + std::fstream m_fumofile; + m_fumofile.open(path, std::ios_base::in); + if (!m_fumofile) { + // could not open specified fumofile + // try to open the default one + path = fumofiles_path + "/" + m_default_fumofile; + m_fumofile.open(path, std::ios_base::in); + if (!m_fumofile) { + // if THIS didn`t help - raise an error + throw "Could not open fumofile"; + }; + }; + + // read the whole fumofile + std::string line; + while (std::getline(m_fumofile, line)) { + m_fumotext += line + "\n"; + }; + + m_fumofile.close(); + }; + + ~Fumo() {}; + + // parses already present fumotext and inserts given message. Prints fumosay + void say(std::string message) { + if (m_fumotext.find(m_message_identificator) == std::string::npos) { + // no identificator found. Inserting message on the first line + m_fumotext.insert(0, message + "\n"); + } else { + // identificator found. Inserting message in the appropriate place + const unsigned int message_index = m_fumotext.find(m_message_identificator); + m_fumotext.replace(message_index, m_message_identificator.length(), message); + }; + + // print fumosay + std::cout << m_fumotext << std::endl; + }; +}; + +int main(int argc, char *argv[]) { + const std::string VERSION = "0.4.4"; + const std::string ABOUT = "fumosay\ncowsay, but with soft friends\n\nWritten by Kasyanov Nikolay Alexeevich (Unbewohnte (me@unbewohnte.xyz))"; + const std::string HELP = "fumosay [OPTION(s)...] [MESSAGE]\n\nOptions:\n-v --version - print version text and exit\n-l --list - list available fumos and exit\n-h --help - print this message and exit\n-f --fumo - specify another fumo to print\n-d --directory - temporarily change fumofiles directory to look fumofiles in\n"; + + + std::string fumofiles_dir = "/usr/local/share/fumosay/fumofiles"; + std::string fumoname = "cirno.fumo"; + + if (argc < 2) { + return 0; + }; + + std::string message; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) { + // print version info + std::cout << VERSION << std::endl << std::endl << ABOUT << std::endl; + return 0; + + } else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list") == 0) { + // print available fumos in the default fumofiles directory + for (const auto &entry : std::filesystem::directory_iterator(fumofiles_dir)) { + std::cout << entry.path().filename() << std::endl; + }; + + return 0; + + } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + // print help infos + std::cout << HELP << std::endl; + return 0; + + } else if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--fumo") == 0) { + // use another fumo + if (i+1 >= argc-1) { + return 1; + }; + + fumoname = argv[i+1]; + i++; + + continue; + + } else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--directory") == 0) { + // temporarily change fumofiles directory to look fumofiles in + if (i+1 >= argc-1 || !std::filesystem::exists(std::filesystem::path(argv[i+1]))){ + return 1; + }; + + fumofiles_dir = argv[i+1]; + i++; + + continue; + }; + + message += argv[i]; + if (i != argc-1) { + message += " "; + }; + }; + + Fumo fumo(fumoname, fumofiles_dir); + fumo.say(message); + + return 0; +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 5cbaed3..0000000 --- a/src/main.rs +++ /dev/null @@ -1,109 +0,0 @@ -use clap::{App, Arg}; -use std::fs; -use std::path::Path; - -/// Indicator of where the message should be in fumofile -const MESSAGE_INDICATOR: &str = "!message"; -/// Fumofile name of the default fumo -const FUMO_DEFAULT: &str = "cirno.fumo"; -/// Default directory where fumofiles are placed -const DEFAULT_FUMOFILES_DIR: &str = "/usr/local/share/fumosay/fumofiles/"; - -/// Returns a resulting string with `MESSAGE_INDICATOR` replaced with given -/// `message`. If `MESSAGE_INDICATOR` is not present in fumofile - the -/// `message` will be added on the new line at the end of the fumofile. -fn process_message(fumofile_contents: &mut String, message: &str) -> String { - if fumofile_contents.contains(MESSAGE_INDICATOR) { - return fumofile_contents.replace(MESSAGE_INDICATOR, message); - } - return format!("{}\n{}", fumofile_contents, message); -} - -fn main() { - // get command line arguments - let matches = App::new("fumosay") - .version("0.4.3") - .author("Unbewohnte | Nikolay Kasyanov ") - .about("cowsay, but with soft friends") - .arg( - Arg::with_name("fumo") - .short("f") - .long("fumo") - .value_name("fumofile") - .help("Choose another fumofumo to print") - .takes_value(true) - .required(false) - .default_value(FUMO_DEFAULT), - ) - .arg( - Arg::with_name("fumofiles_directory") - .short("d") - .long("fumofiles_directory") - .value_name("fumofiles_directory_path") - .help("Look for fumofiles in given directory") - .takes_value(true) - .required(false) - .default_value(DEFAULT_FUMOFILES_DIR), - ) - .arg( - Arg::with_name("list_fumos") - .short("l") - .long("list_fumos") - .help("Lists all fumofiles in the default directory instead") - .takes_value(false) - .required(false), - ) - .arg( - Arg::with_name("message") - .short("m") - .long("message") - .help("Message to print") - .takes_value(true) - .index(1) - .multiple(true), - ) - .get_matches(); - - // check for list_fumos flag. - if matches.is_present("list_fumos") { - // list fumofiles and exit - let fumofiles = fs::read_dir(DEFAULT_FUMOFILES_DIR) - .expect("Could not read default fumofiles directory"); - for entry in fumofiles { - let file = entry.unwrap(); - let filename = file.file_name(); - print!("{:?}\n", filename); - } - std::process::exit(0); - } - - // process fumofiles directory - - // directory with all fumofiles - let new_fumofiles_dir = matches.value_of("fumofiles_directory").unwrap(); - // name of the fumofile to work with - let fumofile_name = matches.value_of("fumo").unwrap(); - // the whole path to the selected fumo - let fumofile_path = Path::new(&new_fumofiles_dir).join(fumofile_name); - - // read fumofile - let mut fumofile_contents: String = - fs::read_to_string(fumofile_path).expect("Could not find a fumofile!"); - - // process the message - let message = matches.values_of("message"); - match message { - Some(messages) => { - let message_vec: Vec<&str> = messages.collect(); - let joined_message = message_vec.join(" "); - - // parse the file, insert fiven message and get the resulting string - let fumosay: String = process_message(&mut fumofile_contents, joined_message.as_str()); - - println!("{}", fumosay); - } - None => { - print!("{}", matches.usage()) - } - } -}