Skip to content

Commit a85d8d1

Browse files
authored
Add foot as a newly supported backend (#77)
See https://codeberg.org/dnkl/foot
1 parent a1bee3d commit a85d8d1

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

Cargo.lock

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ shellexpand = "2.1.*"
1919
log = "0.4.*"
2020
env_logger = "0.10.*"
2121
sysinfo = "0.33.*"
22+
rust-ini = "0.21"
2223

2324
[package.metadata.deb]
2425
maintainer = "beeender <chenmulong@gmail.com>"

src/backend/foot.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use super::Functions;
2+
use crate::config::Config;
3+
use crate::error::GlrnvimError;
4+
use ini::Ini;
5+
use std::io::Write;
6+
use std::path::PathBuf;
7+
use tempfile::NamedTempFile;
8+
9+
pub const FOOT_NAME: &str = "foot";
10+
11+
struct Foot {
12+
exe_path: PathBuf,
13+
temp_file: Option<NamedTempFile>,
14+
}
15+
16+
pub fn init(config: &Config) -> Result<Box<dyn Functions>, GlrnvimError> {
17+
let exe_path = super::exe_path(&config.term_exe_path, FOOT_NAME)?;
18+
19+
Ok(Box::new(Foot {
20+
exe_path,
21+
temp_file: None,
22+
}))
23+
}
24+
25+
impl Foot {
26+
fn create_conf_file(&mut self, config: &Config) {
27+
let mut foot_conf = if config.load_term_conf {
28+
let conf_files = Foot::find_default_confs();
29+
if conf_files.is_empty() {
30+
Ini::new()
31+
} else {
32+
let path = conf_files.first().unwrap();
33+
Ini::load_from_file(path).expect("Failed to load default config file")
34+
}
35+
} else {
36+
Ini::new()
37+
};
38+
39+
let mut font_str = String::new();
40+
for f in &config.fonts {
41+
if !font_str.is_empty() {
42+
font_str += ",";
43+
}
44+
font_str += f;
45+
if config.font_size != 0 {
46+
font_str += &format!(":size={}", config.font_size).to_string();
47+
}
48+
}
49+
if !font_str.is_empty() {
50+
foot_conf.with_section(Some("main")).set("font", font_str);
51+
}
52+
// Note: The ctrl-z seems to be no-op so we don't have to disable it. Other default
53+
// key bindings are quite harmless for now. If needed, just disable them in the config file
54+
// here.
55+
56+
let mut file = tempfile::NamedTempFile::new().expect("Failed to create temporary file");
57+
foot_conf
58+
.write_to_file(&file)
59+
.expect("Failed to write to temporary file");
60+
file.flush().unwrap();
61+
62+
file.path();
63+
self.temp_file = Some(file);
64+
}
65+
66+
fn find_default_confs() -> Vec<String> {
67+
let base_confs: [String; 0] = [];
68+
let pri_confs: [String; 3] = [
69+
"$XDG_CONFIG_HOME/foot/foot.conf".to_string(),
70+
"$HOME/.config/foot/foot.conf".to_string(),
71+
"$XDG_CONFIG_DIRS/foot/foot.conf".to_string(),
72+
];
73+
super::find_term_conf_files(&base_confs, &pri_confs)
74+
}
75+
}
76+
77+
impl Functions for Foot {
78+
fn create_command(&mut self, config: &Config) -> std::process::Command {
79+
let mut command = std::process::Command::new(&self.exe_path);
80+
81+
command.arg("--config");
82+
if let Some(config_path) = config.term_config_path.as_ref() {
83+
command.arg(config_path.as_str());
84+
} else {
85+
self.create_conf_file(config);
86+
// Overwrite the config with the generated settings from glrnvim.yml
87+
command.arg(self.temp_file.as_ref().unwrap().path());
88+
}
89+
90+
command.arg("--app-id");
91+
command.arg("glrnvim");
92+
93+
command.arg(&config.nvim_exe_path);
94+
command.args(super::COMMON_ARGS);
95+
96+
command
97+
}
98+
}

src/backend/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod alacritty;
22
mod kitty;
33
mod urxvt;
44
mod wezterm;
5+
mod foot;
56
use super::config::Config;
67
use crate::config::Backend;
78
use crate::error::GlrnvimError;
@@ -42,6 +43,7 @@ pub fn init(config: &Config) -> Result<Box<dyn Functions>, GlrnvimError> {
4243
Backend::Urxvt => urxvt::init(config),
4344
Backend::Kitty => kitty::init(config),
4445
Backend::Wezterm => wezterm::init(config),
46+
Backend::Foot => foot::init(config),
4547
},
4648
None => {
4749
for init_func in &[alacritty::init, urxvt::init, kitty::init, wezterm::init] {

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum Backend {
1313
Urxvt,
1414
Kitty,
1515
Wezterm,
16+
Foot,
1617
}
1718

1819
#[derive(Debug, PartialEq, Eq, Deserialize)]

0 commit comments

Comments
 (0)