how to make a scrapbook card of code snippets.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
CAsba
Posts: 389
Joined: Fri Sep 30, 2022 12:11 pm

how to make a scrapbook card of code snippets.

Post by CAsba » Mon Nov 14, 2022 5:09 pm

Hi all,
I thought I'd make a reference card of code examples I've used, for future use of the snippets. I put a couple of code lines into fields on the card, but to retrieve a snippet, copy it and paste it into an object editor seems impossible. Am I missing out on something much easier ?

stam
Posts: 2758
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: how to make a scrapbook card of code snippets.

Post by stam » Mon Nov 14, 2022 5:20 pm

Well if you use Dash you can use its snippet manager. It has a 'text-expander' funcion - type the name and function appears as if by magic.

Or just go low-brow and use Notes.app (if you're on Mac) - available everywhere incl. web, just create a folder for code and put entries in there - I'll be honest I do this most frequently.

Or why not write your own :)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9454
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: how to make a scrapbook card of code snippets.

Post by richmond62 » Mon Nov 14, 2022 5:22 pm

Screen Shot 2022-11-14 at 6.24.37 PM.png
-
Try a copy-paste from this field.
Attachments
code snip.livecode.zip
Stack.
(876 Bytes) Downloaded 75 times

CAsba
Posts: 389
Joined: Fri Sep 30, 2022 12:11 pm

Re: how to make a scrapbook card of code snippets.

Post by CAsba » Mon Nov 14, 2022 5:59 pm

Thanks guys, Im using Windows.
I may have come up with a make-do solution..
Storing the snippets in the code editor of a label on the first card. With my (poor) level of expertise I think this will have to do.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: how to make a scrapbook card of code snippets.

Post by dunbarx » Mon Nov 14, 2022 8:20 pm

CAsba.

These little stacks are a perfect way to learn. You make one, see what you missed and screwed up, and improve it. This process can never end, so be careful.

What you want to do is simple, and most of the work is in the user interface. The main question is how you want to search. There are about a thousand good ways to start. But let's say you have a single field called "codeSnippets" on a card that contains all of your snippets. It does not even have to be visible. When you load these you insert a leading line such as "snippet", along with a few identifying word. Let us say "snippet find Date".

You then might have another field that has all your snippets:
snippet sort by name
snippet do this
snippet find date
snippet whatever
Then, when you want to find the snippet of interest:

Code: Select all

on mouseUp
   set the itemDel to "snippet"
   ask "Snippet?" -- enter keywords here
   get lineOffset("snippet" && it,fld "codeSnippets")
   put the number of items of line 1 to it of fld "codeSnippets" into numItems
   get item numItems of fld "codeSnippets"
   delete line 1 of it
   set the clipBoardData to it
   answer it
end mouseUp
Re-using "it" a lot. Never a good idea...

Craig
Last edited by dunbarx on Wed Nov 16, 2022 4:13 pm, edited 5 times in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9454
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: how to make a scrapbook card of code snippets.

Post by richmond62 » Mon Nov 14, 2022 9:15 pm

And, so what that you use Windows: I use Linux and Macintosh.

As far as copy-pasting from a text field goes, that should not make a blind bit of difference.

LiveCode claim their thing is 'cross-platform.'

stam
Posts: 2758
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: how to make a scrapbook card of code snippets.

Post by stam » Mon Nov 14, 2022 9:44 pm

richmond62 wrote:
Mon Nov 14, 2022 9:15 pm
And, so what that you use Windows: I use Linux and Macintosh.
* can't use Dash - but if you don't use it you don't know what you're missing so it's all good ;)

If I were to want to store snippets in live code it' would be easy enough - the final stack would be a palette so you could past into a field without it loosing focus. I might just build one for the hell of it ;)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: how to make a scrapbook card of code snippets.

Post by dunbarx » Mon Nov 14, 2022 11:28 pm

Or why not write your own :)
Exactly the right way.

Are we talking about programs outside of LC with this snippet library/loader? Why would, assuming that these snippets are LC snippets, anyone leave LC for such a resource?

I modified my handler to load the clipBoardData directly, so it can be pasted.

Or am I missing something?

Craig

stam
Posts: 2758
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: how to make a scrapbook card of code snippets.

Post by stam » Tue Nov 15, 2022 4:06 am

dunbarx wrote:
Mon Nov 14, 2022 11:28 pm
Why would, assuming that these snippets are LC snippets, anyone leave LC for such a resource?
Craig.
Υou don't leave LC. That is the point you are missing with Dash.

Whatever you do, you'd need to leave the window you're working on to store the snippet, be it in LC or externally.
If stored in LC, what would be your workflow? You'd need to launch the stack/plugin you created, select the snippet and copy/paste it.
Using Dash, you just need to type in LC's script editor and Dash actually fills in the code for you. Major improvement.

