1

I want to record the weather info within org with commands

#+BEGIN_SRC shell :results output
curl wttr.in/place
#+END_SRC

#+RESULTS:
#+begin_example
Weather report: place

 [38;5;226m    \   /    [0m Clear
 [38;5;226m     .-.     [0m [38;5;047m7[0m °C[0m           
 [38;5;226m  ― (   ) ―  [0m [1m↑[0m [38;5;046m0[0m km/h[0m       
 [38;5;226m     `-’     [0m 10 km[0m          
 [38;5;226m    /   \    [0m 0.0 mm[0m         

The gibberish get outputted however,

How could get it display as 'curl wttr.in' work on terminal ?

5

Use the T option to turn off the color (you can learn all possible options on http://wttr.in/:help, I learnt this from https://github.com/chubin/wttr.in):

T # switch terminal sequences off (no colors)

For example,

#+BEGIN_SRC sh :results output
curl 'wttr.in/?0T'
#+END_SRC

#+RESULTS:
: Weather report: Yancheng, China
: 
:     \  /       Partly cloudy
:   _ /"".-.     15..16 °C      
:     \_(   ).   ← 12 km/h      
:     /(___(__)  10 km          
:                0.0 mm   

The 0 option means only current weather.


There is also a hard way suggested by @legoscia's answer, you can remove the ansi escape sequences with ansi-color-apply (it translates the ascii escape color codes into text props which can be easily ignored or stripped).

#+NAME: strip
#+BEGIN_SRC elisp :var text="\e[31mHello world\e[0m"
(ansi-color-apply text)
#+END_SRC

#+RESULTS: strip
: Hello world

#+BEGIN_SRC sh :results output :post strip(*this*)
curl 'wttr.in/?0'
#+END_SRC

#+RESULTS:
: Weather report: Yancheng, China
: 
:     \  /       Partly cloudy
:   _ /"".-.     15..16 °C      
:     \_(   ).   ← 12 km/h      
:     /(___(__)  10 km          
:                0.0 mm         

The :post header is documented in the org manual, see (org) post.

  • #+BEGIN_SRC sh :results output :post (ansi-color-apply *this*) should also work according to the doc. (Have not tried it yet.) No need for a second source block. – Tobias Nov 11 at 15:17
  • @Tobias I also noticed the manual mentions "even direct Emacs Lisp code execution" and I tried but it didn't work (eval: Symbol's value as variable is void: this), so I used a second source block. I'm using the builtin Org mode in Emacs 26.3. Not sure if it's a bug of Org. – xuchunyang Nov 11 at 16:17
  • 1
    You are right :post is buggy. Only accepts strings and then immediately searches the buffer for references. The advice (advice-add 'org-babel-ref-resolve :around (lambda (fun ref) (if (stringp ref) (funcall fun ref) (eval ref)))) makes #+begin_src sh :results output :post '(ansi-color-apply *this*) work. The quote is strange but unavoidable as far as I saw in the code. ob-core defines *this* as dynamic variable. This violates the coding guidelines for Emacs lisp packages. But they do not care at org. – Tobias Nov 11 at 17:31
3

You can use ansi-color to format the text. It doesn't have an interactive function, so you need to evaluate something like:

(require 'ansi-color)
(ansi-color-apply-on-region (point) (point-max))

That would treat the output in the current buffer from point to the end of buffer.

Note that some of the formatting gets lost if you save the file, close it, and open it again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.