fgallaire

fgallaire

Hello everybody,

I’m new to elixir and I don’t find a text wrapping library, something
like Python textwrap.

Is it missing ? If yes, is there a will to have it ?

Cheers

Showing Posts 1 to 4

kokolegorille

kokolegorille

Welcome,

The best way to search for a specific lib is to look at hex.pm

For example… with text as search key.

I am not sure this one compares to TextWrap, but it seems to help with text format.

You might also enjoy this read, if You are in the mood of rewriting it, and don’t mind reading some Erlang code.

ferd

ferd

Author of Property-Based Testing with PropEr, LYSE, & Erlang in Anger

Be aware of the big wrapping dangers of Unicode. U+FDFD is a single very wide character: ﷽ in some fonts, and a reasonably large one in others. Ask your library to wrap, and if it has no knowledge of font widths (and you’re not using a monospace font), you’ll end up with lines that are guaranteed to blow everything up.

I would generally maintain healthy skepticism when someone attempts to wrap text without knowing how it is going to be rendered, although the rest of the functionality of the textwrap module in python are quite useful.

Paradox

Paradox

I haven’t seen nor found any that do exactly what you want, but the algorithm isn’t terribly complicated; with a bit of recursion you can actually accomplish it pretty quickly.

Of particular note is the Knuth-Pass line-wrap algorithim

RosettaCode shows this implementation of a line wrapper:

defmodule Word_wrap do
  def paragraph( string, max_line_length ) do
    [word | rest] = String.split( string, ~r/\s+/, trim: true )
    lines_assemble( rest, max_line_length, String.length(word), word, [] )
      |> Enum.join( "\n" )
  end
 
  defp lines_assemble( [], _, _, line, acc ), do: [line | acc] |> Enum.reverse
  defp lines_assemble( [word | rest], max, line_length, line, acc ) do
    if line_length + 1 + String.length(word) > max do
      lines_assemble( rest, max, String.length(word), word, [line | acc] )
    else
      lines_assemble( rest, max, line_length + 1 + String.length(word), line <> " " <> word, acc )
    end
  end
end
 
text = """
Even today, with proportional fonts and complex layouts, there are still cases where you need to 
wrap text at a specified column. The basic task is to wrap a paragraph of text in a simple way in
your language. If there is a way to do this that is built-in, trivial, or provided in a standard 
library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia.
"""
Enum.each([72, 80], fn len ->
  IO.puts String.duplicate("-", len)
  IO.puts Word_wrap.paragraph(text, len)
end)
fgallaire

fgallaire OP

@kokolegorille thanks for the tip and the link
@ferd I know perfect wrapping text is a complex problem, but most of the time a not so good wrapping is good enough
@Paradox thanks for the link

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews