Justificador de texto em 80 colunas
Publicado por Enzo de Brito Ferber (última atualização em 22/01/2013)
[ Hits: 7.343 ]
Homepage: http://www.maximasonorizacao.com.br
Download 1358116595.just.tar (versão 2)
Hoje eu precisei justificar um texto em 80 colunas (como o man do Linux), mas não achei nenhum programa built-in pra fazer. Então, depois de procurar muito, achei uma thread que dizia que o emacs fazia isso.... Odeio o emacs.
Então fiz o programa pra trabalhar em conjunto com o fmt do GNU/Linux.
Fica assim:
$ fmt -w 79 arquivo | ./just
O just coloca tudo em 80 colunas, usando apenas espaços. Bem legal e resolveu meu problema. Pra ficar melhor é só colocar na ~/bin
Versão 2 - Enviado por Enzo de Brito Ferber em 13/01/2013
Changelog: Agora funciona normalmente com fmt -80 | just.
Adicionada apenas 1 linha na função formatline() para consertar o bug de formatação.
Quando uma linha tinha exatamente o tamanho desejado, o programa ainda assim adicionava 1 espaço a mais, fazendo com que a linha ficasse fora de formato.
if ( line[ strlen(line) - 1] != '\n' ) return line;
/*
* just.c
*
* Extention for 'fmt' unit command to justify the text
* to 'n' columns
*
* $ fmt -w 79 file | just
*
*
* Author: Enzo Ferber
* : enzoferber@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define LINESIZE 80
#define CUTSIZE 55
/*
* @ insertchar (str, pos, c )
*
* Insert character at given string positing
*
* - str : pointer to the string
* - pos : the position to insert
* - c : character to insert
*/
char *insertchar ( char *str, int pos, char c )
{
register int i;
int len = strlen(str);
// open space for the new character
for ( i = len; i >= pos; i-- )
str[i + 1] = str[i];
// puts new character
str[pos] = c;
// end of string terminator
str[len + 1] = 0x0;
// return
return str;
}
/*
* @ formatline ( line, ls )
*
* Format the line inserting spaces in it
*
* - line: pointer to the line string
* - ls : line size - PUT THE SAME NUMBER AS IN 'fmt' -w argument
*/
char *formatline ( char *line, int ls )
{
register int i;
/*
* if it's just a new line
*/
if ( line[0] == '\n') return line;
/*
* format
*/
for ( i = 0 ; i < strlen(line); i ++)
{
if ( strlen(line) >= ls ) break;
else if ( line[i] == ' ' )
line = insertchar(line, i++, ' ' );
}
/*
* maybe just one run didn't work...
* run the routine to put spaces again
*
* i.e.: strings with low spaces number
*/
if ( strlen(line) < ls )
line = formatline( line, ls );
return line;
}
int main ( void )
{
char *line = (char *)malloc(LINESIZE * sizeof(char));
while ( fgets(line, LINESIZE, stdin) != NULL )
{
printf( "%s", (strlen(line) > CUTSIZE) ?
formatline(line, LINESIZE) : line);
memset(line, 0x0, LINESIZE );
}
free ( line );
return 0;
}
Barra de progresso em forma de “roda”
Editar o arquivo /etc/rc.d/rc.local
Controle de Funcionários (Básico)
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
Necessário autenticar ao imprimir - Ubuntu X Windowns (1)
O que houve com slackware ??? (11)
O Free Download Manager não abre no Fedora 43 KDE Plasma (2)









