From db909605b8ae365b8c73d45a3b06af3f7e20c1eb Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 7 Jun 2020 18:47:27 -0700 Subject: [PATCH] Migrate reformat_for_screen to new termsize container --- src/builtin_functions.cpp | 3 ++- src/common.cpp | 5 +++-- src/common.h | 6 ++++-- src/termsize.h | 3 +++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/builtin_functions.cpp b/src/builtin_functions.cpp index 790f7582c..f0fb7bca2 100644 --- a/src/builtin_functions.cpp +++ b/src/builtin_functions.cpp @@ -27,6 +27,7 @@ #include "parser_keywords.h" #include "proc.h" #include "signal.h" +#include "termsize.h" #include "wcstringutil.h" #include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep @@ -371,7 +372,7 @@ int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) { buff.append(name); buff.append(L", "); } - streams.out.append(reformat_for_screen(buff)); + streams.out.append(reformat_for_screen(buff, termsize_last())); } else { for (const auto &name : names) { streams.out.append(name.c_str()); diff --git a/src/common.cpp b/src/common.cpp index 61c5482e0..afc11cb58 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -62,6 +62,7 @@ #include "parser.h" #include "proc.h" #include "signal.h" +#include "termsize.h" #include "wcstringutil.h" #include "wildcard.h" #include "wutil.h" // IWYU pragma: keep @@ -778,10 +779,10 @@ void narrow_string_safe(char buff[64], const wchar_t *s) { buff[idx] = '\0'; } -wcstring reformat_for_screen(const wcstring &msg) { +wcstring reformat_for_screen(const wcstring &msg, const termsize_t &termsize) { wcstring buff; int line_width = 0; - int screen_width = common_get_width(); + int screen_width = termsize.width; if (screen_width) { const wchar_t *start = msg.c_str(); diff --git a/src/common.h b/src/common.h index c7fa396b6..985e7b8ea 100644 --- a/src/common.h +++ b/src/common.h @@ -38,6 +38,8 @@ typedef std::wstring wcstring; typedef std::vector wcstring_list_t; +struct termsize_t; + // Maximum number of bytes used by a single utf-8 character. #define MAX_UTF8_BYTES 6 @@ -662,8 +664,8 @@ int common_get_height(); /// variable used by common_get_wisth and common_get_height(). void common_handle_winch(int signal); -/// Write the given paragraph of output, redoing linebreaks to fit the current screen. -wcstring reformat_for_screen(const wcstring &msg); +/// Write the given paragraph of output, redoing linebreaks to fit \p termsize. +wcstring reformat_for_screen(const wcstring &msg, const termsize_t &termsize); /// Print a short message about how to file a bug report to stderr. void bugreport(); diff --git a/src/termsize.h b/src/termsize.h index 750d8f5c0..e85257d6d 100644 --- a/src/termsize.h +++ b/src/termsize.h @@ -107,4 +107,7 @@ struct termsize_container_t { friend termsize_tester_t; }; +/// Convenience helper to return the last known termsize. +inline termsize_t termsize_last() { return termsize_container_t::shared().last(); } + #endif // FISH_TERMSIZE_H