Sha256: 42781bfeb13c076224698aca7dec775919789a0e0a741c19e66beb841a0283ee

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

#!/usr/bin/env fancy
# -*- fancy -*-

require("readline")

["Welcome to the (still very simple) Fancy REPL",
 "Fancy " ++ FANCY_VERSION] println

HISTORY_FILE = File expand_path("~/.fancy_history")
HISTORY = []

ARGV rest each: |file| {
  "LOADING: " ++ file println
  require: file
}

# handle SIGINT
trap("INT") {
  "Quitting." println
  save_history
  System exit
}

Console newline;

def double_or_empty?: line {
  (line =~ /^\s*$/) or: (HISTORY [-2] == line)
}

def load_history {
  if: (File exists?: HISTORY_FILE) then: {
    File open: HISTORY_FILE modes: ['read] with: |f| {
      f readlines each: |l| {
        l = l strip()
        Readline HISTORY <<(l)
        HISTORY << l
      }
    }
  } else: {
    File touch: HISTORY_FILE
  }
}

def save_history {
  puts("saving history")
  unless: @history_saved do: {
    File open: HISTORY_FILE modes: ['write] with: |f| {
      HISTORY last: 100 . each: |l| {
        f writeln: l
      }
    }
    @history_saved = true
  }
}

load_history

try {
  bnd = binding()

  while: { Readline readline(">> ", true) } do: |line| {
    HISTORY << line
    if: (double_or_empty?: line) then: {
      Readline::HISTORY pop()
      HISTORY pop()
    }

    unless: (line =~ /^\s*$/) do: {
      try {
        Fancy eval: line binding: bnd . inspect println
      } catch Exception => e {
        e message() println
      }
    }
  }
  save_history
} catch Interrupt => e {
  save_history
  System exit
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fancy-0.5.0 bin/ifancy
fancy-0.4.0 bin/ifancy