twm 0.13.0

A customizable workspace manager for tmux
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use crate::layout::LayoutDefinition;
use crate::workspace::{
    HasAllFilesCondition, HasAnyFileCondition, MissingAllFilesCondition, MissingAnyFileCondition,
    NullCondition, WorkspaceConditionEnum, WorkspaceDefinition,
};
use anyhow::{Context, Result};
use schemars::{JsonSchema, schema_for};
use serde::{Deserialize, Serialize};
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
struct WorkspaceDefinitionConfig {
    /// Name for the workspace type defined by the list item.
    ///
    /// This name corresponds to the `TWM_TYPE` environment variable that will be set inside a session.
    pub name: String,

    /// List of files for which at least one must be present in a directory to be considered a workspace of this type.
    ///
    /// If unset, this constraint is simply ignored.
    ///
    /// For example if the list is `["requirements.txt", "Pipfile", "pyproject.toml", "poetry.lock", "setup.py"]`, a
    /// directory not containing *any* of those files cannot match this workspace definition.
    pub has_any_file: Option<Vec<String>>,

    /// List of files for which all must be present in a directory to be considered a workspace of this type.
    ///
    /// If unset, this constraint is simply ignored.
    ///
    /// For example, if the list is `["flake.nix", ".envrc"]`, only directories with *both* files present can match
    /// this workspace definition.
    pub has_all_files: Option<Vec<String>>,

    /// List of files for which at least one must be missing in a directory to be considered a workspace of this type.
    ///
    /// If unset, this constraint is simply ignored.
    ///
    /// For example, if the list is `["node_modules", "target"]`, directories containing *both* `node_modules` and `target`
    /// cannot match this workspace definition.
    pub missing_any_file: Option<Vec<String>>,

    /// List of files for which all must be missing in a directory to be considered a workspace of this type.
    ///
    /// If unset, this constraint is simply ignored.
    ///
    /// For example, if the list is `["node_modules", "target"]`, directories containing *either* `node_modules` or `target`
    /// cannot match this workspace definition.
    pub missing_all_files: Option<Vec<String>>,

    /// The name of the layout to apply to a session during initialization.
    ///
    /// If unset, no layout will be applied by default.
    ///
    /// This option can be overridden either by using the `-l/--layout` command line flag, which will prompt you to select
    /// a layout from the list of configured layouts, or by the presence of a `.twm.yaml` local layout configuration file
    /// in the workspace directory.
    pub default_layout: Option<String>,
}

impl From<WorkspaceDefinitionConfig> for WorkspaceDefinition {
    fn from(config: WorkspaceDefinitionConfig) -> Self {
        let mut conditions = Vec::<WorkspaceConditionEnum>::new();

        if let Some(has_any_file) = config.has_any_file
            && !has_any_file.is_empty()
        {
            let condition = HasAnyFileCondition {
                files: has_any_file,
            };
            conditions.push(condition.into());
        }

        if let Some(has_all_files) = config.has_all_files
            && !has_all_files.is_empty()
        {
            let condition = HasAllFilesCondition {
                files: has_all_files,
            };
            conditions.push(condition.into());
        }

        if let Some(missing_any_file) = config.missing_any_file
            && !missing_any_file.is_empty()
        {
            let condition = MissingAnyFileCondition {
                files: missing_any_file,
            };
            conditions.push(condition.into());
        }

        if let Some(missing_all_files) = config.missing_all_files
            && !missing_all_files.is_empty()
        {
            let condition = MissingAllFilesCondition {
                files: missing_all_files,
            };
            conditions.push(condition.into());
        }

        if conditions.is_empty() {
            let condition = NullCondition {};
            conditions.push(condition.into());
        }

        WorkspaceDefinition {
            name: config.name,
            conditions,
            default_layout: config.default_layout,
        }
    }
}

fn default_search_paths() -> Vec<String> {
    vec!["~".into()]
}

fn default_workspace_definitions() -> Vec<WorkspaceDefinitionConfig> {
    vec![WorkspaceDefinitionConfig {
        name: "default".into(),
        has_any_file: Some(vec![".git".into(), ".twm.yaml".into()]),
        default_layout: Some("default".into()),
        has_all_files: None,
        missing_any_file: None,
        missing_all_files: None,
    }]
}

const fn default_max_search_depth() -> usize {
    3
}

const fn default_session_name_path_components() -> usize {
    2
}

fn default_exclude_path_components() -> Vec<String> {
    vec![
        ".cache".into(),
        ".cargo".into(),
        ".git".into(),
        "__pycache__".into(),
        "node_modules".into(),
        "target".into(),
        "venv".into(),
    ]
}

fn default_layout_definitions() -> Vec<LayoutDefinition> {
    vec![LayoutDefinition {
        name: "default".into(),
        inherits: None,
        commands: Some(vec![String::from("echo \"Created $TWM_TYPE session\"")]),
    }]
}

