diff --git a/ac/tiocgwinsz.m4 b/ac/tiocgwinsz.m4 new file mode 100644 index 000000000..54ceaeb42 --- /dev/null +++ b/ac/tiocgwinsz.m4 @@ -0,0 +1,14 @@ +AC_CACHE_CHECK([for ioctl & TIOCGWINSZ], [ac_cv_tiocgwinsz],[ + ac_cv_tiocgwinsz="no" + AC_LANG_PUSH(C++) + AC_TRY_COMPILE([ + #include + ],[ + struct winsize ws; + ioctl(0, TIOCGWINSZ, &ws); + ],[ac_cv_tiocgwinsz="yes"]) + AC_LANG_POP +]) +if test x"$ac_cv_posix_fadvise" = "xyes" ; then + AC_DEFINE([HAVE_TIOCGWINSZ], 1, [define if ioctl & TIOCGWINSZ are available]) +fi diff --git a/configure.in b/configure.in index 34552077c..4616426ed 100644 --- a/configure.in +++ b/configure.in @@ -43,6 +43,7 @@ m4_include(ac/ax_boost_foreach.m4) m4_include(ac/boost.m4) m4_include(ac/etags.m4) m4_include(ac/ax_docbook.m4) +m4_include(ac/tiocgwinsz.m4) AC_OUTPUT( Makefile diff --git a/src/common/strings/formatting.cpp b/src/common/strings/formatting.cpp index e2562497e..b9990e5e4 100644 --- a/src/common/strings/formatting.cpp +++ b/src/common/strings/formatting.cpp @@ -16,6 +16,7 @@ #include "common/common.h" #include "common/strings/formatting.h" #include "common/strings/utf8.h" +#include "common/terminal.h" #include "common/translation.h" std::string @@ -152,6 +153,9 @@ format_paragraph(const std::wstring &text_to_wrap, int current_column = get_width_in_em(text); bool break_anywhere = translation_c::get_active_translation().m_line_breaks_anywhere; + if (WRAP_AT_TERMINAL_WIDTH == wrap_column) + wrap_column = get_terminal_columns() - 1; + if ((0 != indent_column) && (current_column >= indent_column)) { text += L"\n"; current_column = 0; diff --git a/src/common/strings/formatting.h b/src/common/strings/formatting.h index f3e1ffacc..29e9b0091 100644 --- a/src/common/strings/formatting.h +++ b/src/common/strings/formatting.h @@ -34,21 +34,21 @@ (int32_t)( (t) % 1000000000) #define ARG_TIMECODEN(t) ARG_TIMECODENINT((int64_t)(t)) -#define WRAP_COLUMN 79 +#define WRAP_AT_TERMINAL_WIDTH -1 std::string MTX_DLL_API format_timecode(int64_t timecode, unsigned int precision = 9); std::string MTX_DLL_API format_paragraph(const std::string &text_to_wrap, int indent_column = 0, const std::string &indent_first_line = empty_string, std::string indent_following_lines = empty_string, - int wrap_column = WRAP_COLUMN, + int wrap_column = WRAP_AT_TERMINAL_WIDTH, const char *break_chars = " ,.)/:"); std::wstring MTX_DLL_API format_paragraph(const std::wstring &text_to_wrap, int indent_column = 0, const std::wstring &indent_first_line = L" ", std::wstring indent_following_lines = L" ", - int wrap_column = WRAP_COLUMN, + int wrap_column = WRAP_AT_TERMINAL_WIDTH, const std::wstring &break_chars = L" ,.)/:"); void MTX_DLL_API fix_format(const char *fmt, std::string &new_fmt); diff --git a/src/common/terminal.cpp b/src/common/terminal.cpp new file mode 100644 index 000000000..1d1d13fe6 --- /dev/null +++ b/src/common/terminal.cpp @@ -0,0 +1,37 @@ +/* + mkvmerge -- utility for splicing together matroska files + from component media subtypes + + Distributed under the GPL + see the file COPYING for details + or visit http://www.gnu.org/copyleft/gpl.html + + terminal access functions + + Written by Moritz Bunkus . +*/ + +#include "common/os.h" + +#if defined(HAVE_TIOCGWINSZ) +# include +#endif // HAVE_TIOCGWINSZ + +#include "common/terminal.h" + +#define DEFAULT_TERMINAL_COLUMNS 80 + +int +get_terminal_columns() { +#if defined(HAVE_TIOCGWINSZ) + struct winsize ws; + + if (ioctl(0, TIOCGWINSZ, &ws) != 0) + return DEFAULT_TERMINAL_COLUMNS; + return ws.ws_col; + +#else // HAVE_TIOCGWINSZ + return DEFAULT_TERMINAL_COLUMNS; +#endif // HAVE_TIOCGWINSZ +} + diff --git a/src/common/terminal.h b/src/common/terminal.h new file mode 100644 index 000000000..beca1358c --- /dev/null +++ b/src/common/terminal.h @@ -0,0 +1,21 @@ +/* + mkvmerge -- utility for splicing together matroska files + from component media subtypes + + Distributed under the GPL + see the file COPYING for details + or visit http://www.gnu.org/copyleft/gpl.html + + terminal access functions + + Written by Moritz Bunkus . +*/ + +#ifndef __MTX_COMMON_TERMINAL_H +#define __MTX_COMMON_TERMINAL_H + +#include "common/os.h" + +int MTX_DLL_API get_terminal_columns(); + +#endif // __MTX_COMMON_TERMINAL_H