alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (ANSIWriter.scala)

This example Scala source code file (ANSIWriter.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

ansiwriter, int, io, list, list, many, many, none, none, outputstream, some, string, string, unit, writer

The Scala ANSIWriter.scala source code

/* NEST (New Scala Test)
 * Copyright 2007-2011 LAMP/EPFL
 * @author Philipp Haller
 */

package scala.tools
package partest
package io

import java.io.{ Writer, PrintWriter, OutputStream, OutputStreamWriter }

object ANSIWriter {
  val NONE = 0
  val SOME = 1
  val MANY = 2

  def apply(isAnsi: Boolean) = if (isAnsi) MANY else NONE  
}
import ANSIWriter._

class ANSIWriter(writer: Writer) extends PrintWriter(writer, true) {  
  def this(out: OutputStream) = this(new OutputStreamWriter(out))
  def colorful: Int = NONE
  
  protected val manyColors = List(
    Console.BOLD + Console.BLACK,
    Console.BOLD + Console.GREEN,
    Console.BOLD + Console.RED,
    Console.BOLD + Console.YELLOW,
    Console.RESET
  )
  protected val someColors = List(
    Console.BOLD + Console.BLACK,
    Console.RESET,
    Console.BOLD + Console.BLACK,
    Console.BOLD + Console.BLACK,
    Console.RESET
  )
  protected val noColors = List("", "", "", "", "")
  
  lazy val List(_outline, _success, _failure, _warning, _default) = colorful match {
    case NONE => noColors
    case SOME => someColors
    case MANY => manyColors
    case _    => noColors
  }
  
  private def wrprint(msg: String): Unit = synchronized {
    print(msg)
    flush()
  }

  def outline(msg: String) = wrprint(_outline + msg + _default)
  def success(msg: String) = wrprint(_success + msg + _default)
  def failure(msg: String) = wrprint(_failure + msg + _default)
  def warning(msg: String) = wrprint(_warning + msg + _default)
  def normal(msg: String) = wrprint(_default + msg)
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala ANSIWriter.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.