diff options
| author | Maxime Coste <mawww@kakoune.org> | 2019-04-07 09:31:36 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2019-04-07 09:32:17 +1000 |
| commit | 744778be30f2f78bea4af5523f00da98480cda4d (patch) | |
| tree | e3f6032a644c471a8959da4c0d964b65785110dc /src | |
| parent | 835f2239a7428d3457e277c95ad39e2cdd5211d5 (diff) | |
Add a -to-file <filename> switch to the echo command
As discussed in #2836
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands.cc | 7 | ||||
| -rw-r--r-- | src/file.cc | 9 | ||||
| -rw-r--r-- | src/file.hh | 1 |
3 files changed, 16 insertions, 1 deletions
diff --git a/src/commands.cc b/src/commands.cc index 4176f673..f1e2b71a 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1200,6 +1200,7 @@ const CommandDesc echo_cmd = { "echo <params>...: display given parameters in the status line", ParameterDesc{ { { "markup", { false, "parse markup" } }, + { "to-file", { true, "echo contents to given filename" } }, { "debug", { false, "write to debug buffer instead of status line" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart }, @@ -1208,7 +1209,11 @@ const CommandDesc echo_cmd = { CommandCompleter{}, [](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")) write_to_debug_buffer(message); else if (parser.get_switch("markup")) diff --git a/src/file.cc b/src/file.cc index 7b49595e..52a0f288 100644 --- a/src/file.cc +++ b/src/file.cc @@ -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 { BufferedWriter(int fd) : fd{fd} {} diff --git a/src/file.hh b/src/file.hh index 6871a1a3..5264616b 100644 --- a/src/file.hh +++ b/src/file.hh @@ -39,6 +39,7 @@ bool fd_writable(int fd); String read_fd(int fd, bool text = false); String read_file(StringView filename, bool text = false); void write(int fd, StringView data); +void write_to_file(StringView filename, StringView data); struct MappedFile { |
