Saturday, 21 March 2020

Working with ttystudio and ttyrec

The best of all the tty to animated gif converters, is ttystudio because it outputs small gifs.

Sadly it is slow, and uses up way to much memory -- even running out of memory for even a small session.

Ironically it boasts of not using imagemagick while requiring node! No wonder it runs out of memory. Sadly the frams also seem to be based on clock time instead of ttyrec frames.

Even worse, it only works for recording live sessions.

This shell script causes ttystudio to convert a ttyrec file.

Use: ./ttyrec2studio ttyrecfilename
to make ttyrecfilename.gif

#! /bin/bash
ttystudio() {
  local ttyrec="$1"
  local fifo=$(mktemp -u)
  mkfifo "$fifo"

  coproc ttypipe { SHELL=$(realpath "$0") QUITPIPE="$fifo" TTYPLAYFILE="$ttyrec" command ttystudio "$ttyrec.gif" >/dev/tty 2>&1 ; }

  # when we've finished with fifo, ttyplay has finished
  cat "$fifo"
  # send ^Q
  printf $'\x11' >/proc/$$/fd/${ttypipe[1]}
  # close pipe to coproc
  eval "exec ${ttypipe[1]}<&-"
  # Drain pipe from coproc
  #eval "exec ${ttypipe[0]}<&-"
  eval "cat /proc/$$/fd/${ttypipe[0]}"
  wait $! # is coproc
  echo Done all $?

emit() {
  rm -f "$QUITPIPE"
  echo "Playing $1" >&3
  ttyplay "$1"
  echo "Finished playing $1 [$?]" >&3

main() {
  if test -z "$QUITPIPE"
  then ttystudio "$@"
  else emit "$TTYPLAYFILE"

main "$@"

It works by tricking ttysudio to re-invoke itself as the shell for what would have been the interactive subshell. As arguments can't be passed to the subshell, it smuggles them as environment variables.

When invoked as the subshell it simply uses ttyplay to re-play the ttyrec file.

Thursday, 18 July 2019

readline with bash's read -e

Want to use bash's read -e for full editing and readline support, but without bash completion or exposing the bash history?

bind -f /dev/stdin <<<"set disable-completion on"
HISTSIZE=0 HISTIGNORE="*" HISTFILE=/dev/null read -e ...

Thursday, 6 June 2019

synchronous pipe based task monitoring

I want a process to monitor another and know when it quits.

An obvious way if I control the launch is to tie them together with a pipe and they can detect when the pipe closes.

Maybe the other process would launch many further processes, all inheriting the pipe-fd, which I want to avoid.

So clearly the launcher needs to hold the pipe-fd but not share it (close-on-exec, don't fork too much), and then wait in the usual way for the launched process to quit, and then close the pipe.

Here's a bash incantation lifetime_fd which runs a simple list of arguments and can share an fd with another process through process substitution.

# just run a simple list, without affecting $?
just() {
set -- $? "$@"
  return $1

lifetime_fd() {
  set -- $_ "$@" ; eval "$1>&-" '"${@:2}"' ; just eval exec "$1>&-"

So if you want to run command fooly barly bazly and link it to the lame read && echo done then this will do the trick

lifetime_fd fooly barly bazly {_}> >( read && echo done )

So to attach a pipe descriptor to a sub-process, it is clear to use the trailing invocation {_}> >( sub-process ) which will attach stdin of the sub-process to a file descriptor to be stored in $_ which is managed in lifetime_fd

The variable $_ is used to avoid messing with any other variables. $_ is constantly adjusted and should do no harm if we abuse it; but as it is constantly adjusted, the first thing we do in lifetime_fd is to save it.

We don't use a local variable in case of a name clash that affects something else, so we store it as $1

We then run "$@" (or "${@:2}" as it now would be) but with the fd closed, so that it is not inherited.

We then close the fd while preserving the exit code.

You can invoke it in a pipeline like this:

get_report_request | lifetime_fd get_report {_}> >( monitor ) | send_report

An illustrative example of monitor (which reads until eof), might be:

monitor() {
  while read -t 1 || test $? = 142 # 142 is timeout code
  do echo -n '*'

which displays a star every second until stdin closes; by continually waiting up to 1 second to fail to read anything from stdin (until it closes, having a different exit code), and displays a star.

Of course it might read other data too.... if you can send it...

Monday, 25 February 2019

The Old Summer House

The Old Summer House

Sam Liddicott
The Fat Controller didn't seem at all bothered when he heard that Diesel had lost all the jobi wood into the sea. Diesel didn't seem too worried either, though a trifle scared at nearly falling into the sea himself.
Instead of salvaging what he could, the Fat Controller declared "More jobi wood must be found", "the search and rescue will be delayed".
My suspicious were not aroused, this waste of resources was typical on the Island of Sodor railway.
But my suspicions were aroused when Rocky the large breakdown crane happened to mention one morning that he was surprised just how deep the water was where Diesel lost the jobi wood. Rocky had come to the island to help put Gordon back on the rails, and wasn't involved in the jobi incident, and it was Thomas that had rescued Diesel. So what did Rocky know about it?
Later that year, after the search and rescue centre was complete, the Fat Controller invited everyone to a summer garden party, and to formally open his new summer house and gazebo.
I was there of course, as were most of the engines. Goodness knows how much it cost to lay the siding so that they could attend. It's hardly putting the shareholders money to good use.
I examined the summer house and gazebo. They were made-to-measure, and done very nicely, but the wood was rather cheaper than I expected, and not properly finished. "It must be painted with preservative" said the fat controller, "but there was not time to do it before the party". I carved my initials behind one the posts at the back. They would remember that "I woz ere".
The works manager was very pleased as the fat controller had promised him the old summer house and gazebo, if he would come and collect it. I went with him to help load the disassembled pieces from the front of the Fat Controllers driveway, and onto the back of a lorry, and then helped to assemble it.
"It's not very large!" said the works manager after we had finished assembling it a few days later, "but it will make a good shed for my model railway, though it is in worse condition than I expected."
As we sat looking at it with out drinks, I got to thinking about the poor use of shareholders money in laying a siding to the Fat Controllers house, and I decided to pay another visit.
As I walked up the driveway, I saw Harvey on the new siding by the Fat Controllers summer house, tearing up the track behind him.
The Fat Controller's wife came out showed me into the garden where the Fat Controller sat under the gazebo with the company auditor enjoying some drinks together, and I was cordially invited to join them.
"Our Fat Controller has rather carelessly cut himself carving some wood!" explained the auditor, pointing towards the bandaged.
The newness of the wound was evident by the bright red mark leaking through, whose colour I thought matched a bloodstain on the floor at the back of the post just where I could just see my initials carved, if I lent back in my chair.
After some talk about assets and amortisations which I could not understand, the Fat Controller excused himself, and so the auditor and I left.
"That's rather a nice summer house," I remarked, and a lovely colour.
"Yes," says the auditor, "it required the treatment of a special preservative which I understand affects it in a remarkable way, and imparts a uniquely heavy feel to the wood. It should last a long time"
And then, after a thought he added as we reached a lorry at the end of the driveway, "the Fat Controller has given me his old summer house, and I understand that you have experience assembling this sort of thing and may be able to help".
One of the posts had my initials carved on it. I noticed this as the auditor slapped the tarpaulin and said "You can fit a lot of bad boys on this one"
I noped out of there.

Tuesday, 11 September 2018

I Speak in Kingsley

I walked up and down
through the town, and round and round
and wondered what to say and why to say it

Then I purposed with an air
of sureness, and took care,
That when I did it, saying said it, I would say it.

Unfortunately it didn't work out like that
because I stubbed my toe
And said some other things instead,
which didn't really go,
I was quite embarrassed and my mother, she was too
And I really couldn't think of what I ought to do-
to break the silence that I started, so:

                                                        I screamed:
                      "Oh look, ohh look behind you!"
                      So they did, and when they had,
I took off running down the street
the pit pat padding of my feet
was all I heard besides my panting
'till I heard a train man chanting
"All aboard for Kingsley Station, time to go, the train is waiting"

So I climbed in and took a seat, nonchalantly trying not to attract attention;
which I can do quite well
(unless I stub my toe)
and the train started to go.

So I waited, looking out of the window as we went,
my foot going clackety clack,
my mind keeping time with the train,
and it started to rain

I was finally starting to calm down when we came to the next station,
and who do you think was waiting
with my mother
to get on the train?

I considered my solutions,
and came to my conclusion,
and operating smoothly I got into the toilet compartment,
waited till we started, pulled the cord and the alarm went -
"Stop the train" the cry went up,
the brakeman working frantically, a scraping screaming sound,

and stop we did! Stopped dead on the tracks
Nobody knew why so after a while we went again,
with a chuff chuff sound, and I carefully jumped out
landing softly on the ground, but as I stood and rose to my height,
past me went a window, and there, pressed to the glass,
were the faces of my mother and her three friends

Sam Liddicott
March 1992

I Wander

I wondered as I wandered
what was what,
and who was where,
And I thought a little more
as I wandered up the stair
and 'cos I wasn't looking
I slipped on three pairs of roller blades and two yin and a yang,
I bumped my pore head     and I trod on my hand
I didn't much like it, I shan't say what I said
But that should explain why I'm sitting in bed.

I should be out working
it's true that I'm not,
when I trod on my hand
I'm not sure what I got,
But it got all twisted
and pulled here and there,
which all should explain
why not to play on the stair

Sam Liddicott
December 1991 and March 1992

Sunday, 29 April 2018

Brexit: All stick and no carrot

"Travel," as Johnny Morris said in his Around the World in 25 Years, "not only, broadens the mind, but stretches it out and rolls it up like a chelsea bun until you don't know who you are, or what you are."

It also gives you jet lag, which means plenty of time to think about what you are too tired to write; until at last you reduce it so such simple points you have to write them.

In the matter of Brexit, remainers still have it so backwards that they keep shooting chunks out of their own foot under the delusion that it is someone else's foot. Their attempts to convert always lead the other way.

If we want home rule and self sovereignty then they offer even more sovereignty (over other countries) via the EU. Apparently sovereignty can be pooled, and in the EU there should be enough to satisfy our thirst for it.

If we want Britain to be ruled by the British, it isn't because we are worried about the race or nationality of the parliament that implements the final legislation to enact the will of the EU commission; and so no, we aren't easily satisfied that our own elected parliament will have the final say on the form of words used to bind us.

We want to be ruled by our own countrymen because they are like us, and they live within marching distance for an angry mob with pitchforks and flaming torches. It keeps them more honest.

But what about unity? What about international cooperation? What about friends and allies? We can't expect other European nations to co-operate in a true spirit of international friendship if we don't pay the EU to permit it.

Think of the hungry golden curly haired European children whose parents pharmaceutical businesses are failing because the EU won't permit the sale of life saving medicines to the suffering sick British golden curly haired children, and all because we wouldn't pay the yearly fee! Wouldn't we pay any amount of money to save those lives? (You can almost see an accounting with an adding machine).

But that argument hardly deserves an answer.; it portrays an EU that can grant nothing but the removal of barriers which it will otherwise enforce.

Then come the veiled threats: Perhaps we wish to avoid war? They suggest: think of all the wars in Europe to defend ourselves from foreign rule (since world war two) that the EU has avoided through the simple expedience of having member states surrender (I mean pool) national sovereignty in advance without need for battle!

Except for the EU army that is suddenly needed...

Stick and Carrot

Their problem is that EU Membership is all they have as carrot, and it having been rejected, they having nothing better to offer but stick.

In fact the EU is in a very hard and woody carrot, in fact pretty much all stick anyway, but it's all they have.

Blasting the Foot

So in what way are the repeatedly blasting their own foot?

The EU can only be made to look attractive in comparison to it's worst parts, and so remainers actually select and identify the worst parts of the EU and present them as some kind of potential exit deal or indefinite transition arrangement, along with larger ongoing fees.

The basis for this is their strategy to argue that if we can't get unity on what the initial leave arrangement should be, we should stay by default. But this is backwards, as I have written before. Our membership of the EU was never assented to and is illegitimate, and so it rather goes like this: if we can't agree on a way to stay, we should leave. It is the remain terms that need unity for us to remain; we already have a mandate to leave without compromise.

But, under their delusion, and in an effort to make remain look attractive, they start drawing attention to the woodier parts of their carrot in the form of various horror bad deals.

If we don't stay, we might leave but remain as part of the customs union (for the good of the economy, so we don't get thrown into the streets and starve). In the customs union we will have much of the obligations and duties but none of the voice - a condition that has been referred to as a vassal state. But the actual choice is to leave, entirely, in comparison to which their vassal-state deal looks absolutely stupid.

"How terrible, " we say, "we don't like that deal" (after all, it is a least carroty part of the stick), but they can't work out why we don't follow up with "so let's stay".

If we leave we will be required to put up a hard border between Eire and Northern Ireland and cause a shooting match. Required by who? The Irish don't want a hard border, the British don't want a hard border, it looks like the EU is making the problems here. The sooner we leave the better.

So now remainers are almost unaware pointing out the bad parts of the EU, are they close to converting?