A bit about the stuff I've done


Wednesday, 23 May 2012

Saving open files remotely (kate)

So I neglected to save a file on my home pc before leaving. Then at work I wanted to continue editing the same file. My home desktop was not configured for VNC so what to do? I investiagted attaching a vnc server to a running desktop, without access to that desktop but quickly came to the conclusion that it would just be a massive security flaw and hence not allowed. I was just about to give up when I remembered dbus With an ssh session open I was able to tell the instance of kate running on the home pc to save the file without ever seeing the desktop. And it's actually really easy! First you need to get the right display - for 99% of cases that will be display 0 export DISPLAY=:0 now you need to find the running instance of kate qdbus | grep kate hopefully there is only one Now identify the correct document (N.B. replace the number below with the once found from the previous statement) qdbus org.kde.kate-28399 | grep Document If there is only one document open then its easy, otherwise you need to go through each document and see if its the right one. Unfortunatly there doesn't appear to be a way to get the filename, so you'll have to inspect the content qdbus org.kde.kate-28399 /Kate/Document/1 org.kde.KTextEditor.Document.text Repeat this for all the documents until you find the one you want then issue: qdbus org.kde.kate-28399 /Kate/Document/13 org.kde.KTextEditor.Document.save If you get the response true then the document is now saved! I did investigate whether this same method could be used to attach a vnc server using krfb but (thankfully) it can't This method almost certainly can be adapted to other applications and editors though - as long as they support dbus

Thursday, 26 April 2012

Why I hate Imports/using

I'm sure other people must find this annoying, not just me.
And even Microsoft do it on MSDN.

What am I on about?

Posting code that requires importing a namespace - without specifying what namespace(s) need to be imported.

Imagine the following code:

Stream stMyStream = new FileStream("SomeFile.txt", FileMode.Open);

Now I know, and you know, that Stream exists within System.IO
but without a using directive at the top of your file, the compiler doesn't know this and it gives you a whole bunch of red squiggly lines.

In the above case it is obvious which namespace you need, but that isn't always so, and you can be left scratchign your head for ages trying to track down which namespace you are missing.

So please people: when you post code example - either use a fully qualified type name, or specify the appropriate namespaces to be used.
e.g.

System.IO.Stream stMyStream = new System.IO.FileStream("SomeFile.txt", System.IO.FileMode.Open);

Just a note to anyone who doesn't know:
the C# directive
using System.IO;
is equivalant to the VB statement
Imports System.IO


To add further confusion both VB and C# have a using statement with is to do with scoping and disposing of variables.


I use both C# and VB so I tend to interchange terms quite a lot!


</Rant>

Thursday, 2 February 2012

Cheetsheet

This is my "cheetsheet" or reference card.
I'll update this post as and when but really its just a place to store the stuff I always forget how to do or the exact syntax involved.

Bash:


For:

for f in dir/file*  do    echo "Processing $f"    # do something on $f  done
for i in {0..10..2}    do echo "Welcome $i times"  done

Ascii dec/hex/binary table

Key:

decimal numbers (base 10) hexadecimal numbers (base 16) ascii character or control code binary digits (base 2). The top table has binary 0 as the first digit, the bottom table has binary 1
The binary numbers across the top are the next most significant digits
The binary numbers down the left side are the final 4 digits
so for example: 182 (B6) has a 1 for the first digit as its in the bottom table, Next look to the top of the column to get 011
and finally look at the row to get 0110
So the entire sequence is 10110110
 
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
000
000
101
202
303
404
505
606
707
808
909
100A
110B
120C
130D
140E
150F
001
1610
1711
1812
1913
2014
2115
2216
2317
2418
2519
261A
271B
281C
291D
301E
311F
010
3220
3321!
3422"
3523#
3624$
3725%
3826&
3927'
4028(
4129)
422A*
432B+
442C,
452D-
462E.
472F/
011
48300
49311
50322
51333
52344
53355
54366
55377
56388
57399
583A:
593B;
603C<
613D=
623E>
633F?
100
6440@
6541A
6642B
6743C
6844D
6945E
7046F
7147G
7248H
7349I
744AJ
754BK
764CL
774DM
784EN
794FO
101
8050P
8151Q
8252R
8353S
8454T
8555U
8656V
8757W
8858X
8959Y
905AZ
915B[
925C\
935D]
945E^
955F_
110
9660`
9761a
9862b
9963c
10064d
10165e
10266f
10367g
10468h
10569i
1066Aj
1076Bk
1086Cl
1096Dm
1106En
1116Fo
111
11270p
11371q
11472r
11573s
11674t
11775u
11876v
11977w
12078x
12179y
1227Az
1237B{
1247C|
1257D}
1267E~
1277F
 
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
12880
12981
13082
13183ƒ
13284
13385
13486
13587
13688ˆ
13789
1388AŠ
1398B
1408CŒ
1418D
1428EŽ
1438F
14490
14591
14692
14793
14894
14995
15096
15197
15298˜
15399
1549Aš
1559B
1569Cœ
1579D
1589Ež
1599FŸ
160A0 
161A1¡
162A2¢
163A3£
164A4¤
165A5¥
166A6¦
167A7§
168A8¨
169A9©
170AAª
171AB«
172AC¬
173AD­
174AE®
175AF¯
176B0°
177B1±
178B2²
179B3³
180B4´
181B5µ
182B6
183B7·
184B8¸
185B9¹
186BAº
187BB»
188BC¼
189BD½
190BE¾
191BF¿
192C0À
193C1Á
194C2Â
195C3Ã
196C4Ä
197C5Å
198C6Æ
199C7Ç
200C8È
201C9É
202CAÊ
203CBË
204CCÌ
205CDÍ
206CEÎ
207CFÏ
208D0Ð
209D1Ñ
210D2Ò
211D3Ó
212D4Ô
213D5Õ
214D6Ö
215D7×
216D8Ø
217D9Ù
218DAÚ
219DBÛ
220DCÜ
221DDÝ
222DEÞ
223DFß
224E0à
225E1á
226E2â
227E3ã
228E4ä
229E5å
230E6æ
231E7ç
232E8è
233E9é
234EAê
235EBë
236ECì
237EDí
238EEî
239EFï
240F0ð
241F1ñ
242F2ò
243F3ó
244F4ô
245F5õ
246F6ö
247F7÷
248F8ø
249F9ù
250FAú
251FBû
252FCü
253FDý
254FEþ
255FFÿ

For a more complete Ascii table, look here

Thursday, 19 January 2012

Binding a datagridview to a list

Ok, so binding a grid to a list is easy, but what if you want changes in the list to be shown in the grid?
Well actually, it turns out, this is easy too - just not obvious.

Instead of using System.Collections.Generic.List<T>
use System.ComponentModel.BindingList<T>
It's exactly the same in all important regards, but it has the methods necessary for the grid to know that the list has changed

Easy huh!

Friday, 13 January 2012

Editing xml in vim

Yes, I use vim!

And large html, xml and xsl documents can be frustrating so here are a few things I sussed out today to make it easier:

  1. None of the commandline tools I could find were capable of cleanly formatting an xml document and putting comments in the correct alignment. All of them put the comments with no indentation.
    Some of you may prefer this but it stosp the following tips from working correctly in vim
    So I wrote a quick 5 min tool to do this, and here it is.
    Extract that archive somewhere into your path and then simply cat unformatted.xml | xmltidy > formatted.xml
    easy :)
    N.B. You'll need to have mono installed.
  2. Next I've assigned this new tool to an easy key in vim
    In your .vimrc file enter the following:
    :map <F2> :1,$!xmltidy<Enter>
    You can use any key you like of course
  3. Next I enabled code-folding
    Now there are plenty of posts out on the web about getting code folding with xml to work however I had issues with all of them.
    What I found to work was to enable folding by indentation (hence why comments have to be in the correct alignment).
    These 2 lines of vimrc sorted that out for me:
    au FileType htm,html,xml.xsl,xslt exe ":set foldmethod=indent"
    au BufNewFile,BufRead *.htm,*.html,*.xml,*.xsl,*.xslt exe ":set shiftwidth=2"

    N.B. I have my tabstop set to 2 so that pressing the tabkey indents the line by 2 columns instead of the usual 
  4.  Finally; if, like me, you don't like that ugly banner on the code folding lines then you'll want to change the colours with this line:
    :highlight Folded ctermbg=black
Having enabled the code folding you can use zo and zc to open and close a fold respectively.
N.B. when folding it folds everything at that level, not everything that is a child.
In other words if you had this document:
<xml>   <node1>     <node1a/>     <node1b/>   </node1>   <node2>     <node2a/>     <node2b/>   </node2> </xml>
and you press zc when the cursor is on the line with node1 then rather than hiding node1a and node1b like you might expect - it hides node1 and node2 (as well as their children)
 


Distinct in XSL

So I frequently have a need to do this but I can never remember how, so I figure why not use my shiny new blog and write about it here!

The problem:
  • You have some node list which contains many, many items.
  • You want to group items based on part of their content
  • You want to do it in xsl
Example:
The xml:
<phrases>   <node type="Greeting">Hello</node>   <node type="Farewell">Goodbye</node>   <node type="Greeting">Hi</node>   <node type="Greeting">Howdy</node>   <node type="Farewell">See you later</node> </phrases>
the result:
Greeting Farewell
The solution:
You can use the generate-id() function.
This function is part of xsl so you don't need any funky includes, but it means you can't use it from XPath (e.g. from C#'s SelectNodes() method)

<xsl:if test="generate-id(.) = generate-id(../node[@type=current()/@type])>   <xsl:value-of select="."/> </xsl:if>

And that's it!
Let's break it down

  • generate-id(.)
    Does exactly what you might expect i.e. it generates a unique ID which represents the current node. Calling generate-id on the same node twice will return the same id.
  • ../node[@type=current()/@type]
    This bit gets all the other nodes which have the same value based on the key we are grouping by (in this case

Visit my webites

As well as this blog I have 2 main websites currently.

  • My development website which lists the personal software projects I have developed
    http://dev.dj-djl.com
I've also got a new site, still under construction dedicated to the radio switcher project which has been my main focus of late.
http://dev.dj-djl.com/radioswitcher.aspx

When this project goes live I will probably give it its own domain name