| 1 | #ifndef _melder_info_h_ |
| 2 | #define _melder_info_h_ |
| 3 | /* melder_info.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2018,2020,2023,2025 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | Give information to stdout (batch), or to an "Info" window (interactive), or to a diverted string, or to a notebook. |
| 23 | */ |
| 24 | |
| 25 | namespace MelderInfo { |
| 26 | using Proc = void (*) (conststring32 message); |
| 27 | void _defaultProc (conststring32 message); |
| 28 | extern Proc _p_currentProc; |
| 29 | extern MelderString _foregroundBuffer, *_p_currentBuffer; |
| 30 | } |
| 31 | |
| 32 | void MelderInfo_open (); // clear the Info window in the background |
| 33 | void MelderInfo_close (); // drain the background info to the Info window, making sure there is a line break |
| 34 | void MelderInfo_drain (); // drain the background info to the Info window, without adding any extra line break |
| 35 | |
| 36 | template <typename... Args> |
| 37 | void MelderInfo_write (const Args&... args) { |
| 38 | MelderString_append (MelderInfo::_p_currentBuffer, args...); |
| 39 | if (MelderInfo::_p_currentProc == & MelderInfo::_defaultProc && MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) |
| 40 | ( MelderConsole::write (MelderArg {args}._arg, false), ... ); |
| 41 | } |
| 42 | |
| 43 | template <typename... Args> |
| 44 | void MelderInfo_writeLine (const Args&... args) { |
| 45 | MelderString_append (MelderInfo::_p_currentBuffer, args...); |
| 46 | MelderString_appendCharacter (MelderInfo::_p_currentBuffer, U'\n'); |
| 47 | if (MelderInfo::_p_currentProc == & MelderInfo::_defaultProc && MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) { |
| 48 | ( MelderConsole::write (MelderArg {args}._arg, false), ... ); |
| 49 | MelderConsole::write (U"\n", false); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | template <typename... Args> |
| 54 | void Melder_information (const Args&... args) { |
| 55 | MelderString_copy (MelderInfo::_p_currentBuffer, args...); |
| 56 | if (MelderInfo::_p_currentProc == & MelderInfo::_defaultProc && MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) |
| 57 | ( MelderConsole::write (MelderArg {args}._arg, false), ... ); |
| 58 | MelderInfo_close (); |
| 59 | } |
| 60 | |
| 61 | void Melder_informationReal (double value, conststring32 units); // %.17g or --undefined--; units may be null |
| 62 | |
| 63 | void Melder_divertInfo (MelderString *p_buffer); // nullptr = back to normal |
| 64 | |
| 65 | class autoMelderDivertInfo { |
| 66 | public: |
| 67 | autoMelderDivertInfo (MelderString *p_buffer) { Melder_divertInfo (p_buffer); } |
| 68 | ~autoMelderDivertInfo () { Melder_divertInfo (nullptr); } |
| 69 | }; |
| 70 | |
| 71 | void Melder_clearInfo (); // clear the Info window |
| 72 | conststring32 Melder_getInfo (); |
| 73 | |
| 74 | void Melder_setInformationProc (MelderInfo::Proc proc); |
| 75 | |
| 76 | class autoMelderSetInformationProc { |
| 77 | public: |
| 78 | autoMelderSetInformationProc (MelderInfo::Proc temporaryNewProc) { |
| 79 | our saveProc = MelderInfo::_p_currentProc; |
| 80 | Melder_setInformationProc (temporaryNewProc); |
| 81 | } |
| 82 | ~autoMelderSetInformationProc () { |
| 83 | Melder_setInformationProc (our saveProc); |
| 84 | } |
| 85 | private: |
| 86 | MelderInfo::Proc saveProc; |
| 87 | }; |
| 88 | |
| 89 | /* End of file melder_info.h */ |
| 90 | #endif |