cli MVP
This commit is contained in:
parent
367c19386c
commit
1ea50c50bc
62 changed files with 9982 additions and 608 deletions
|
|
@ -1,10 +1,12 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
#include <cerrno>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
module Nostro:File_impl;
|
||||
import :File;
|
||||
|
|
@ -13,31 +15,33 @@ import std;
|
|||
using namespace Nostro;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
// An environment variable as a path, or nothing when it is not set.
|
||||
std::optional<fs::path> Environment(const std::string& name) {
|
||||
// getenv answers with a char*, null when the variable is not set.
|
||||
// lint-disable-next-line no-char-pointer
|
||||
const char* const value = std::getenv(name.c_str());
|
||||
return value ? std::optional<fs::path>(value) : std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
fs::path Nostro::GetConfigPath() {
|
||||
const char* const xdg = std::getenv("XDG_CONFIG_HOME");
|
||||
if(xdg) {
|
||||
return fs::path(fs::path(std::move(xdg)) / ".config/nostro");
|
||||
if(const std::optional<fs::path> xdg = Environment("XDG_CONFIG_HOME")) {
|
||||
return *xdg / ".config/nostro";
|
||||
}
|
||||
|
||||
const char* const home = std::getenv("HOME");
|
||||
if(home) {
|
||||
return fs::path(fs::path(std::move(home)) / ".config/nostro");
|
||||
if(const std::optional<fs::path> home = Environment("HOME")) {
|
||||
return *home / ".config/nostro";
|
||||
}
|
||||
|
||||
throw std::runtime_error("XDG_CONFIG_HOME and HOME not set");
|
||||
}
|
||||
|
||||
fs::path Nostro::GetCachePath() {
|
||||
const char* const xdg = std::getenv("XDG_CACHE_HOME");
|
||||
if(xdg) {
|
||||
return fs::path(fs::path(std::move(xdg)) / "nostro");
|
||||
if(const std::optional<fs::path> xdg = Environment("XDG_CACHE_HOME")) {
|
||||
return *xdg / "nostro";
|
||||
}
|
||||
|
||||
const char* const home = std::getenv("HOME");
|
||||
if(home) {
|
||||
return fs::path(fs::path(std::move(home)) / ".cache/nostro");
|
||||
if(const std::optional<fs::path> home = Environment("HOME")) {
|
||||
return *home / ".cache/nostro";
|
||||
}
|
||||
|
||||
throw std::runtime_error("XDG_CACHE_HOME and HOME not set");
|
||||
}
|
||||
|
||||
|
|
@ -60,22 +64,28 @@ void Nostro::CreatePrivateFile(const fs::path& path) {
|
|||
}
|
||||
}
|
||||
|
||||
void Nostro::WriteString(std::ofstream& file, std::string_view str) {
|
||||
std::uint16_t size = str.size();
|
||||
file.write(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
file.write(str.data(), size);
|
||||
}
|
||||
std::string Nostro::ReadSecret(std::string_view prompt) {
|
||||
const bool terminal = isatty(STDIN_FILENO) == 1;
|
||||
termios shown{};
|
||||
if(terminal) {
|
||||
std::print(std::cerr, "{}", prompt);
|
||||
tcgetattr(STDIN_FILENO, &shown);
|
||||
termios hidden = shown;
|
||||
hidden.c_lflag &= ~static_cast<tcflag_t>(ECHO);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &hidden);
|
||||
}
|
||||
|
||||
void Nostro::ReadString(std::ifstream& file, std::string& str) {
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
str.resize(size);
|
||||
file.read(str.data(), size);
|
||||
}
|
||||
std::string line;
|
||||
const bool read = static_cast<bool>(std::getline(std::cin, line));
|
||||
if(terminal) {
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &shown);
|
||||
std::println(std::cerr);
|
||||
}
|
||||
if(!read) {
|
||||
throw std::runtime_error(std::format("nothing given for '{}'", prompt));
|
||||
}
|
||||
|
||||
std::uint16_t Nostro::SeekString(std::ifstream& file) {
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
file.seekg(size, std::ios::cur);
|
||||
return size;
|
||||
}
|
||||
const std::size_t first = line.find_first_not_of(" \t\r");
|
||||
const std::size_t last = line.find_last_not_of(" \t\r");
|
||||
return first == std::string::npos ? std::string() : line.substr(first, last - first + 1);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue