|
| 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 | +} |
0 commit comments