This might give you what you need (hooking in via the runtime output writer, 
which is where the print function routes to), it seems to work for my quick 
test:

using System;
using System.IO;
using System.Text;
using IronPython.Hosting;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            RunScript("22");
            RunScript("print(23)");
            Console.ReadKey();
        }

        static void RunScript(string script)
        {
            var engine = Python.CreateEngine();
            var io = engine.Runtime.IO;
            io.SetOutput(io.OutputStream, new GreenWriter(io.OutputWriter));
            var result = engine.Execute(script);
            if (result != null)
                Console.WriteLine(result);
        }
    }

    class GreenWriter : TextWriter
    {
        private readonly TextWriter _inner;
        public GreenWriter(TextWriter inner)
        {
            _inner = inner;
        }
        public override void Write(string value)
        {
            var original = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Green;
            _inner.Write(value);
            Console.ForegroundColor = original;
        }
        public override Encoding Encoding
        {
            get { return _inner.Encoding; }
        }
    }
}


Keith Rome
Senior Consultant and Architect
MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
Wintellect | 770.617.4016 | kr...@wintellect.com
www.wintellect.com

-----Original Message-----
From: Ironpython-users 
[mailto:ironpython-users-bounces+rome=wintellect....@python.org] On Behalf Of 
Doug Blank
Sent: Thursday, September 20, 2012 12:43 AM
To: ironpython-users@python.org
Subject: [Ironpython-users] Separating return values from printed values?

IronPython users,

Interesting question: we have IronPython embedded in a nice little educational 
GUI, and have the DLR output sent to our text output window where we can 
control the font color, etc.

In teaching CS, it is always hard to try to get across the difference between a 
"return value" and a "displayed string". For example:

>>> 22
22
>>> print(23)
23
>>> function()
22

where 22 is just a value, and 23 is a displayed string (a side-effect for you 
functional types). These GUIs don't make it easy to see the difference.

One thing we've thought about is making a distinction between them with color, 
such that 22 might be blue, and 23 would be green.

Can you think of an easy way to do that with IronPython and the DLR?
Anything we could tap into to send evaluations one way, and displayed values 
another?

Thanks for any suggestions,

-Doug
_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users


_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to