Top console Questions

List of Tags

Does anyone know how to print debug messages in the Google Chrome Javascript Console?

Please note that the Javascript Console is not the same as the Javascript Debugger, they have different syntaxes AFAIK, so the print command in Javascript Debugger will not work here. In the Javascript Console, print() will send the parameter to the printer.

There is a related question already on SO, but it does not solve my problem: http://stackoverflow.com/questions/45965/how-do-i-use-the-javascript-console-in-google-chrome

Answered By: Sergey Ilinsky ( 308)

Executing following code from the browser address bar:

javascript: console.log(2);

successfully prints message to the "JavaScript Console" in Google Chrome.

179
CodingWithoutComments

How do I call console/bash commands from inside of a Ruby Program? Also, how do I get output from these commands back into my program?

Answered By: Steve Willard ( 242)

This explanation is based on this commented Ruby script from a friend of mine. If you want to improve the script, feel free to update it at the link.

Ways to execute a shell script

cmd = "echo 'hi'" # Sample string that can be used

1. Kernel#`, commonly called backticks – `cmd`

This is like many other languages, including Bash, PHP, and Perl

Returns the result of the shell command

Docs: http://ruby-doc.org/core/Kernel.html#method-i-60

value = `echo 'hi'`
value = `#{cmd}`

2. Built-in syntax, %x( cmd )

Following the x character is a delimiter, which can be any character. If the delimiter is one of the characters (, [, {, or <, the literal consists of the characters up to the matching closing delimiter, taking account of nested delimiter pairs. For all other delimiters, the literal comprises the characters up to the next occurrence of the delimiter character. String interpolation #{ ... } is allowed.

Returns the result of the shell command, just like the backticks

Docs: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html

value = %x( echo 'hi' )
value = %x[ #{cmd} ]

3. Kernel#system

Executes the given command in a subshell,

Return: true if the command was found and ran successfully, false otherwise

Docs: http://ruby-doc.org/core/Kernel.html#method-i-system

wasGood = system( "echo 'hi'" )
wasGood = system( cmd )

4. Kernel#exec

Replaces the current process by running the given external command.

Return: none, the current process is replaced and never continues

Docs: http://ruby-doc.org/core/Kernel.html#method-i-exec

exec( "echo 'hi'" )
exec( cmd ) # Note: this will never be reached beacuse of the line above

Extra Advice

$?, which is the same as $CHILD_STATUS, accesses the status of the last system executed command if you use the backticks, system() or %{}. You can then access the exitstatus and pid properties

$?.exitstatus

More Reading

169
Alan Storm

I find working on the command line in Windows frustrating, primarily because the console window is wretched to use compared to terminal applications on linux and OS X such as "rxvt", "xterm", or "Terminal". Major complaints:

  1. No standard copy/paste. You have to turn on "mark" mode and it's only available from a multi-level popup triggered by the (small) left hand corner button. Then copy and paste need to be invoked from the same menu

  2. You can't arbitrarily resize the window by dragging, you need to set a preference (back to the multi-level popup) each time you want to resize a window

  3. You can only make the window so big before horizontal scroll bars enter the picture. Horizontal scroll bars suck.

  4. With the cmd.exe shell, you can't navigate to folders with \\netpath notation (UNC?), you need to map a network drive. This sucks when working on multiple machines that are going to have different drives mapped

Are there any tricks or applications, (paid or otherwise), that address these issue?

Answered By: Maximus ( 120)

Sorry for the self-promotion, I'm the author of another Console Emulator, not mentioned here.

ConEmu is opensource console emulator with tabs, which represents multiple consoles and simple GUI applications as one customizable GUI window.

Initially, the program was designed to work with Far Manager (my favorite shell replacement - file and archive management, command history and completion, powerful editor). But ConEmu can be used with any other console application or simple GUI tools (like PuTTY for example). ConEmu is a live project, open to suggestions.

A brief excerpt from the long list of options:

  • Latest versions of ConEmu may set up itself as default terminal for Windows
  • Use any font installed in the system, or copied to a folder of the program (ttf, otf, fon, bdf)
  • Run selected tabs as Administrator (Vista+) or as selected user
  • Windows 7 Jump lists and Progress on taskbar
  • Integration with DosBox (useful in 64bit systems to run DOS applications)
  • Smooth resize, maximized and fullscreen window modes
  • Scrollbar initially hidden, may be revealed by mouseover or checkbox in settings
  • Optional settings (e.g. pallette) for selected applications
  • User friendly text and block selection (from keyboard or mouse), copy, paste, text search in console
  • ANSI X3.64 and Xterm 256 color

Far Manager users will acquire shell style drag-n-drop, thumbnails and tiles in panles, tabs for editors and viewers, true colors and font styles (italic/bold/underline).

PS. Far Manager supports UNC paths (\\server\share\...)

When I load script/console, some times I want play with the output of a controller or a view helper method.

Are there ways to:

  • simulate a request?
  • call methods from a controller instance on said request?
  • test helper methods, either via said controller instance or another way?
Answered By: kch ( 185)

To call helpers, use the helper …hmm… helper.

$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"

If you want to use a helper that's not included by default (say, because you removed helper :all from ApplicationController), just include the helper.

>> include BogusHelper
>> helper.bogus
=> "bogus output"

As for dealing with controllers, I quote Nick's answer:

> app.get '/posts/1'
> response = app.response
# you now have a rails response object much like the integration tests

> response.body            # get you the HTML
> response.cookies         # hash of the cookies

# etc, etc

I have a console app, and I would like to find the application's path.

In winforms I can use Application.StartupPath to find the current path, but this doesn't seem to be available in a console app?

Answered By: Dan-o ( 210)

System.Reflection.Assembly.GetExecutingAssembly().Location

Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.

UPDATE (solution)

Months later someone turned me on to an awesome solution to this. If you install Git for Windows, it comes with this console app called Git Bash that does exactly what I descibed. I gather that it is derived from MSYS. It's easy and convenient and gives you all the Unix bash command line conveniences. Tab-completion works the way it does in bash (completes up to the point of ambiguity and doesn't try to guess beyond that forcing you to backspace out of an incorrect completion) and you can control-R to search command history, and history is preserved across restarts.


Can someone recommend a Windows XP program that provides a command line environment? I am an avid Cygwin user, but it doesn't solve my current problem. The problem with Cygwin that's tripping me up is that paths within the Cygwin environment are different from those under Windows, so some things I need to do are failing. My current project requires the Windows environment, not an alternate universe.

I've also considered PowerShell and Windows Services for Unix. I might try them later but for this project, I don't have time to learn a whole programming language that is PowerShell, and I suspect that WSU will have the same path problem as Cywin.

What I'm really looking for to solve my current problem is an app that gives me a Windows console but with some basic amenities added such as...

  • Tab-completion.
  • Being able to cut and paste text appearing in the window.
  • Search command history.
  • Being able to make the window wider than 80 chars.
Answered By: Burnstreet ( 142)

There is also Console: http://sourceforge.net/projects/console/ it is a replacement for the Windows console window and much more flexible (tabs, transparency, presets, etc) For a better tab completion you need a different command interpreter like 4nt / take command.


Alternatively, there is now ConEmu, another console replacement but more configurable and actively maintained than Console.

As for bash-style completion, recent builds of ConEmu integrate with Clink. More info on what Clink can do here. Or you can use TCC/LE with ConEmu. It also offers powerful bash-style completion plus a whole lot more.