For example, some time back I create a universal function for sorting single- or multidimensional arrays with the help of many in the forum here. I named it it (imaginatively enough) sortArray, which returns the array sorted as desired (ok let's not get into the philosophy of sorting associative arrays...). I store this function in Dash and gave it the shortcut name functionSortArray.

If I just type functionSortArray in the script editor, Dash automatically puts the entire function into my script editor, without ever leaving LC.
I don't even leave the script editor. No searching, copying, pasting.
Does that make sense now?

Replicating this in LC would be a challenge - and why would you waste time re-inveting the wheel someone else has so beautifully crafted, even if it isn't in LC?

And what is this weird mentality that everything has to be done in LiveCode I keep seeing recently on this forum?
Always use the best tool for the job :)



The other point you may be missing is that 'snippets' aren't fragments of text - what I'm describing above is storing handlers/functions for re-use.
And I wonder how good your handler would be if managing 150 handlers/functions that each may take up to 500 lines?
Would storing these all in single field and somehow divining the start and end of each function/handler really be practical?

Ideally each 'snippet' should also have some meta data: a description/help text, ideally a name/identifier etc. The point is to store your methods in a way that when you come back to them a year later they can still make sense. At the very least these should be stored as an array or database in my mind...

A lot of this is doable in LC - but entering text into the script editor as you type an identifier for the snippet would not be easy and would likely slow it down even more... Ideally I'd like to see LC build this functionality into the iDE - but Dash works superbly if on a Mac...

S.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: how to make a scrapbook card of code snippets.

Post by dunbarx » Tue Nov 15, 2022 5:07 am

Stam.

I did not appreciate either what Dash was nor how well it seems to work "within" LC. If I had, I would not have posted at all. i would never rclaim the clipBoard is similarly suspect because it is "outside" of LC. It is, of course, but I certainly have no issue with it, since it is so well integrated.

I assumed that "snippets" would include fully developed handlers, including, well, snippets.

Thanks for the explanation.

Craig

CAsba
Posts: 389
Joined: Fri Sep 30, 2022 12:11 pm

Re: how to make a scrapbook card of code snippets.

Post by CAsba » Wed Nov 16, 2022 1:37 pm

Thanks Craig, for the work you put into answering with your suggestion. At the moment, there are just a few snippets, but when they get more than enough to find them difficult, I'll do what you suggest.

stam
Posts: 2758
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: how to make a scrapbook card of code snippets.

Post by stam » Mon Nov 21, 2022 9:37 pm

Hi CAsba and all

I still think that if doing this in LiveCode, an sqlite database is a preferable way to to do this (thinking especially of this thread: https://forums.livecode.com/viewtopic.php?f=9&t=37466) - so I put my money where my mouth is and rolled my own. After all, it's one thing to suggest random things and another to actually create as solution ;)

This should be a fairly easy-to-use snippet manager that you can add to your plugins folder if you wish and should be good to go (he says...). It stores the data in an sqlite database which, as we know is super-fast - just make sure it's in the same folder as the stack (ie the folder as downloaded - I guess I could add code to create an sqlite database if it's missing, but haven't done this yet...).

list.jpg
In the main view you can add snippets with the '+' button. Copy and edit snippets with the buttons in the data grid. Delete via the contextual menu (right-click on the data grid). Use the search field in the list view to search name/description/tags.

detail.jpg
In the detail view all changes are saved as soon as you exit the field, so make sure you don't accidentally do something you didn't intend to do. Each entry can have tags, in addition to a name, description and notes; an author field and a relevant URL field (so you can remember who to thank for a snippet ;) )

Code in the detail view is automatically colourised on textChanged and formatted on return and tab in field, just like the Script Editor (although the text stored is just plain text). Layout is responsive in both views, but by default the detail view is scaled off the list view size. Fun little project and a good opportunity to brush up on those rusty sqlite skills - liveCloud has made me lazy! On that note, if there is a demand I could add an online/shared section to be able to view/share snippets from others. I could also include a tag cloud (following a blog post on this by LC). But that's a bit more work and this was more to show an SQLite solution and a few little other tricks.


Download it from my GitHub:

https://github.com/stam66/SnippetManager

Easiest is to click on the 'Code' button and download the .zip file to download the folder intact (MIT license - free use/reuse but the copyright notice has to follow in distribution). All feedback welcome...

S.

keliko
Posts: 92
Joined: Thu Aug 01, 2019 8:15 am

Re: how to make a scrapbook card of code snippets.

Post by keliko » Thu Sep 14, 2023 4:38 pm

Thanks stam.. :D :D

stam
Posts: 2758
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: how to make a scrapbook card of code snippets.

Post by stam » Thu Sep 14, 2023 4:40 pm

It’s far from perfect so if something isn’t doing what it’s supposed to, please raise and issue on its github page…

I’ve been meaning to get back to this and fix stuff, but just haven’t had time. But I will at some point ;)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”