Skip to content

Automatically shrink longest column until row fits terminal width #142

@forthrin

Description

@forthrin
#!/usr/bin/env ruby

longest = -> { it.each_with_index.max_by { |str, _| str.length }.last }
dict = File.readlines('/usr/share/dict/words', chomp: true)
width = 80

loop do
  words = dict.sample(7)
  line = nil
  loop do
    line = words.join(' | ')
    break if line.length <= width

    w = longest.call(words)
    word = words[w].delete('…')
    mid = word.length / 2
    parts = [word[0, mid], word[mid..]]
    p = longest.call(parts)
    p.zero? ? parts[p].chop! : parts[p].slice!(0)
    words[w] = parts.join('…')
  end
  puts line
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions