Skip to content

Instantly share code, notes, and snippets.

View sma's full-sized avatar

Stefan Matthias Aust sma

  • I.C.N.H GmbH
  • Kiel
View GitHub Profile
@sma
sma / forth.dart
Last active August 4, 2023 02:07
a tiny forth interpreter
import 'package:flutter/material.dart';
void main() {
Forth().run('V: count 5 ! : inc dup @ 1 + ! ; '
'[ [ text ] count builder [ count inc ] " Increment text button ] '
'list column app run');
}
typedef Impl = void Function(Forth f);
@sma
sma / py.dart
Last active June 11, 2023 09:21
/// A Python runtime written in Dart.
///
/// Create [Python] instance to set and lookup variables, create classses
/// and instances and call functions or methods. It is highly incomplete.
///
/// It uses Dart types for Python types, that is `null` for `None`, `int`
/// for `int` and `double` for `float`, `String` for `str`, `PyList` for
/// `list` and `PyDictionary` for `dict`. Python classes are represented
/// by [PyClass] and instances by [PyInstance]. A function is represented
/// by [PyFunction]. A method is represented by [PyMethod] which combines
import 'dart:convert';
/// Provides translations from keys to either strings or plural forms.
///
/// Use `format('Hello %s', ['world'])` to format a string with arguments.
/// You can use `%s` for strings, `%d` for integers, and `%f` for doubles.
/// To escape a percent sign, use `%%`. You can also use `%p(key)` to
/// insert a plural form based on an intger argument.
///
/// Use `string('key')` or `string('key', ['arg'])` to the translation for
@sma
sma / presentation.dart
Created May 24, 2023 08:37
A proof of concept of a keynote-like presentation in Flutter
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
abstract class Slide extends StatelessWidget {
const Slide({super.key, this.title});
final String? title;
@override
Widget build(BuildContext context) {
import 'package:flutter/material.dart';
class ShieldBadge extends StatelessWidget {
const ShieldBadge({
super.key,
required this.leftLabel,
required this.rightLabel,
this.leftColor = Colors.black87,
this.rightColor = Colors.deepOrange,
this.textColor = Colors.white,

I want to support indentation in a multiline TextField.

What's the best way to do so? Here's my current approach.

To indent on TAB, I use an explicit TextEditingController and wrap the TextField in a CallbackShortcuts widget to detect the TAB key. I know I could use intents and actions here, but it's simpler this way. I can access then controller from the callbacks.

class _Editor extends State<Editor> {
  final _controller = TextEditingController();

I wrote

/// Parses [input] as an .ini configuration.
Map<String, Map<String, String>> 

and Copilot completed this:

/// Parses [input] as an .ini configuration.
Map<String, Map<String, String>> parse(String input) {

final result = >{};

@sma
sma / level_map.dart
Created May 1, 2023 09:52
a level map Flutter widget
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
@sma
sma / article.md
Last active April 15, 2023 21:24
A TYON implementation by ChatGPT (cannot deal with typed values)

Here is a ChatGPT experiment that might even result in something useful.

I recently saw a mention on HackerNews of a JSON-like data format called TYON for which a parser written in Dart might be quite useful. It shouldn't be too difficult to write by hand, but why not let ChatGPT 4 do the job?

Basically, in addition to JSON, TYON allows for Lisp-style comments, doesn't require " and starts with an implicit object so you need no top level {}. It also supports type definition that can be used to build objects from just data.

Here's my prompt:

Please write a parser in Dart for a file format according the specification below. Parsing a string of TYON should return a Map<String, dynamic> where dynamic is really either a String, another of such maps or a List<dynamic> where each element is again either a string, a map or a list.

@sma
sma / save.js
Created March 28, 2023 09:21
Save ChatGPT to Markdown – Paste into Chrome Dev Console
function h(html){return html.replace(/<li><p>/g,'\n - ').replace(/<p>/g,'\n\n').replace(/<\/p>/g,'').replace(/<b>/g,'**').replace(/<\/b>/g,'**').replace(/<i>/g,'_').replace(/<\/i>/g,'_').replace(/<code[^>]*>/g,(match)=>{const lm=match.match(/class="[^"]*language-([^"]*)"/);return lm?'\n```'+lm[1]+'\n':'```';}).replace(/<\/code[^>]*>/g,'```').replace(/<[^>]*>/g,'').replace(/Copy code/g,'').replace(/This content may violate our content policy. If you believe this to be in error, please submit your feedback — your input will aid our research in this area./g,'').trim();}(()=>{const e=document.querySelectorAll(".text-base");let t="";for(const s of e)s.querySelector(".whitespace-pre-wrap")&&(t+=t==""?"":"---\n",t+=`**${s.querySelectorAll('img').length>1?s.querySelectorAll('img')[1].alt:'ChatGPT'}**: ${h(s.querySelector(".whitespace-pre-wrap").innerHTML)}\n\n`);const o=document.createElement("a");o.download=(document.querySelector(".pr-14.bg-gray-800")?.innerText||"Conversation with ChatGPT")+".md",o.href=URL.create