wt [en X11-Basic]

Descripción del contenido de la página

Justificador de texto para programas en X11-Basic.

Etiquetas:

wt es un pequeño accesorio para X11-Basic que permite imprimir en el modo de texto párrafos justificados a la izquierda.

wt comenzó como parte de un proyecto mayor aún en desarrollo, una aventura de texto, pero pronto fue extraído como un módulo reutilizable.

Código fuente

' wt.xbas

' ==============================================================
' Description

' wt ("wrapping text") is a text output module for X11-Basic
' console programs.
' <http://programandala.net/en.program.wt.html>

' Version 0.1.0+201707291402
' (after Semantic Versioning: http://semver.org)
' See change log at the end of the file

' ==============================================================
' Author and license

' Author: Marcos Cruz (programandala.net),
' 2012,2013,2014,2016,2017 

' You may do whatever you want with this work, so long as you
' retain all copyright, authorship and credit notices and this
' license in all redistributed copies and derived works.
' There is no warranty.

' ==============================================================
' To-do

' 2014-03-31:
' scroll, pause

' ==============================================================
' Main {{{1

procedure wt_init

  ' Init the constants and variables of the module.

  ' Internal constant
  let wt_plus_background%=10 ! background color is foreground color +10
  ' Internal variables
  let wt_effect%=0
  let wt_foreground_color%=0
  let wt_background_color%=0
  let wt_free_cols%=0
  let wt_free_rows%=0

  ' User constants (text effects)
  let none%=0
  let intensive%=1
  let dark%=2
  let underline%=4
  let blink%=5
  let reverse%=7
  ' User constants (text colors)
  let black%=30
  let red%=31
  let green%=32
  let yellow%=33
  let blue%=34
  let magenta%=35
  let cyan%=36
  let white%=37
  ' User variables
  let wt_indentation%=2 ! Spaces at the start of a paragraph's first line
  let wt_paragraph_separation%=0 ! Blank lines between paragraphs

  return

procedure wt_paper(color%)
  ' Set the background color.
  let wt_background_color%=color%+wt_plus_background%
  return

procedure wt_pen(color%)
  ' Set the foreground color.
  let wt_foreground_color%=color%
  return

procedure wt_effect(effect_%)
  ' Set the text effect.
  let wt_effect%=effect_%
  return

procedure wt_home
  ' Set the cursor at the top left position.
  locate 0,0
  let wt_free_cols%=cols
  let wt_free_rows%=rows
  return

procedure wt_cls
  ' Clear the screen.
  cls
  @wt_home
  return

procedure wt_wipe
  ' Wipe the screen with the current color.
  @wt_line(space$(rows*cols)) ! paint the screen
  @wt_home
  return

function wt_at_first_col%
  ' Is the cursor at the first column?
  return wt_free_cols%=cols

function wt_at_first_row%
  ' Is the cursor at the first row?
  return wt_free_rows%=rows

procedure wt_do_cr
  ' Do a carriage return.
  print
  let wt_free_cols%=cols
  dec wt_free_rows%
  return

procedure wt_cr
  ' Do a carriage return, if needed.
  if not @wt_at_first_col%()
    @wt_do_cr
  endif
  return

procedure wt_line(text$)
  ' Print a text at the current cursor position; the text is not
  ' longer than the free space of the current row.
  print color(wt_effect%,wt_background_color%,wt_foreground_color%);text$;
  flush
  sub wt_free_cols%,len(text$)
  return

procedure wt_indent
  ' Do a carriage return and indent.
  @wt_cr
  if not @wt_at_first_row%()
    ' An additional space will be shown before the first
    ' word of the paragraph, so one space is removed here:
    @wt_line(space$(max(wt_indentation%-1,0)))
  endif
  return

procedure wt(text$)
  ' Print a text from the current cursor position.
  local a_word$
  while left$(text$,1)=" "
    @wt_line(" ")
    let text$=rightof$(text$," ")
  wend
  if right$(text$,1)<>" "
    let text$=text$+" "
  endif
  do
    split text$," ",0,a_word$,text$
    if (wt_free_cols%-1)>=len(a_word$)
      if not @wt_at_first_col%()
        @wt_line(" ")
      endif
    else
      @wt_cr
    endif
    @wt_line(a_word$)
    exit if text$=""
  loop
  return

procedure wt_(text$)
  ' Print a text on a new line.
  @wt_cr
  @wt(text$)
  return

procedure wt__(text$)
  ' Print a text on a new paragraph.
  local i%
  if wt_paragraph_separation%>0 and not @wt_at_first_row%()
    for i%=1 to wt_paragraph_separation%
      @wt_do_cr
    next i%
  endif
  @wt_indent
  @wt(text$)
  return

' ==============================================================

Descarga

Páginas relacionadas

tw
Ventanas de texto para programas en X11-Basic.
wt [para SAM Coupé, en MBim]
Justificador de texto para programas en MBim.