reload!
Tag Archive for console
Refresh models in console
Compile all css files in a rails project
# compiles all the css files into one huge string
# this is useful when running with less
# USAGE : all_css.open # => opens the string in less allowing you to search
def all_css
css_files = Dir.glob File.join(RAILS_ROOT, "public/stylesheets/","*.css")
css_content = ""
css_files.each {|file| css_content << IO.read(file)}
css_content.instance_eval do
# opens with less command
def open
system('echo "' << self << '"|less')
end
end
css_content
end
Debug object
if(!window.config) var config = {};
config.Debug = {
maxPopups: 5 //default number of popups/alerts allowed before asking to continue
};
//static Debug object
var Debug = function()
{
//***** private properties and methods *****//
var maxPopups = window.config.Debug.maxPopups;
var debugWindow;
var popupCount = 0;
var continuePopups = true;
//write a message (HTML) to the debug window
function write(msg)
{
if(!debugWindow || !debugWindow.document)
{
debugWindow = window.open("", "debug"); //create the debug window
if(!debugWindow) //a popup blocker caught it
{
msg = msg.replace(/<br(s+/)?>/ig, "
");
debugAlert(msg); //use an alert instead
return;
}
//debugWindow.document.open("text/html"); //it's text/html by default
debugWindow.document.write("<html><head><title>Debug: "+document.title+"</title></head><body></body></html>");
//note: <title> only works in Opera
debugWindow.document.close();
}
debugWindow.document.title = "Debug: "+document.title; //doesn't work in Opera
debugWindow.document.body.innerHTML += msg+"<br />";
}
//write some code to the debug window
function writeCode(code)
{
code = code.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
write('<pre style="background-color:#EEEEEE; margin:1em 0; min-height:2.5em; overflow:auto;">'+code+'</pre>');
}
//log a message to the javascript/error console, if supported
function logToConsole(msg)
{
if(window.console) window.console.log(msg);
}
//open a new window with specified content (employs maxPopups)
function popup(content, mimeType)
{
if(!continuePopups) return;
if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
{
//This is a safety mechanism in case popups/alerts are displayed in large quantities
// (e.g., if it was called inside a loop)
continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
if(!continuePopups) return;
}
var popWindow = window.open("", ""); //create a new window
if(!popWindow) //a popup blocker caught it
{
debugAlert(content); //use an alert instead
return;
}
if(mimeType) popWindow.document.open(mimeType); //it's text/html by default
//As far as I know, text/xml isn't supported. In Firefox it becomes text/plain instead of text/html.
//Also, setting mimeType to text/plain doesn't always work; use popupCode function below instead
popWindow.document.write(content);
popWindow.document.close();
popWindow.document.title = "Debug Popup: "+document.title;
popupCount++;
}
//open a new window with specified content as code (employs maxPopups)
function popupCode(content)
{
content = content.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
popup("<pre>"+content+"</pre>");
}
//displays an alert (employs maxPopups)
function debugAlert(txt)
{
if(!continuePopups) return;
if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
{
//This is a safety mechanism in case popups/alerts are displayed in large quantities
// (e.g., if it was called inside a loop)
continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
if(!continuePopups) return;
}
window.alert(txt);
popupCount++;
}
//from <a href="http://code.google.com/p/trimpath/wiki/TrimBreakpoint" >http://code.google.com/p/trimpath/wiki/TrimBreakpoint</a>
//allows you to evaluate expressions in the current scope
//usage: Debug.breakpoint(function(s){return eval(s);}, "Note about breakpoint", "1+2");
//the first argument is required as shown (to access the closure)
function breakpoint(evalFunc, msg, initialExpr)
{
msg = msg || "";
var result = "";
var expr = initialExpr || "";
if(expr)
{
try{
result = evalFunc(expr);
}catch(e){
result = e;
}
}
while(true)
{
expr = prompt("BREAKPOINT: " + msg + "
Enter an expression to evaluate, or Cancel to continue.
Result:
" +
result, expr);
if(!expr) return; //cancelled or left blank
try{
result = evalFunc(expr);
}catch(e){
result = e;
}
}
}
function resetPopups()
{
popupCount = 0;
continuePopups = true;
maxPopups = window.config.Debug.maxPopups; //in case it changed
}
//***** public properties and methods *****//
return {
write: write,
writeCode: writeCode,
log: logToConsole,
popup: popup,
popupCode: popupCode,
alert: debugAlert,
breakpoint: breakpoint,
resetPopups: resetPopups
}
}(); //initialize Debug
Category: Uncategorized |
Tags: alert, breakpoint, console, debug, javascript, object, popup, window
Check MySQL Queries
# mysqladmin -vi 1 proc
Stop Firebug Console Command Errors
(function(){
if (!window.console || !console.firebug) {
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; i++) {
window.console[names[i]] = function(){};
}
}
})();
Browser friendly logging
function log(message) {
if (console) console.log(message)
else if (window.console) window.console.log(message)
else alert(message);
}
C Console Scripting Framework
#include <stdio.h>
//#include <stdlib.h> //for system pause
//USAGE (in DOS): dir blah | yourProgramName > outFile.txt
// (use /b for JUST file and folder names)
// (use /b /ad for JUST folder names)
void printsln(char *s) {printf("%s
", s);}
void error(char *s){printsln(s); exit(1);}
bool ngets(char *s, int n) {
int i = 0;
char c;
c = getchar();
if (c==EOF) {s[i] = 0; return false;}
while(c!='
'){
if(i>=n) error("input stream overflowed buffer");
s[i++] = (char)c;
c = getchar();
}
s[i] = 0;
return true;
}
int main(int argc, char *argv[])
{
char s[10000]; //note: possible (in theory) security hole
while(ngets(s, 10000)) { //security hole closed.
printf("/new/%s
", s, s); //TWEAK THIS LINE!!!
}
//system("pause");
return 0;
}
Better Unicode Text Wrapping Function
# This recipe refers:
#
# <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061" >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061</a>
import re
rx=re.compile(u"([u2e80-uffff])", re.UNICODE)
def cjkwrap(text, width, encoding="utf8"):
return reduce(lambda line, word, width=width: '%s%s%s' %
(line,
[' ','n', ''][(len(line)-line.rfind('n')-1
+ len(word.split('n',1)[0] ) >= width) or
line[-1:] == ' ' and 2],
word),
rx.sub(r'1 ', unicode(text,encoding)).split(' ')
).replace(' ', '').encode(encoding)
Wrap Text to Eighty Columns
def wrap_text(txt, col = 80):
import re
re.sub("/(.{1,#{col}})( +|$n?)|(.{1,#{col}})/", "13n", txt)
# warning: snipplr tends to escape the quotes and slashes, causing this
# snippet to fail when pasted in.
return txt