Skip to content

Commit

Permalink
Warn the user when the terminal uses grapheme clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Nov 30, 2024
1 parent 80725d0 commit 2fe2415
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 7 deletions.
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 2024-11-30 23:34:14+00:00 - build 6396

1. Warn the user when the terminal uses grapheme clusters.

--------------------------------------------------------------------------------
skipik 2024-11-26 17:23:11+03:00 - build 6395

Expand Down
6 changes: 6 additions & 0 deletions far/char_width.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,10 @@ namespace char_width
static const auto Result = console.GetWidthPreciseExpensive(U'𝖆');
return Result > 1;
}

bool is_grapheme_clusters_on()
{
static const auto Result = console.GetWidthPreciseExpensive(L""sv);

This comment has been minimized.

Copy link
@DHowett

DHowett Dec 2, 2024

FWIW, you can also detect this by checking DEC private mode 2027 with DECRQM.

Image

3 = permanently set

Image

4 = permanently reset

This comment has been minimized.

Copy link
@alabuzhev

alabuzhev Dec 2, 2024

Author Contributor

Neat, af775c5.
Thank you!

return Result == 1;
}
}
3 changes: 3 additions & 0 deletions far/char_width.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ namespace char_width

[[nodiscard]]
bool is_half_width_surrogate_broken();

[[nodiscard]]
bool is_grapheme_clusters_on();
}

#endif // CHAR_WIDTH_HPP_D66C86AC_3415_4FD1_89DA_0AB843FFEEB8
14 changes: 14 additions & 0 deletions far/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@ void Options::InterfaceSettings()
Panels->SetScreenPosition();
// $ 10.07.2001 SKV ! надо это делать, иначе если кейбар спрятали, будет полный рамс.
Panels->Redraw();

if (FullWidthAwareRendering && char_width::is_grapheme_clusters_on())
{
Message(FMSG_WARNING, msg(lng::MWarning),
{
msg(lng::MConfigFullWidthAwareRenderingGraphemeClustersDected1),
msg(lng::MConfigFullWidthAwareRenderingGraphemeClustersDected2),
msg(lng::MConfigFullWidthAwareRenderingGraphemeClustersDected3),
msg(lng::MConfigFullWidthAwareRenderingGraphemeClustersDected4),
},
{
lng::MOk
});
}
}
}

Expand Down
17 changes: 11 additions & 6 deletions far/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2906,7 +2906,7 @@ namespace console_detail
return ExternalConsole.Imports.pWriteOutput.operator bool();
}

size_t console::GetWidthPreciseExpensive(char32_t const Codepoint)
size_t console::GetWidthPreciseExpensive(string_view const Str)
{
// It ain't stupid if it works

Expand Down Expand Up @@ -2944,21 +2944,26 @@ namespace console_detail
}

DWORD Written;
const auto Pair = encoding::utf16::to_surrogate(Codepoint);
const std::array Chars{ Pair.first, Pair.second };
if (!WriteConsole(m_WidthTestScreen.native_handle(), Chars.data(), Pair.second? 2 : 1, &Written, {}))
if (!WriteConsole(m_WidthTestScreen.native_handle(), Str.data(), static_cast<DWORD>(Str.size()), &Written, {}))
{
LOGWARNING(L"WriteConsole(): {}"sv, os::last_error());
return 1;
return Str.size();
}

CONSOLE_SCREEN_BUFFER_INFO Info;
if (!get_console_screen_buffer_info(m_WidthTestScreen.native_handle(), &Info))
return 1;
return Str.size();

return Info.dwCursorPosition.X;
}

size_t console::GetWidthPreciseExpensive(char32_t const Codepoint)
{
const auto Pair = encoding::utf16::to_surrogate(Codepoint);
const std::array Chars{ Pair.first, Pair.second };
return GetWidthPreciseExpensive({ Chars.data(), Pair.second? 2uz : 1uz });
}

void console::ClearWideCache()
{
m_WidthTestScreen = {};
Expand Down
1 change: 1 addition & 0 deletions far/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ namespace console_detail
bool ExternalRendererLoaded() const;

[[nodiscard]]
size_t GetWidthPreciseExpensive(string_view Str);
size_t GetWidthPreciseExpensive(char32_t Codepoint);
void ClearWideCache();

Expand Down
56 changes: 56 additions & 0 deletions far/farlang.templ.m4
Original file line number Diff line number Diff line change
Expand Up @@ -2871,6 +2871,62 @@ upd:"Рендерінг з урахуванням повної ширини"
"Візуалізацыя з улікам поўнай шырыні"
upd:"Fullwidth-aware rendering"

MConfigFullWidthAwareRenderingGraphemeClustersDected1
upd:"It appears that your terminal supports Unicode grapheme clusters."
"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."
upd:"It appears that your terminal supports Unicode grapheme clusters."

MConfigFullWidthAwareRenderingGraphemeClustersDected2
upd:"Unfortunately, Far does not support them yet."
"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."
upd:"Unfortunately, Far does not support them yet."

MConfigFullWidthAwareRenderingGraphemeClustersDected3
upd:"For best experience it is recommended to switch to codepoint-based rendering."
"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."
upd:"For best experience it is recommended to switch to codepoint-based rendering."

MConfigFullWidthAwareRenderingGraphemeClustersDected4
upd:"Consult the documentation of your terminal for advice on how to do this."
"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."
upd:"Consult the documentation of your terminal for advice on how to do this."

MConfigClearType
"Перер&исовка с поддержкой ClearType (может быть медленной)"
"Cl&earType-friendly redraw (can be slow)"
Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6395
6396

0 comments on commit 2fe2415

Please sign in to comment.