11import logging
2- import os
32import re
43import typing as t
54
6- from colorama import Fore
7-
8- from tmuxp .types import StrPath
9- from tmuxp .workspace .constants import VALID_WORKSPACE_DIR_FILE_EXTENSIONS
10-
115from .. import log
126
137logger = logging .getLogger (__name__ )
@@ -32,162 +26,6 @@ def tmuxp_echo(
3226 print (message )
3327
3428
35- def get_workspace_dir () -> str :
36- """
37- Return tmuxp configuration directory.
38-
39- ``TMUXP_CONFIGDIR`` environmental variable has precedence if set. We also
40- evaluate XDG default directory from XDG_CONFIG_HOME environmental variable
41- if set or its default. Then the old default ~/.tmuxp is returned for
42- compatibility.
43-
44- Returns
45- -------
46- str :
47- absolute path to tmuxp config directory
48- """
49-
50- paths = []
51- if "TMUXP_CONFIGDIR" in os .environ :
52- paths .append (os .environ ["TMUXP_CONFIGDIR" ])
53- if "XDG_CONFIG_HOME" in os .environ :
54- paths .append (os .path .join (os .environ ["XDG_CONFIG_HOME" ], "tmuxp" ))
55- else :
56- paths .append ("~/.config/tmuxp/" )
57- paths .append ("~/.tmuxp" )
58-
59- for path in paths :
60- path = os .path .expanduser (path )
61- if os .path .isdir (path ):
62- return path
63- # Return last path as default if none of the previous ones matched
64- return path
65-
66-
67- def find_workspace_file (
68- workspace_file : StrPath ,
69- workspace_dir : t .Optional [StrPath ] = None ,
70- ) -> str :
71- """
72- Return the real config path or raise an exception.
73-
74- If config is directory, scan for .tmuxp.{yaml,yml,json} in directory. If
75- one or more found, it will warn and pick the first.
76-
77- If config is ".", "./" or None, it will scan current directory.
78-
79- If config is has no path and only a filename, e.g. "myconfig.yaml" it will
80- search config dir.
81-
82- If config has no path and only a name with no extension, e.g. "myconfig",
83- it will scan for file name with yaml, yml and json. If multiple exist, it
84- will warn and pick the first.
85-
86- Parameters
87- ----------
88- workspace_file : str
89- workspace file, valid examples:
90-
91- - a file name, myconfig.yaml
92- - relative path, ../config.yaml or ../project
93- - a period, .
94- """
95- if not workspace_dir :
96- workspace_dir = get_workspace_dir ()
97- path = os .path
98- exists , join , isabs = path .exists , path .join , path .isabs
99- dirname , normpath , splitext = path .dirname , path .normpath , path .splitext
100- cwd = os .getcwd ()
101- is_name = False
102- file_error = None
103-
104- workspace_file = os .path .expanduser (workspace_file )
105- # if purename, resolve to confg dir
106- if is_pure_name (workspace_file ):
107- is_name = True
108- elif (
109- not isabs (workspace_file )
110- or len (dirname (workspace_file )) > 1
111- or workspace_file == "."
112- or workspace_file == ""
113- or workspace_file == "./"
114- ): # if relative, fill in full path
115- workspace_file = normpath (join (cwd , workspace_file ))
116-
117- # no extension, scan
118- if path .isdir (workspace_file ) or not splitext (workspace_file )[1 ]:
119- if is_name :
120- candidates = [
121- x
122- for x in [
123- f"{ join (workspace_dir , workspace_file )} { ext } "
124- for ext in VALID_WORKSPACE_DIR_FILE_EXTENSIONS
125- ]
126- if exists (x )
127- ]
128- if not len (candidates ):
129- file_error = (
130- "workspace-file not found in workspace dir (yaml/yml/json) %s "
131- "for name" % (workspace_dir )
132- )
133- else :
134- candidates = [
135- x
136- for x in [
137- join (workspace_file , ext )
138- for ext in [".tmuxp.yaml" , ".tmuxp.yml" , ".tmuxp.json" ]
139- ]
140- if exists (x )
141- ]
142-
143- if len (candidates ) > 1 :
144- tmuxp_echo (
145- Fore .RED
146- + "Multiple .tmuxp.{yml,yaml,json} workspace_files in %s"
147- % dirname (workspace_file )
148- + Fore .RESET
149- )
150- tmuxp_echo (
151- "This is undefined behavior, use only one. "
152- "Use file names e.g. myproject.json, coolproject.yaml. "
153- "You can load them by filename."
154- )
155- elif not len (candidates ):
156- file_error = "No tmuxp files found in directory"
157- if len (candidates ):
158- workspace_file = candidates [0 ]
159- elif not exists (workspace_file ):
160- file_error = "file not found"
161-
162- if file_error :
163- raise FileNotFoundError (file_error , workspace_file )
164-
165- return workspace_file
166-
167-
168- def is_pure_name (path : str ) -> bool :
169- """
170- Return True if path is a name and not a file path.
171-
172- Parameters
173- ----------
174- path : str
175- Path (can be absolute, relative, etc.)
176-
177- Returns
178- -------
179- bool
180- True if path is a name of config in config dir, not file path.
181- """
182- return (
183- not os .path .isabs (path )
184- and len (os .path .dirname (path )) == 0
185- and not os .path .splitext (path )[1 ]
186- and path != "."
187- and path != ""
188- )
189-
190-
19129def prompt (
19230 name : str ,
19331 default : t .Any = None ,
0 commit comments