Dabbling in Dart

I work at a big tech company on a programming language called Dart.

So, naturally I use it for any hobby programming and will probably end up writing Dart code to interact with Gemini.

Dart is a modern object-oriented language that’s easy to pick up; it looks like a simplified Java. Its big feature is that it runs everywhere: it compiles to javascript for the web, to native code for iOS and Android, and can run in JIT and native compiled modes on various desktop/server platforms.

You’re most likely to encounter Dart in day to day life as the language behind Flutter, the multi-platform app framework. But it’s just as well suited to simple scripting. Here is my first Dart code for Gemini:

import 'dart:io';

/// Replaces typewriter quotes and double dashes in all `.gmi` files under
/// the specified path with their nicer unicode equivalents.
///
/// Usage: dart fix_typography.dart <root path>
void main(List<String> arguments) {
  final gmis = Directory(arguments[0])
      .listSync(recursive: true)
      .whereType<File>()
      .where((f) => f.path.endsWith('.gmi'));
  for (final gmi in gmis) {
    print('Fixing ${gmi.path}.');
    final lines = gmi.readAsLinesSync();
    var skip = false;
    for (var i = 0; i != lines.length; ++i) {
      var line = lines[i];
      if (line.startsWith('```')) {
        skip = !skip;
        continue;
      }
      if (skip) continue;
      line = line.replaceAll("'", "’");
      line = line.replaceAll('--', '—');
      line = line.replaceAllMapped(RegExp(r'"(\w)'), (m) => '“${m.group(1)}');
      line = line.replaceAllMapped(RegExp(r'(\w)"'), (m) => '${m.group(1)}”');
      lines[i] = line;
    }
    gmi.writeAsStringSync(lines.join('\n'));
  }
}

It processes all `gmi` files under a root, replacing typewriter quotes and double dashes with their nicer unicode equivalents.

More fun that preprocessing would be a Gemini server in Dart; of course, if I write one, I’ll share!

Links

Dart: a client-optimized language for fast apps on any platform

Flutter: Build apps for any screen

Feedback 📮

👍 Thanks!

👎 Not for me.

🤷 No opinion.

Comments.

So far today, 2024-11-25, feedback has been received 2 times. Of these, 0 were likely from bots, and 2 might have been from real people. Thank you, maybe-real people!

   ———
 /     \   i a
| C   a \ D   n |
   irc   \     /
           ———