How to detect less -R from python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I can use termcolor[1] to colorize the output from my command-line Python utility. However if piping to "less" or "more" or redirecting to a file the colors show up as an ugly soup of escape codes, basically unreadable.



To address this I can check sys.stdout.isatty() and only colorize if going directly to a terminal. However when piping to "less -R" I do want to colorized, because it can handle it.



Is there a way do use colors if writing to a real terminal or if pipeline to a command like "less -R" which can handle colors?



[1] - https://pypi.org/project/termcolor/










share|improve this question























  • A program doesn't know what happens to its output once it's written, all it can do is detect if stdout appears to be a tty or not. I suggest you give your Python script an option to manually override the result of sys.stdout.isatty(), similar to what ls does.

    – PM 2Ring
    Nov 23 '18 at 14:47













  • Yeah I have a "force colorized" flag and that works. Was hoping for something better. This seems like a real gap with pipe. Interestingly "head" can deal with non-colorized or colorized output. But "less" cannot, it requires "less -R" if the output is colorized. Seems like less is dumb here.

    – Philip
    Nov 23 '18 at 15:06













  • If there were a simple way to solve this issue, then ls would be using it. It's not so much that less is dumb. It's just that by default it assumes you want to read binary data if you feed it binary data. ;)

    – PM 2Ring
    Nov 23 '18 at 15:16













  • Yeah that's too bad. It's pretty lame. If A is sending data to B without knowing what format B wants, it should either have a way to ask B, or it should send both formats and let B decide. Just dreaming here. Thanks for confirmation we as a species still have work to do. Ha.

    – Philip
    Nov 23 '18 at 15:55













  • I know very little about PowerShell, but I think they try to address this by piping objects between commands not text. Anyway good to confirm this is not possible with Linux so it doesn't keep me up at night.

    – Philip
    Nov 24 '18 at 2:39




















0















I can use termcolor[1] to colorize the output from my command-line Python utility. However if piping to "less" or "more" or redirecting to a file the colors show up as an ugly soup of escape codes, basically unreadable.



To address this I can check sys.stdout.isatty() and only colorize if going directly to a terminal. However when piping to "less -R" I do want to colorized, because it can handle it.



Is there a way do use colors if writing to a real terminal or if pipeline to a command like "less -R" which can handle colors?



[1] - https://pypi.org/project/termcolor/










share|improve this question























  • A program doesn't know what happens to its output once it's written, all it can do is detect if stdout appears to be a tty or not. I suggest you give your Python script an option to manually override the result of sys.stdout.isatty(), similar to what ls does.

    – PM 2Ring
    Nov 23 '18 at 14:47













  • Yeah I have a "force colorized" flag and that works. Was hoping for something better. This seems like a real gap with pipe. Interestingly "head" can deal with non-colorized or colorized output. But "less" cannot, it requires "less -R" if the output is colorized. Seems like less is dumb here.

    – Philip
    Nov 23 '18 at 15:06













  • If there were a simple way to solve this issue, then ls would be using it. It's not so much that less is dumb. It's just that by default it assumes you want to read binary data if you feed it binary data. ;)

    – PM 2Ring
    Nov 23 '18 at 15:16













  • Yeah that's too bad. It's pretty lame. If A is sending data to B without knowing what format B wants, it should either have a way to ask B, or it should send both formats and let B decide. Just dreaming here. Thanks for confirmation we as a species still have work to do. Ha.

    – Philip
    Nov 23 '18 at 15:55













  • I know very little about PowerShell, but I think they try to address this by piping objects between commands not text. Anyway good to confirm this is not possible with Linux so it doesn't keep me up at night.

    – Philip
    Nov 24 '18 at 2:39
















0












0








0


0






I can use termcolor[1] to colorize the output from my command-line Python utility. However if piping to "less" or "more" or redirecting to a file the colors show up as an ugly soup of escape codes, basically unreadable.



To address this I can check sys.stdout.isatty() and only colorize if going directly to a terminal. However when piping to "less -R" I do want to colorized, because it can handle it.



Is there a way do use colors if writing to a real terminal or if pipeline to a command like "less -R" which can handle colors?



[1] - https://pypi.org/project/termcolor/










share|improve this question














I can use termcolor[1] to colorize the output from my command-line Python utility. However if piping to "less" or "more" or redirecting to a file the colors show up as an ugly soup of escape codes, basically unreadable.



To address this I can check sys.stdout.isatty() and only colorize if going directly to a terminal. However when piping to "less -R" I do want to colorized, because it can handle it.



Is there a way do use colors if writing to a real terminal or if pipeline to a command like "less -R" which can handle colors?



[1] - https://pypi.org/project/termcolor/







python terminal






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 14:40









PhilipPhilip

3761822