fn default_follow_links() -> bool {
    true
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RawTwmGlobal {
    /// List of directories to have twm search for workspaces.
    ///
    /// If unset, defaults to `~` (shell expansion is supported).
    ///
    /// Be careful to not make your search paths overlap, e.g. if you include `~/projects` and `~/projects/foo/bar`
    /// with `max_search_depth: 3`, `~/projects/foo/bar` will be searched twice and results will be displayed twice
    /// in the picker. Generally it's easiest to just include the parent directory and increase `max_search_depth`
    /// if needed.
    #[serde(default = "default_search_paths")]
    search_paths: Vec<String>,

    /// List of configurations for workspaces.
    ///
    /// If unset, the default twm workspace definition is any directory containing a `.git` file/directory or a
    /// `.twm.yaml` layout file.
    ///
    /// When a directory is found that matches a workspace definition the first match, in order of appearance in
    /// this list, is the workspace "type" that will be for things like choosing which layout to apply to the session
    /// and in setting the `TWM_TYPE` environment variable
    #[serde(default = "default_workspace_definitions")]
    workspace_definitions: Vec<WorkspaceDefinitionConfig>,

    /// Maximum depth to search for workspaces inside the `search_paths` directories.
    /// If unset, defaults to 3.
    #[serde(default = "default_max_search_depth")]
    max_search_depth: usize,

    /// Default number of components of the workspace directory to use for the created session name.
    /// If unset, defaults to 1.
    ///
    /// E.g. if you open a workspace at `/home/vinny/projects/foo/bar` and `session_name_path_components` is set to 1,
    /// The session name will be `bar`. If 2, `foo/bar`, etc.
    #[serde(default = "default_session_name_path_components")]
    session_name_path_components: usize,

    /// List of path components which will *exclude* a directory from being considered a workspace.
    /// If unset, defaults to an empty list.
    ///
    /// A common use case would be to exclude things like `node_modules`, `target`, `__pycache__`, etc.
    #[serde(default = "default_exclude_path_components")]
    exclude_path_components: Vec<String>,

    /// List of layout definitions made available when opening a workspace.
    /// If unset, defaults to an empty list.
    ///
    /// The layouts in this list can be used as the `default_layout` in a workspace definition and also
    /// will be available in the layout list when using `-l/--layout` command line flag.
    #[serde(default = "default_layout_definitions")]
    layouts: Vec<LayoutDefinition>,

    /// Whether to follow symbolic links when searching for workspaces.
    /// If unset, defaults to true.
    #[serde(default = "default_follow_links")]
    follow_links: bool,
}

impl Default for RawTwmGlobal {
    fn default() -> Self {
        // test case ensures this works
        RawTwmGlobal::from_str("").unwrap()
    }
}

impl RawTwmGlobal {
    pub fn schema() -> Result<String> {
        Ok(serde_json::to_string_pretty(&schema_for!(Self))?)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TwmGlobal {
    pub search_paths: Vec<String>,
    pub exclude_path_components: Vec<String>,
    pub workspace_definitions: Vec<WorkspaceDefinition>,
    pub session_name_path_components: usize,
    pub layouts: Vec<LayoutDefinition>,
    pub max_search_depth: usize,
    pub follow_links: bool,
}

#[derive(Debug, Deserialize, Clone, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TwmLayout {
    /// Layout definition to default to when opening the current workspace.
    /// This will override the `default_layout` in the matching workspace definition if present.
    pub layout: LayoutDefinition,
}

impl TwmLayout {
    pub fn schema() -> Result<String> {
        Ok(serde_json::to_string_pretty(&schema_for!(Self))?)
    }
}

impl From<RawTwmGlobal> for TwmGlobal {
    fn from(raw_config: RawTwmGlobal) -> Self {
        // search paths are the only place we need to worry about shell expansion
        let search_paths: Vec<String> = raw_config
            .search_paths
            .iter()
            .map(|path| shellexpand::tilde(path).to_string())
            .collect();

        let exclude_path_components = raw_config.exclude_path_components;

        let workspace_definitions = raw_config
            .workspace_definitions
            .into_iter()
            .map(WorkspaceDefinition::from)
            .collect();

        Self {
            search_paths,
            exclude_path_components,
            workspace_definitions,
            layouts: raw_config.layouts,
            max_search_depth: raw_config.max_search_depth,
            session_name_path_components: raw_config.session_name_path_components,
            follow_links: raw_config.follow_links,
        }
    }
}

impl TryFrom<&PathBuf> for RawTwmGlobal {
    type Error = anyhow::Error;

    fn try_from(path: &PathBuf) -> Result<Self> {
        let config = fs::read_to_string(path)
            .with_context(|| format!("Failed to read config from path: {path:#?}"))?;
        let raw_config =
            RawTwmGlobal::from_str(&config).with_context(|| "Failed to parse twm config file.")?;
        Ok(raw_config)
    }
}

impl FromStr for RawTwmGlobal {
    type Err = anyhow::Error;

    fn from_str(config: &str) -> Result<Self> {
        let settings = config::Config::builder()
            .add_source(config::File::from_str(config, config::FileFormat::Yaml))
            .build()
            .with_context(|| "Failed build configuration. You should never see this. I think.")?;

        let raw_config = settings
            .try_deserialize()
            .with_context(|| "Failed to deserialize twm config.")?;
        Ok(raw_config)
    }
}

impl TwmGlobal {
    fn get_config_path() -> Result<Option<PathBuf>> {
        let config_file_name = format!("{}.yaml", clap::crate_name!());
        match std::env::var_os("TWM_CONFIG_FILE") {
            // if TWM_CONFIG_FILE is not set, search xdg dirs for config file as normal
            c if c.as_ref().unwrap_or(&OsString::default()).is_empty() => {
                let xdg_dirs = xdg::BaseDirectories::with_prefix(clap::crate_name!());
                let xdg_config_path = xdg_dirs.get_config_file(config_file_name);
                match xdg_config_path {
                    Some(path) => {
                        if path.exists() {
                            Ok(Some(path))
                        } else {
                            Ok(None)
                        }
                    }
                    None => Ok(None),
                }
            }
            // if we explicitly set the TWM_CONFIG_FILE, we should take it at face value and return the path here
            // which will cause an error later if it doesn't turn out to exist. This choice is made because it could
            // be a really annoying silent error if someone set the env var override somewhere, forgot, and changed it
            // vs its unlikely that many people would not understand where they need to put their config file and end
            // up confused why their settings aren't being picked up. ignoring a missing conf file lets the program run
            // without someone explicitly setting up any config
            Some(config_file_path) => Ok(Some(PathBuf::from(config_file_path))),
            _ => unreachable!(),
        }
    }

    pub fn load() -> Result<Self> {
        let raw_config = match TwmGlobal::get_config_path()? {
            Some(path) => RawTwmGlobal::try_from(&path)?,
            None => RawTwmGlobal::default(),
        };
        let config = TwmGlobal::from(raw_config);
        Ok(config)
    }
}

impl FromStr for TwmLayout {
    type Err = anyhow::Error;

    fn from_str(config: &str) -> Result<Self> {
        let settings = config::Config::builder()
            .add_source(config::File::from_str(config, config::FileFormat::Yaml))
            .build()
            .with_context(
                || "Failed to build configuration. You should never see this. I think.",
            )?;

        let local_config = settings
            .try_deserialize()
            .with_context(|| "Failed to deserialize local twm config.")?;
        Ok(local_config)
    }
}

impl TwmLayout {
    /// Attemps to load a local config file from the given path.
    /// Will return Ok(None) if no config file is found.
    /// Errors if the config file is found but results in an error during parsing.
    pub fn load(path: &Path) -> Result<Option<Self>> {
        const CONFIG_FILE_NAME: &str = ".twm.yaml";
        let config_path = path.join(CONFIG_FILE_NAME);
        if config_path.exists() {
            let config = fs::read_to_string(&config_path)
                .with_context(|| format!("Failed to read config from path: {config_path:#?}"))?;
            Ok(Some(TwmLayout::from_str(&config)?))
        } else {
            Ok(None)
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::handler::DEFAULT_LAYOUT_CONFIG_TEMPLATE;

    use super::*;
    use serial_test::serial;

    #[test]
    fn test_empty_config_is_valid() {
        let raw_config = RawTwmGlobal::from_str("").unwrap();
        let _ = TwmGlobal::from(raw_config);
    }

    #[test]
    fn test_invalid_config_key_is_error() {
        let raw_config = RawTwmGlobal::from_str("foo: bar");
        assert!(raw_config.is_err());
    }

    /// Make noise if we change which env var overrides the config file path or it breaks
    #[test]
    #[serial]
    fn test_get_config_path_env_var_override() {
        let orig_twm = std::env::var_os("TWM_CONFIG_FILE");
        let config_file = "/tmp/twm.yaml";
        unsafe {
            std::env::set_var("TWM_CONFIG_FILE", config_file);
        }

        let config_path = TwmGlobal::get_config_path().unwrap();
        assert_eq!(config_path, Some(PathBuf::from(config_file)));

        if let Some(twm) = orig_twm {
            unsafe {
                std::env::set_var("TWM_CONFIG_FILE", twm);
            }
        } else {
            unsafe {
                std::env::remove_var("TWM_CONFIG_FILE");
            }
        }
    }

    #[test]
    #[serial]
    fn test_get_config_path_xdg_default_file_doesnt_exist() {
        let orig_twm = std::env::var_os("TWM_CONFIG_FILE");
        let orig_home = std::env::var_os("HOME");
        let orig_xdg = std::env::var_os("XDG_CONFIG_HOME");
        unsafe {
            std::env::remove_var("TWM_CONFIG_FILE");
        }
        unsafe {
            std::env::set_var("HOME", "/tmp");
        }
        unsafe {
            std::env::set_var("XDG_CONFIG_HOME", "/tmp/.config");
        }
        let config_path = TwmGlobal::get_config_path().unwrap();
        assert_eq!(
            config_path,
            None,
            //Some(PathBuf::from("/tmp/.config/twm/twm.yaml"))
        );

        if let Some(twm) = orig_twm {
            unsafe {
                std::env::set_var("TWM_CONFIG_FILE", twm);
            }
        }
        if let Some(home) = orig_home {
            unsafe {
                std::env::set_var("HOME", home);
            }
        } else {
            unsafe {
                std::env::remove_var("HOME");
            }
        }
        if let Some(xdg) = orig_xdg {
            unsafe {
                std::env::set_var("XDG_CONFIG_HOME", xdg);
            }
        } else {
            unsafe {
                std::env::remove_var("XDG_CONFIG_HOME");
            }
        }
    }

    /// this could end up being a flaky test we'll see
    #[test]
    #[serial]
    fn test_get_config_path_xdg_default_file_exists() {
        let orig_twm = std::env::var_os("TWM_CONFIG_FILE");
        let orig_home = std::env::var_os("HOME");
        let orig_xdg = std::env::var_os("XDG_CONFIG_HOME");
        unsafe {
            std::env::remove_var("TWM_CONFIG_FILE");
        }
        unsafe {
            std::env::set_var("HOME", "/tmp");
        }
        unsafe {
            std::env::set_var("XDG_CONFIG_HOME", "/tmp/.config");
        }
        std::fs::create_dir_all("/tmp/.config/twm").unwrap();
        std::fs::write("/tmp/.config/twm/twm.yaml", "").unwrap();
        let config_path = TwmGlobal::get_config_path().unwrap();
        assert_eq!(
            config_path,
            Some(PathBuf::from("/tmp/.config/twm/twm.yaml"))
        );

        if let Some(twm) = orig_twm {
            unsafe {
                std::env::set_var("TWM_CONFIG_FILE", twm);
            }
        }
        if let Some(home) = orig_home {
            unsafe {
                std::env::set_var("HOME", home);
            }
        } else {
            unsafe {
                std::env::remove_var("HOME");
            }
        }
        if let Some(xdg) = orig_xdg {
            unsafe {
                std::env::set_var("XDG_CONFIG_HOME", xdg);
            }
        } else {
            unsafe {
                std::env::remove_var("XDG_CONFIG_HOME");
            }
        }
        let _ = std::fs::remove_file("/tmp/.config/twm/twm.yaml");
    }

    #[test]
    #[serial]
    fn test_get_config_path_empty_string_equals_unset() {
        let orig_twm = std::env::var_os("TWM_CONFIG_FILE");
        let orig_home = std::env::var_os("HOME");
        let orig_xdg = std::env::var_os("XDG_CONFIG_HOME");
        unsafe {
            std::env::remove_var("TWM_CONFIG_FILE");
        }
        unsafe {
            std::env::set_var("HOME", "/tmp");
        }
        unsafe {
            std::env::set_var("XDG_CONFIG_HOME", "/tmp/.config");
        }

        let unset_twm_file_config_path = TwmGlobal::get_config_path().unwrap();

        unsafe {
            std::env::set_var("TWM_CONFIG_FILE", "");
        }
        let empty_twm_file_config_path = TwmGlobal::get_config_path().unwrap();

        assert_eq!(unset_twm_file_config_path, empty_twm_file_config_path);

        if let Some(twm) = orig_twm {
            unsafe {
                std::env::set_var("TWM_CONFIG_FILE", twm);
            }
        } else {
            unsafe {
                std::env::remove_var("TWM_CONFIG_FILE");
            }
        }
        if let Some(home) = orig_home {
            unsafe {
                std::env::set_var("HOME", home);
            }
        } else {
            unsafe {
                std::env::remove_var("HOME");
            }
        }
        if let Some(xdg) = orig_xdg {
            unsafe {
                std::env::set_var("XDG_CONFIG_HOME", xdg);
            }
        } else {
            unsafe {
                std::env::remove_var("XDG_CONFIG_HOME");
            }
        }
    }

    #[test]
    fn test_default_layout_config_template_is_valid() {
        TwmLayout::from_str(DEFAULT_LAYOUT_CONFIG_TEMPLATE).unwrap();
    }
}