feat: add config file

This commit is contained in:
atagen 2025-06-22 21:27:11 +10:00
parent 75538cc3d3
commit e6d2d0946a
5 changed files with 123 additions and 5 deletions

View file

@ -2,9 +2,29 @@ mod ipc;
mod manager;
mod socket;
use anyhow::{Context, Result};
use std::io::Read;
use anyhow::{Context, Result, anyhow};
use microxdg::Xdg;
use niri_tag::Config;
fn main() -> Result<()> {
// try to read a config
let xdg = Xdg::new()?;
let mut config_dir = xdg.config()?;
config_dir.push("niri-tag");
config_dir.push("config.toml");
let config = std::fs::File::open(config_dir)
.map_err(|e| anyhow!(e))
.and_then(|mut f| {
let mut buf = String::new();
f.read_to_string(&mut buf).map_err(|e| anyhow!(e))?;
Ok(buf)
})
.and_then(|buf| toml::from_str::<Config>(&buf).map_err(|e| anyhow!(e)))
.inspect_err(|e| tracing::error!("Using default config due to: {}", e))
.unwrap_or_default();
// let systemd know we're ready
let _ = libsystemd::daemon::notify(false, &[libsystemd::daemon::NotifyState::Ready])?;
// debug stuff
@ -25,7 +45,7 @@ fn main() -> Result<()> {
smol::spawn(ipc::event_provider(event_rx, fullstate_tx)).detach();
// begin managing niri tags
smol::block_on(async {
let niri_tag = manager::NiriTag::new(event_tx)
let niri_tag = manager::NiriTag::new(config, event_tx)
.await
.context("Initialising niri tag manager")
.unwrap();

View file

@ -4,7 +4,7 @@ use niri_ipc::{
Action, Event, Reply, Request, Response, Window, Workspace, WorkspaceReferenceArg,
state::{EventStreamState, EventStreamStatePart},
};
use niri_tag::{TagCmd, TagEvent, TagState};
use niri_tag::{Config, TagCmd, TagEvent, TagState};
use smol::{
channel::{self, Sender},
future,
@ -14,6 +14,7 @@ use smol::{
use std::collections::HashMap;
pub struct NiriTag {
config: Config,
tags: HashMap<u8, bool>,
windows: HashMap<u64, u8>,
active_ws: Vec<u64>,
@ -28,13 +29,14 @@ enum TagAction {
}
impl NiriTag {
pub async fn new(ev_tx: channel::Sender<TagEvent>) -> Result<Self> {
pub async fn new(config: Config, ev_tx: channel::Sender<TagEvent>) -> Result<Self> {
Ok(Self {
config,
tags: HashMap::new(),
windows: HashMap::new(),
active_ws: Vec::new(),
state: EventStreamState::default(),
socket: create_niri_socket().await?,
active_ws: Vec::new(),
ev_tx,
})
}