3761822













  • A program doesn't know what happens to its output once it's written, all it can do is detect if stdout appears to be a tty or not. I suggest you give your Python script an option to manually override the result of sys.stdout.isatty(), similar to what ls does.

    – PM 2Ring
    Nov 23 '18 at 14:47













  • Yeah I have a "force colorized" flag and that works. Was hoping for something better. This seems like a real gap with pipe. Interestingly "head" can deal with non-colorized or colorized output. But "less" cannot, it requires "less -R" if the output is colorized. Seems like less is dumb here.

    – Philip
    Nov 23 '18 at 15:06













  • If there were a simple way to solve this issue, then ls would be using it. It's not so much that less is dumb. It's just that by default it assumes you want to read binary data if you feed it binary data. ;)

    – PM 2Ring
    Nov 23 '18 at 15:16













  • Yeah that's too bad. It's pretty lame. If A is sending data to B without knowing what format B wants, it should either have a way to ask B, or it should send both formats and let B decide. Just dreaming here. Thanks for confirmation we as a species still have work to do. Ha.

    – Philip
    Nov 23 '18 at 15:55













  • I know very little about PowerShell, but I think they try to address this by piping objects between commands not text. Anyway good to confirm this is not possible with Linux so it doesn't keep me up at night.

    – Philip
    Nov 24 '18 at 2:39





















  • A program doesn't know what happens to its output once it's written, all it can do is detect if stdout appears to be a tty or not. I suggest you give your Python script an option to manually override the result of sys.stdout.isatty(), similar to what ls does.

    – PM 2Ring
    Nov 23 '18 at 14:47













  • Yeah I have a "force colorized" flag and that works. Was hoping for something better. This seems like a real gap with pipe. Interestingly "head" can deal with non-colorized or colorized output. But "less" cannot, it requires "less -R" if the output is colorized. Seems like less is dumb here.

    – Philip
    Nov 23 '18 at 15:06













  • If there were a simple way to solve this issue, then ls would be using it. It's not so much that less is dumb. It's just that by default it assumes you want to read binary data if you feed it binary data. ;)

    – PM 2Ring
    Nov 23 '18 at 15:16













  • Yeah that's too bad. It's pretty lame. If A is sending data to B without knowing what format B wants, it should either have a way to ask B, or it should send both formats and let B decide. Just dreaming here. Thanks for confirmation we as a species still have work to do. Ha.

    – Philip
    Nov 23 '18 at 15:55













  • I know very little about PowerShell, but I think they try to address this by piping objects between commands not text. Anyway good to confirm this is not possible with Linux so it doesn't keep me up at night.

    – Philip
    Nov 24 '18 at 2:39



















A program doesn't know what happens to its output once it's written, all it can do is detect if stdout appears to be a tty or not. I suggest you give your Python script an option to manually override the result of sys.stdout.isatty(), similar to what ls does.

– PM 2Ring
Nov 23 '18 at 14:47







A program doesn't know what happens to its output once it's written, all it can do is detect if stdout appears to be a tty or not. I suggest you give your Python script an option to manually override the result of sys.stdout.isatty(), similar to what ls does.

– PM 2Ring
Nov 23 '18 at 14:47















Yeah I have a "force colorized" flag and that works. Was hoping for something better. This seems like a real gap with pipe. Interestingly "head" can deal with non-colorized or colorized output. But "less" cannot, it requires "less -R" if the output is colorized. Seems like less is dumb here.

– Philip
Nov 23 '18 at 15:06







Yeah I have a "force colorized" flag and that works. Was hoping for something better. This seems like a real gap with pipe. Interestingly "head" can deal with non-colorized or colorized output. But "less" cannot, it requires "less -R" if the output is colorized. Seems like less is dumb here.

– Philip
Nov 23 '18 at 15:06















If there were a simple way to solve this issue, then ls would be using it. It's not so much that less is dumb. It's just that by default it assumes you want to read binary data if you feed it binary data. ;)

– PM 2Ring
Nov 23 '18 at 15:16







If there were a simple way to solve this issue, then ls would be using it. It's not so much that less is dumb. It's just that by default it assumes you want to read binary data if you feed it binary data. ;)

– PM 2Ring
Nov 23 '18 at 15:16















Yeah that's too bad. It's pretty lame. If A is sending data to B without knowing what format B wants, it should either have a way to ask B, or it should send both formats and let B decide. Just dreaming here. Thanks for confirmation we as a species still have work to do. Ha.

– Philip
Nov 23 '18 at 15:55







Yeah that's too bad. It's pretty lame. If A is sending data to B without knowing what format B wants, it should either have a way to ask B, or it should send both formats and let B decide. Just dreaming here. Thanks for confirmation we as a species still have work to do. Ha.

– Philip
Nov 23 '18 at 15:55















I know very little about PowerShell, but I think they try to address this by piping objects between commands not text. Anyway good to confirm this is not possible with Linux so it doesn't keep me up at night.

– Philip
Nov 24 '18 at 2:39







I know very little about PowerShell, but I think they try to address this by piping objects between commands not text. Anyway good to confirm this is not possible with Linux so it doesn't keep me up at night.

– Philip
Nov 24 '18 at 2:39














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448705%2fhow-to-detect-less-r-from-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448705%2fhow-to-detect-less-r-from-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings