Tree @fresh-snapshots/upstream (Download .tar.gz)
- ..
- abstract_crystal.cr
- ada1.ada
- ada1.adb
- ampl.mod
- as1.as
- assembler1.asm
- assembler2.S
- assembler6502.as8
- assembler6502.asx
- augeas.aug
- awk1.awk
- b.bfpp
- bat1.bat
- blitzmax.bmx
- boo1.boo
- brainfuck.bf
- c1.c
- c2.h.in
- c_binding_crystal.cr
- c_str.c
- chaiscript.chai
- classic_basic.b
- classic_basic.bas
- classic_basic1.classic_basic
- clearsilver_template1.cs
- click_me.xaml
- clj1.clj
- cmake1.cmake
- cobol1.cbl
- components.cu
- configure.ac
- coq.v
- cs.aspx
- cs1.cs
- css1.css
- d1.d
- dcl.com
- diff1_new.html
- diff1_old.html
- diff2_new.c
- diff2_old.c
- diff3_new.xml
- diff3_old.xml
- dylan1.dylan
- eiffel.e
- el1.el
- english.st
- erl1.erl
- example.qml
- example.R
- example.rkt
- example.xsl
- factor1.factor
- fb.js
- foo.coffee
- foo.dart
- foo.dtx
- foo.ebuild
- foo.exheres-0
- foo.glsl
- foo.lc
- foo.nsh
- foo.nsi
- foo.tex
- foo.vim
- foo_glsl.vert
- forth.4th
- forth.fs
- fortranfixed.f
- fortranfree.f
- frx1.frx
- fs1.fs
- grace.grace
- groovy1.groovy
- haml.haml
- haskell1.hs
- haskell2.hs
- haskell3.hs
- haxe1.hx
- html.cgi
- html1.html
- idl_pvwave.pro
- java1.java
- java2.java
- js1.js
- js2.js
- js3.js
- jsp1.jsp
- kotlin.kt
- limbo.b
- limbo.m
- logtalk.lgt
- lsp1.lsp
- lua1.lua
- macro_crystal.cr
- Makefile
- Makefile.am
- mathematica1.m
- matlab1.m
- metafont.mf
- metapost.mp
- modelica.mo
- mxml1.mxml
- mysql-stale-table-sniper
- nix.nix
- objj.j
- ocaml.ml
- octave1.m
- optimer
- pascal1.pas
- pascal2.pp
- perl1.pl
- perl_module.pm
- perl_pod_to_eof.pl
- php1.php
- pike1.pike
- pike2.pmod
- postscript.ps
- prolog.pl
- puppet1.pp
- puppet_empty_class_1.pp
- puppet_empty_class_2.pp
- puppet_with_include_only.pp
- py1.py
- rebol.r
- rexx1.rex
- rhtml1.rhtml
- ruby1.rb
- rust.rs
- sample.i3
- sample.m3
- sample.mod
- sample.ob2
- sample.obn
- sampleDef.def
- sampleImp.mod
- scala1.scala
- schema.xsd
- scheme.scm
- scilab.sci
- sh1.sh
- sh2.sh
- smalltalk1.st
- sql1.sql
- standard_crystal.cr
- stratego.str
- structured_basic.b
- structured_basic.bas
- tcl1.tcl
- ts1.ts
- unrealscript.uc
- vala1.vala
- vb.aspx
- vb1.vb
- vbs1.vbs
- vhdl1.vhd
- vhdl1.vhdl
- visual_basic.bas
- xml1.xml
lua1.lua @fresh-snapshots/upstream — raw · history · blame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | -- random code from http://lua-users.org/wiki/TextJustification -- easy queue implementation ------------------------------------------ function q_create() local q = {} q.first = 0 q.last = 0 return q end function q_insert(q, s) q[q.last] = s q.last = q.last + 1 end function q_empty(q) return q.first >= q.last end function q_remove(q) if q_empty(q) then return nil end local s = q[q.first] q[q.first] = nil q.first = q.first+1 return s end function q_card(q) return q.last - q.first end function q_length(q, f) local l, i = 0, q.first while i < q.last do l = l + strlen(q[i]) i = i + 1 end return l end -- line creation routines ------------------------------------------ -- justifies one line to fit into MAX columns function justify(q) local blanks = MAX - q_length(q) local skips = q_card(q) - 1 local line = q_remove(q) local quotient = floor(blanks/skips) local reminder = blanks/skips - quotient local error = 0 while skips > 0 do error = error + reminder if error >= 0.5 then error = error - 1 line = line .. strrep(" ", quotient+1) else line = line .. strrep(" ", quotient) end line = line .. q_remove(q) skips = skips - 1 end return line or "" end -- join all words with one space between them function catenate(q) local line = q_remove(q) while not q_empty(q) do line = line .. " " .. q_remove(q) end return line or "" end -- main program ----------------------------------------------------- DEFMAX = 72 -- tries to get MAX from command-line if not arg or getn(arg) < 1 then MAX = DEFMAX else MAX = tonumber(arg[1]) if not MAX or MAX < 0 then MAX = DEFMAX end end -- collects all text from stdin text = q_create() line = read() while line do _, n = gsub(line, "(%S+)", function (s) q_insert(%text, s) end) if n == 0 then q_insert(text, "\n") end line = read() end -- justify paragraphs line = q_create() word = q_remove(text) size = 0 while word do if word == "\n" then if not q_empty(line) then write(catenate(line), "\n\n") else write("\n") end size = 0 elseif size + strlen(word) > MAX then write(justify(line), "\n") size = 0 end if word ~= "\n" then q_insert(line, word) size = size + strlen(word) + 1 end word = q_remove(text) end write(catenate(line), "\n") |