now it haves a config

This commit is contained in:
froslanz
2026-11-12 16:49:04 -04:00
parent 0c62179b69
commit 567a20f0ef
7 changed files with 425 additions and 17 deletions
+1
View File
@@ -10,6 +10,7 @@ pkg_check_modules(XFT REQUIRED IMPORTED_TARGET xft fontconfig)
add_executable(gassyterm
src/main.cpp
src/config.cpp
src/terminal.cpp
src/screen.cpp
src/ansi.cpp
+30
View File
@@ -24,6 +24,34 @@ The shell defaults to `/bin/sh`; pass another one as the first argument. Close
the window or leave the shell (`exit`, `^D`) to quit. The window resizes in whole character
cells and tells the shell about it via `SIGWINCH`.
## Configuration
Settings are read once at startup from `$XDG_CONFIG_HOME/gassyterm/config`, or
`~/.config/gassyterm/config` when `XDG_CONFIG_HOME` is unset. The file is
created with the defaults on first run, and changes apply the next time you
open the window (no rebuild needed).
```ini
# ~/.config/gassyterm/config
font = monospace:size=11
foreground = #cccccc
background = #0b0b0b
cursor = #cccccc
shell = /bin/sh
columns = 80
rows = 24
color4 = #2472c8 # optional: override any of color0..color15
```
- `font` is a fontconfig pattern, so size, family and style go in one string
(`"Iosevka:size=13:weight=medium"`). If it cannot be matched, a generic
monospace font is used instead.
- Colours are `#rrggbb`. `cursor` falls back to `foreground`.
- `shell` is only used when no shell is given on the command line.
- `columns` and `rows` set the initial window size in cells.
- Unknown keys and malformed values are reported on stderr and ignored, so a
typo never stops the terminal from starting.
## How it works
- `src/pty.cpp` opens a pty pair (`posix_openpt`), forks, attaches the slave as
@@ -37,6 +65,8 @@ cells and tells the shell about it via `SIGWINCH`.
cursor, and UTF-8 decoding into cells.
- `src/screen.h` is the cell grid; each cell keeps up to four bytes of UTF-8,
foreground/background palette indices and attribute bits.
- `src/config.cpp` reads the startup settings from the XDG config directory
and creates a commented default file when there is none.
- `src/x11.cpp` is the window: display and window setup, fontconfig/Xft font
loading, the 256-colour palette, key mapping (modelled on `st`), and
cell-by-cell rendering into a pixmap that is blitted on expose.
+283
View File
@@ -0,0 +1,283 @@
#include "config.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <cerrno>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
namespace gassyterm {
namespace {
const char kDefaultFile[] =
"# gassyterm configuration.\n"
"# Read once, when the window opens; changes apply the next time you\n"
"# start it. Format: key = value. '#' starts a comment.\n"
"\n"
"# Font pattern as understood by fontconfig, including the size.\n"
"font = monospace:size=11\n"
"\n"
"# Colours are #rrggbb. The cursor block follows the foreground unless\n"
"# you give it its own colour.\n"
"foreground = #cccccc\n"
"background = #0b0b0b\n"
"cursor = #cccccc\n"
"\n"
"# Shell to run when none is given on the command line.\n"
"shell = /bin/sh\n"
"\n"
"# Initial window size, in character cells.\n"
"columns = 80\n"
"rows = 24\n"
"\n"
"# Optional overrides for the 16 ANSI base colours (color0 = black ...).\n"
"# color0 = #000000\n"
"# color1 = #cd3131\n"
"# color2 = #0dbc79\n"
"# color3 = #e5e510\n"
"# color4 = #2472c8\n"
"# color5 = #bc3fbc\n"
"# color6 = #11a8cd\n"
"# color7 = #e5e5e5\n"
"# color8 = #666666\n"
"# color9 = #f14c4c\n"
"# color10 = #23d18b\n"
"# color11 = #f5f543\n"
"# color12 = #3b8eea\n"
"# color13 = #d670d6\n"
"# color14 = #29b8db\n"
"# color15 = #ffffff\n";
std::string trim(const std::string& text) {
size_t begin = text.find_first_not_of(" \t\r\n");
if (begin == std::string::npos) return std::string();
size_t end = text.find_last_not_of(" \t\r\n");
return text.substr(begin, end - begin + 1);
}
std::string unquote(const std::string& text) {
if (text.size() >= 2 && (text.front() == '"' || text.front() == '\'') &&
text.back() == text.front()) {
return text.substr(1, text.size() - 2);
}
return text;
}
std::string lowercase(const std::string& text) {
std::string result = text;
for (char& c : result) {
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return result;
}
int hexValue(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
std::string parentDirectory(const std::string& file) {
size_t slash = file.find_last_of('/');
if (slash == std::string::npos) return std::string(".");
if (slash == 0) return std::string("/");
return file.substr(0, slash);
}
// mkdir -p, so ~/.config/gassyterm exists even on a bare home directory.
bool makeDirectories(const std::string& dir) {
std::string partial;
size_t pos = 0;
while (pos <= dir.size()) {
size_t next = dir.find('/', pos);
if (next == std::string::npos) next = dir.size();
partial = dir.substr(0, next);
if (!partial.empty() && mkdir(partial.c_str(), 0755) != 0 &&
errno != EEXIST) {
return false;
}
pos = next + 1;
}
return true;
}
void writeDefaultFile(const std::string& file) {
const std::string dir = parentDirectory(file);
if (!makeDirectories(dir)) {
std::fprintf(stderr, "gassyterm: cannot create %s\n", dir.c_str());
return;
}
std::ofstream out(file);
if (!out) {
std::fprintf(stderr, "gassyterm: cannot write %s\n", file.c_str());
return;
}
out << kDefaultFile;
}
bool readInteger(const std::string& text, int minimum, int* out) {
if (text.empty()) return false;
const char* begin = text.c_str();
char* end = nullptr;
long value = std::strtol(begin, &end, 10);
if (end == begin || *end != '\0') return false;
if (value < minimum || value > 4096) return false;
*out = static_cast<int>(value);
return true;
}
} // namespace
bool parseColor(const std::string& text, int* r, int* g, int* b) {
std::string hex = text;
if (!hex.empty() && hex[0] == '#') hex.erase(0, 1);
if (hex.size() != 6) return false;
int values[3] = {0, 0, 0};
for (int i = 0; i < 3; ++i) {
int high = hexValue(hex[static_cast<size_t>(i) * 2]);
int low = hexValue(hex[static_cast<size_t>(i) * 2 + 1]);
if (high < 0 || low < 0) return false;
values[i] = high * 16 + low;
}
*r = values[0];
*g = values[1];
*b = values[2];
return true;
}
std::string Config::path() {
const char* xdg = std::getenv("XDG_CONFIG_HOME");
if (xdg != nullptr && xdg[0] != '\0') {
return std::string(xdg) + "/gassyterm/config";
}
const char* home = std::getenv("HOME");
if (home != nullptr && home[0] != '\0') {
return std::string(home) + "/.config/gassyterm/config";
}
return std::string(".gassyterm-config");
}
bool Config::loadFile(const std::string& file, Config* out) {
std::ifstream in(file);
if (!in) return false;
std::string line;
int number = 0;
while (std::getline(in, line)) {
++number;
const std::string text = trim(line);
if (text.empty() || text[0] == '#') continue;
size_t equals = text.find('=');
if (equals == std::string::npos) {
std::fprintf(stderr, "gassyterm: %s:%d: expected key = value\n",
file.c_str(), number);
continue;
}
const std::string key = lowercase(trim(text.substr(0, equals)));
const std::string value = unquote(trim(text.substr(equals + 1)));
if (key == "font") {
if (value.empty()) {
std::fprintf(stderr, "gassyterm: %s:%d: empty font\n", file.c_str(),
number);
} else {
out->font = value;
}
} else if (key == "foreground") {
int r = 0, g = 0, b = 0;
if (parseColor(value, &r, &g, &b)) {
out->foreground = value;
} else {
std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n",
file.c_str(), number, value.c_str());
}
} else if (key == "background") {
int r = 0, g = 0, b = 0;
if (parseColor(value, &r, &g, &b)) {
out->background = value;
} else {
std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n",
file.c_str(), number, value.c_str());
}
} else if (key == "cursor") {
int r = 0, g = 0, b = 0;
if (value.empty()) {
out->cursor.clear();
} else if (parseColor(value, &r, &g, &b)) {
out->cursor = value;
} else {
std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n",
file.c_str(), number, value.c_str());
}
} else if (key == "shell") {
if (value.empty()) {
std::fprintf(stderr, "gassyterm: %s:%d: empty shell\n", file.c_str(),
number);
} else {
out->shell = value;
}
} else if (key == "columns") {
if (!readInteger(value, 1, &out->columns)) {
std::fprintf(stderr, "gassyterm: %s:%d: bad columns \"%s\"\n",
file.c_str(), number, value.c_str());
}
} else if (key == "rows") {
if (!readInteger(value, 1, &out->rows)) {
std::fprintf(stderr, "gassyterm: %s:%d: bad rows \"%s\"\n",
file.c_str(), number, value.c_str());
}
} else if (key.size() == 6 && key.compare(0, 5, "color") == 0 &&
key[5] >= '0' && key[5] <= '9') {
int index = key[5] - '0';
int r = 0, g = 0, b = 0;
if (parseColor(value, &r, &g, &b)) {
out->colors[index] = value;
} else {
std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n",
file.c_str(), number, value.c_str());
}
} else if (key.size() == 7 && key.compare(0, 5, "color") == 0 &&
key[5] >= '1' && key[5] <= '5' && key[6] >= '0' &&
key[6] <= '9') {
int index = (key[5] - '0') * 10 + (key[6] - '0');
if (index < 16) {
int r = 0, g = 0, b = 0;
if (parseColor(value, &r, &g, &b)) {
out->colors[index] = value;
} else {
std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n",
file.c_str(), number, value.c_str());
}
} else {
std::fprintf(stderr, "gassyterm: %s:%d: unknown key \"%s\"\n",
file.c_str(), number, key.c_str());
}
} else {
std::fprintf(stderr, "gassyterm: %s:%d: unknown key \"%s\"\n",
file.c_str(), number, key.c_str());
}
}
return true;
}
Config Config::load() {
Config config;
const std::string file = path();
struct stat info {};
if (stat(file.c_str(), &info) != 0) writeDefaultFile(file);
loadFile(file, &config);
if (config.cursor.empty()) config.cursor = config.foreground;
return config;
}
} // namespace gassyterm
+53
View File
@@ -0,0 +1,53 @@
// src/config.h — startup configuration read from the XDG config directory.
//
// gassyterm reads $XDG_CONFIG_HOME/gassyterm/config (normally
// ~/.config/gassyterm/config) once, when the window opens. A commented file
// with the defaults is created on first run.
#ifndef GASSYTERM_SRC_CONFIG_H_
#define GASSYTERM_SRC_CONFIG_H_
#include <string>
#include "screen.h"
namespace gassyterm {
struct Config {
// Font pattern as understood by fontconfig, including the size.
std::string font = "monospace:size=11";
// Colours as "#rrggbb". An empty cursor colour follows the foreground.
std::string foreground = "#cccccc";
std::string background = "#0b0b0b";
std::string cursor;
// Shell used when none is given on the command line.
std::string shell = "/bin/sh";
// Optional overrides for the 16 ANSI base colours; empty means built in.
std::string colors[16];
// Initial window size in character cells.
int columns = kDefaultWidth;
int rows = kDefaultHeight;
// Returns the configuration, creating a default file if there is none.
// Invalid lines are reported on stderr and ignored, so the result is
// always usable.
static Config load();
// Path of the configuration file (~/.config/gassyterm/config by default).
static std::string path();
// Reads one file into `out`; returns false if it cannot be opened.
static bool loadFile(const std::string& file, Config* out);
};
// Parses "#rrggbb" (or "rrggbb") into 0-255 components. Returns false on any
// other input, leaving the outputs untouched.
bool parseColor(const std::string& text, int* r, int* g, int* b);
} // namespace gassyterm
#endif // GASSYTERM_SRC_CONFIG_H_
+7 -4
View File
@@ -6,6 +6,9 @@
//
// Build: cmake -S . -B build && cmake --build build
// Run: ./build/gassyterm [shell]
//
// Settings are read once at startup from $XDG_CONFIG_HOME/gassyterm/config
// (normally ~/.config/gassyterm/config).
#include <sys/select.h>
#include <sys/wait.h>
@@ -17,16 +20,16 @@
#include <cstdio>
#include <cstdlib>
#include "config.h"
#include "pty.h"
#include "terminal.h"
#include "x11.h"
int main(int argc, char** argv) {
const char* shell = "/bin/sh";
if (argc > 1) shell = argv[1];
const gassyterm::Config config = gassyterm::Config::load();
const char* shell = (argc > 1) ? argv[1] : config.shell.c_str();
gassyterm::Window window(gassyterm::kDefaultWidth, gassyterm::kDefaultHeight,
"gassyterm");
gassyterm::Window window(config, "gassyterm");
if (!window.ok()) {
std::fprintf(stderr, "gassyterm: cannot open X display\n");
return 1;
+47 -12
View File
@@ -81,6 +81,22 @@ void allocColor(Display* dpy, int screen, Colormap cmap, XftColor* out,
}
}
// Resolves a "#rrggbb" setting against a built-in default.
void colorOr(const std::string& text, const int fallback[3], int out[3],
const char* key) {
out[0] = fallback[0];
out[1] = fallback[1];
out[2] = fallback[2];
if (text.empty()) return;
if (!parseColor(text, &out[0], &out[1], &out[2])) {
std::fprintf(stderr, "gassyterm: %s: bad colour \"%s\"\n", key,
text.c_str());
out[0] = fallback[0];
out[1] = fallback[1];
out[2] = fallback[2];
}
}
XftFont* openFont(Display* dpy, const char* name, int weight) {
FcPattern* pattern = FcNameParse(reinterpret_cast<const FcChar8*>(name));
if (pattern == nullptr) return nullptr;
@@ -110,9 +126,9 @@ std::string cellText(const Cell& cell) {
} // namespace
Window::Window(int cols, int rows, const char* title) {
cols_ = std::max(1, cols);
rows_ = std::max(1, rows);
Window::Window(const Config& config, const char* title) : cfg_(config) {
cols_ = std::max(1, config.columns);
rows_ = std::max(1, config.rows);
dpy_ = XOpenDisplay(nullptr);
if (dpy_ == nullptr) return;
@@ -153,9 +169,11 @@ Window::~Window() {
}
void Window::loadFonts() {
static const char* kFontNames[] = {"monospace:size=11", "monospace:size=10",
"monospace:size=12", "fixed"};
for (const char* name : kFontNames) {
// The configured pattern first, then generic fallbacks, so a typo in the
// config still leaves a usable window.
const char* candidates[] = {cfg_.font.c_str(), "monospace:size=11",
"monospace:size=10", "fixed"};
for (const char* name : candidates) {
font_ = openFont(dpy_, name, -1);
if (font_ != nullptr) break;
}
@@ -163,7 +181,7 @@ void Window::loadFonts() {
std::fprintf(stderr, "gassyterm: could not load a monospace font\n");
return;
}
bold_font_ = openFont(dpy_, "monospace:size=11", FC_WEIGHT_BOLD);
bold_font_ = openFont(dpy_, cfg_.font.c_str(), FC_WEIGHT_BOLD);
if (bold_font_ == nullptr) bold_font_ = font_;
// Measure the real advance of a wide glyph: for some fonts
@@ -187,8 +205,17 @@ void Window::loadColors() {
{59, 142, 234}, {214, 112, 214}, {41, 184, 219}, {255, 255, 255},
};
for (int i = 0; i < 16; ++i) {
allocColor(dpy_, screen_, cmap_, &palette_[i], kBase[i][0], kBase[i][1],
kBase[i][2]);
int r = kBase[i][0];
int g = kBase[i][1];
int b = kBase[i][2];
if (!cfg_.colors[i].empty() && !parseColor(cfg_.colors[i], &r, &g, &b)) {
std::fprintf(stderr, "gassyterm: color%d: bad colour \"%s\"\n", i,
cfg_.colors[i].c_str());
r = kBase[i][0];
g = kBase[i][1];
b = kBase[i][2];
}
allocColor(dpy_, screen_, cmap_, &palette_[i], r, g, b);
}
// 216-colour cube.
@@ -206,9 +233,17 @@ void Window::loadColors() {
allocColor(dpy_, screen_, cmap_, &palette_[232 + i], v, v, v);
}
allocColor(dpy_, screen_, cmap_, &default_fg_, 204, 204, 204);
allocColor(dpy_, screen_, cmap_, &default_bg_, 11, 11, 11);
allocColor(dpy_, screen_, cmap_, &cursor_color_, 204, 204, 204);
static const int kDefaultFg[3] = {204, 204, 204};
static const int kDefaultBg[3] = {11, 11, 11};
int fg[3] = {204, 204, 204};
int bg[3] = {11, 11, 11};
int cur[3] = {204, 204, 204};
colorOr(cfg_.foreground, kDefaultFg, fg, "foreground");
colorOr(cfg_.background, kDefaultBg, bg, "background");
colorOr(cfg_.cursor, fg, cur, "cursor");
allocColor(dpy_, screen_, cmap_, &default_fg_, fg[0], fg[1], fg[2]);
allocColor(dpy_, screen_, cmap_, &default_bg_, bg[0], bg[1], bg[2]);
allocColor(dpy_, screen_, cmap_, &cursor_color_, cur[0], cur[1], cur[2]);
}
void Window::createWindow(const char* title) {
+4 -1
View File
@@ -12,6 +12,7 @@
#include <string>
#include "config.h"
#include "terminal.h"
namespace gassyterm {
@@ -25,7 +26,7 @@ class Window {
bool closed = false; // window manager asked us to quit
};
Window(int cols, int rows, const char* title);
Window(const Config& config, const char* title);
~Window();
Window(const Window&) = delete;
@@ -63,6 +64,8 @@ class Window {
XftDraw* draw_ = nullptr;
Cursor cursor_ = 0;
Config cfg_;
XftFont* font_ = nullptr;
XftFont* bold_font_ = nullptr;