FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/cecli/editor.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
11
(11)
Pull requests
8
(8)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
cecli
/
editor.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
148 lines (120 loc) · 4.3 KB
Breadcrumbs
cecli
/
cecli
/
editor.py
Copy path
File metadata and controls
148 lines (120 loc) · 4.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
Editor module for handling system text editor interactions.
This module provides functionality to:
- Discover and launch the system's configured text editor
- Create and manage temporary files for editing
- Handle editor preferences from environment variables
- Support cross-platform editor operations
"""
import
os
import
platform
import
subprocess
import
tempfile
from
rich
.
console
import
Console
from
cecli
.
decoding
import
safe_open
from
cecli
.
dump
import
dump
# noqa
DEFAULT_EDITOR_NIX
=
"vi"
DEFAULT_EDITOR_OS_X
=
"vim"
DEFAULT_EDITOR_WINDOWS
=
"notepad"
console
=
Console
()
def
print_status_message
(
success
,
message
,
style
=
None
):
"""
Print a status message with appropriate styling.
:param success: Whether the operation was successful
:param message: The message to display
:param style: Optional style override. If None, uses green for success and red for failure
"""
if
style
is
None
:
style
=
"bold green"
if
success
else
"bold red"
console
.
print
(
message
,
style
=
style
)
print
(
""
)
def
write_temp_file
(
input_data
=
""
,
suffix
=
None
,
prefix
=
None
,
dir
=
None
,
):
"""
Create a temporary file with the given input data.
:param input_data: Content to write to the temporary file
:param suffix: Optional file extension (without the dot)
:param prefix: Optional prefix for the temporary filename
:param dir: Optional directory to create the file in
:return: Path to the created temporary file
:raises: OSError if file creation or writing fails
"""
kwargs
=
{
"prefix"
:
prefix
,
"dir"
:
dir
}
if
suffix
:
kwargs
[
"suffix"
]
=
f".
{
suffix
}
"
fd
,
filepath
=
tempfile
.
mkstemp
(
**
kwargs
)
try
:
with
os
.
fdopen
(
fd
,
"w"
)
as
f
:
f
.
write
(
input_data
)
except
Exception
:
os
.
close
(
fd
)
raise
return
filepath
def
get_environment_editor
(
default
=
None
):
"""
Fetches the preferred editor from the environment variables.
This function checks the following environment variables in order to
determine the user's preferred editor:
- VISUAL
- EDITOR
:param default: The default editor to return if no environment variable is set.
:type default: str or None
:return: The preferred editor as specified by environment variables or the default value.
:rtype: str or None
"""
editor
=
os
.
environ
.
get
(
"VISUAL"
,
os
.
environ
.
get
(
"EDITOR"
,
default
))
return
editor
def
discover_editor
(
editor_override
=
None
):
"""
Discovers and returns the appropriate editor command.
Handles cases where the editor command includes arguments, including quoted arguments
with spaces (e.g. 'vim -c "set noswapfile"').
:return: The editor command as a string
:rtype: str
"""
system
=
platform
.
system
()
if
system
==
"Windows"
:
default_editor
=
DEFAULT_EDITOR_WINDOWS
elif
system
==
"Darwin"
:
default_editor
=
DEFAULT_EDITOR_OS_X
else
:
default_editor
=
DEFAULT_EDITOR_NIX
if
editor_override
:
editor
=
editor_override
else
:
editor
=
get_environment_editor
(
default_editor
)
return
editor
def
pipe_editor
(
input_data
=
""
,
suffix
=
None
,
editor
=
None
):
"""
Opens the system editor with optional input data and returns the edited content.
This function creates a temporary file with the provided input data, opens it in
the system editor, waits for the user to make changes and close the editor, then
reads and returns the modified content. The temporary file is deleted afterwards.
:param input_data: Initial content to populate the editor with
:type input_data: str
:param suffix: Optional file extension for the temporary file (e.g. '.txt', '.md')
:type suffix: str or None
:return: The edited content after the editor is closed
:rtype: str
"""
filepath
=
write_temp_file
(
input_data
,
suffix
)
command_str
=
discover_editor
(
editor
)
command_str
+=
" "
+
filepath
subprocess
.
call
(
command_str
,
shell
=
True
)
with
safe_open
(
filepath
,
"r"
)
as
f
:
output_data
=
f
.
read
()
try
:
os
.
remove
(
filepath
)
except
PermissionError
:
print_status_message
(
False
,
(
f"WARNING: Unable to delete temporary file
{
filepath
!r
}
. You may need to delete it"
" manually."
),
)
return
output_data
Back
|
FazBrowse Home
|
New Git URL