Re: centering display

new topic     » goto parent     » topic index » view thread      » older message » newer message

Blast from the past:

-------------------------------------------------------------------------- 
-- formtext.e by Mike Nelson 
-- text formatting for DOS/Linux  
 
include graphics.e  
 
public constant ALIGN_LEFT=1 
public constant ALIGN_CENTER=2 
public constant ALIGN_RIGHT=3 
 
 
public integer screenColumns, 
			   leftMargin, rightMargin,  
			   tabSize,  
			   textAlignment 
			    
public enum l,r 
public sequence margin = {0,0} 
-- 
-- 
public procedure init_text_formatter() 
   -- initializes screen parameters 
   -- call after changing video modes 
   sequence vidconf=video_config() 
   screenColumns=vidconf[VC_COLUMNS] 
   leftMargin=1 
   rightMargin=screenColumns 
   tabSize=5 
   textAlignment=ALIGN_LEFT 
end procedure  
init_text_formatter()  -- first initialization is automatic 
-- 
public function set_margins(integer left,integer right)     
   -- Sets margins, returns 1 or 0 on invalid margin(s). 
   -- left=0 or right=0 leaves that margin unchanged 
   -- a negative value for right indicates moving left  
   if left<0 then return 0 end if 
   if right<0 then right=screenColumns+right end if 
   if right>screenColumns then return 0 end if 
   if left>right then return 0 end if 
   if left>0 then leftMargin=left end if 
   if right>0 then rightMargin=right end if  
   return 1    
end function                      
-- 
public function get_margins() 
   return {leftMargin,rightMargin}    
end function 
margin = get_margins() 
-- 
public function set_tabsize(integer size) 
   -- sets tabsize, returns 1 or 0 on invalid size. 
   if size<0 then return 0 end if 
   if size>rightMargin-leftMargin+1 then return 0 end if 
   tabSize=size 
   return 1 
end function  
-- 
public function get_tabsize() 
   return tabSize   
end function 
-- 
public function set_alignment(integer align) 
   -- ALIGN_LEFT, ALLIGN_CENTER, ALLIGN_RIGHT 
   --sets alignment, returns 1. Returns 0 on invalid alignment 
   if find(align, {1,2,3}) then  
	  textAlignment=align  
	  return 1 
   else  
	  return 0 
   end if 
end function 
-- 
public function get_alignment() 
   return textAlignment    
end function              
-- 
function ltrim(sequence text) 
   for i=1 to length(text) do 
      if text[i]!=' ' then 
	 return text[i..length(text)] 
      end if 
   end for 
   return "" 
end function  
 
function rtrim(sequence text) 
   for i=length(text) to 1 by -1 do 
      if text[i]!=' ' then 
	 return text[1..i]    
      end if 
   end for 
   return "" 
end function 
-- 
procedure print_aligned(sequence text,integer width) 
   sequence lspace,rspace 
   integer spaces  
   spaces=width-length(text) 
   if textAlignment=ALIGN_LEFT then 
      lspace="" 
      rspace=repeat(' ',spaces) 
   elsif textAlignment=ALIGN_CENTER then 
      lspace=repeat(' ',floor(spaces/2)) 
      rspace=repeat(' ',floor((spaces+1)/2)) 
   else 
      lspace=repeat(' ',spaces) 
      rspace="" 
   end if 
   puts(1,lspace&text&rspace) 
end procedure 
-- 
public procedure print_formatted(sequence text) 
--Prints formatted text to screen starting at current postion, 
--using the curent margin and alignment settings.  
 
--Output is word-wrapped if possible, hyphenated if not. 
--Tabs are expanded to the number of spaces given  
--by the current tabsize setting. 
--Whitespace is trimmed only at word wraps and hyphenations. 
--Assumes text is a valid string. 
 
   integer width,offset,index 
   sequence pos,printText 
   while 1 do 
      index=find('\t',text) 
      if index=0 then exit end if 
      text=text[1..index-1]&repeat(' ',tabSize)&text[index+1..length(text)] 
   end while 
   width=rightMargin-leftMargin+1  
   pos=get_position() 
   offset=0 
   if pos[2]>rightMargin then 
      puts(1,"\n") 
      pos=get_position() 
   end if 
   if pos[2]<leftMargin then 
      position(pos[1],leftMargin) 
   elsif pos[2]>leftMargin then 
      offset=pos[2]-leftMargin  
   end if 
   index=find('\n',text) 
   if index=0 then index=length(text)+1 end if 
   if index<=width-offset+1 then 
      print_aligned(text[1..index-1],width-offset) 
      if index<=length(text) then 
	 print_formatted(text[index+1..length(text)]) 
      end if 
      return 
   end if 
   for i=width-offset+1 to 1 by -1 do 
      if text[i]=' ' then 
	 print_aligned(rtrim(text[1..i-1]),width) 
	 print_formatted(ltrim(text[i+1..length(text)])) 
	 return 
      end if 
   end for 
   print_aligned(text[1..width-offset-1]&'-',width-offset) 
   if length(text)>=width-offset then 
      print_formatted(ltrim(text[width-offset..length(text)])) 
   end if 
end procedure 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu