wt [in X11-Basic]

Description of the page content

Text wrapping module for X11-Basic console programs.

Tags:

wt stands for “wrapping text”. It's a little addon for X11-Basic that makes it possible to print left-justified paragraphs in console programs.

wt started as part of a bigger project still under development, a text adventure game, but soon was extracted as a re-usable module.

Source code

' 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

' ==============================================================
' Change log {{{1

' 2013-09-12:
'
' Code extracted from an old version (A-00-2013052214) of Fifi,
' a text adventure game under development; current versions of
' Fifi use the "tw" ("text window") module instead.  Procedures
' and variables are renamed. 
'
' New: 'wt_init', to be called by the main program; this is
' needed because the values of the variables are not preserved
' after 'merge'.
'
' Fix: 'wt_indentation%' was not declared.
'
' Change: '_wt' just does a carriage return; now the new '__wt'
' starts a new paragraph.
'
' 2013-09-19:
'
' Fix: 'flush' added to 'wt_line' in order to prevent the
' effects of buffered output (e.g. 'pause' takes effect before a
' preceding text).
'
' 2014-02-12:
'
' Two typos fixed in comments.  Change: '_wt' and '__wt' renamed
' to 'wt_' and 'wt__'.
'
' 2014-06-12:
'
' New: 'wt_paragraph_separation%'.
'
' 2014-07-24:
'
' Fix: In 'wt__' an empty line was printed also when
' 'wt_paragraph_separation%' was 0.
'
' 2014-12-12:
'
' Some changes in the source and comments layout. All
' 'endfunction' are removed; they are not mandatory. Version
' B-00, for publishing.
'
' 2017-07-29:
'
' Update source style. Change version numbering and license.

Download

Related pages

tw
Text windows for X11-Basic console programs.
wt [for SAM Coupé, in MBim]
Text wrapping module for MBim console programs.