Add a -to-file <filename> switch to the echo command
As discussed in #2836
This commit is contained in:
parent
835f2239a7
commit
744778be30
|
@ -206,6 +206,10 @@ of the file onto the filesystem
|
||||||
*-debug*:::
|
*-debug*:::
|
||||||
print the given text to the *\*debug** buffer
|
print the given text to the *\*debug** buffer
|
||||||
|
|
||||||
|
*-to-file* <filename>:::
|
||||||
|
write the given text to the given file on the host
|
||||||
|
filesystem.
|
||||||
|
|
||||||
*set-face* <scope> <name> <facespec>::
|
*set-face* <scope> <name> <facespec>::
|
||||||
*alias* face +
|
*alias* face +
|
||||||
define a face in *scope*
|
define a face in *scope*
|
||||||
|
|
|
@ -1200,6 +1200,7 @@ const CommandDesc echo_cmd = {
|
||||||
"echo <params>...: display given parameters in the status line",
|
"echo <params>...: display given parameters in the status line",
|
||||||
ParameterDesc{
|
ParameterDesc{
|
||||||
{ { "markup", { false, "parse markup" } },
|
{ { "markup", { false, "parse markup" } },
|
||||||
|
{ "to-file", { true, "echo contents to given filename" } },
|
||||||
{ "debug", { false, "write to debug buffer instead of status line" } } },
|
{ "debug", { false, "write to debug buffer instead of status line" } } },
|
||||||
ParameterDesc::Flags::SwitchesOnlyAtStart
|
ParameterDesc::Flags::SwitchesOnlyAtStart
|
||||||
},
|
},
|
||||||
|
@ -1208,7 +1209,11 @@ const CommandDesc echo_cmd = {
|
||||||
CommandCompleter{},
|
CommandCompleter{},
|
||||||
[](const ParametersParser& parser, Context& context, const ShellContext&)
|
[](const ParametersParser& parser, Context& context, const ShellContext&)
|
||||||
{
|
{
|
||||||
String message = fix_atom_text(join(parser, ' ', false));
|
String message = join(parser, ' ', false);
|
||||||
|
if (auto filename = parser.get_switch("to-file"))
|
||||||
|
return write_to_file(*filename, message);
|
||||||
|
|
||||||
|
message = fix_atom_text(message);
|
||||||
if (parser.get_switch("debug"))
|
if (parser.get_switch("debug"))
|
||||||
write_to_debug_buffer(message);
|
write_to_debug_buffer(message);
|
||||||
else if (parser.get_switch("markup"))
|
else if (parser.get_switch("markup"))
|
||||||
|
|
|
@ -257,6 +257,15 @@ void write(int fd, StringView data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void write_to_file(StringView filename, StringView data)
|
||||||
|
{
|
||||||
|
const int fd = open(filename.zstr(), O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
||||||
|
if (fd == -1)
|
||||||
|
throw file_access_error(filename, strerror(errno));
|
||||||
|
auto close_fd = on_scope_end([fd]{ close(fd); });
|
||||||
|
write(fd, data);
|
||||||
|
}
|
||||||
|
|
||||||
struct BufferedWriter
|
struct BufferedWriter
|
||||||
{
|
{
|
||||||
BufferedWriter(int fd) : fd{fd} {}
|
BufferedWriter(int fd) : fd{fd} {}
|
||||||
|
|
|
@ -39,6 +39,7 @@ bool fd_writable(int fd);
|
||||||
String read_fd(int fd, bool text = false);
|
String read_fd(int fd, bool text = false);
|
||||||
String read_file(StringView filename, bool text = false);
|
String read_file(StringView filename, bool text = false);
|
||||||
void write(int fd, StringView data);
|
void write(int fd, StringView data);
|
||||||
|
void write_to_file(StringView filename, StringView data);
|
||||||
|
|
||||||
struct MappedFile
|
struct MappedFile
|
||||||
{
|
{
|
||||||
|
|
1
test/compose/echo-to-file/cmd
Normal file
1
test/compose/echo-to-file/cmd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
:echo -to-file data %{foo bar}<ret>!cat data<ret>
|
1
test/compose/echo-to-file/in
Normal file
1
test/compose/echo-to-file/in
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
1
test/compose/echo-to-file/out
Normal file
1
test/compose/echo-to-file/out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
foo bar
|
Loading…
Reference in New Issue
Block a user